harmony 鸿蒙Interface (VideoSession)

  • 2025-06-12
  • 浏览 (5)

Interface (VideoSession)

说明:

本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

VideoSession extends Session, Flash, AutoExposure, Focus, Zoom, Stabilization, ColorManagement, AutoDeviceSwitch, Macro

普通录像模式会话类,提供了对闪光灯、曝光、对焦、变焦、视频防抖、色彩空间及微距的操作。

说明:

默认的视频录制模式,适用于一般场景。支持720P、1080p等多种分辨率的录制,可选择不同帧率(如30fps、60fps)。

canPreconfig12+

canPreconfig(preconfigType: PreconfigType, preconfigRatio?: PreconfigRatio): boolean

查询当前Session是否支持指定的与配置类型。

原子化服务API: 从API version 19开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Camera.Core

参数:

参数名 类型 必填 说明
preconfigType PreconfigType 指定配置预期分辨率。
preconfigRatio PreconfigRatio 可选画幅比例,默认为16:9。

返回值:

类型 说明
boolean true: 支持指定预配值类型。
false: 不支持指定预配值类型。

错误码:

以下错误码的详细介绍请参见Camera错误码

错误码ID 错误信息
7400201 Camera service fatal error.

示例:

function testCanPreconfig(videoSession: camera.VideoSession, preconfigType: camera.PreconfigType,
  preconfigRatio: camera.PreconfigRatio): void {
  try {
    let result = videoSession.canPreconfig(preconfigType, preconfigRatio);
    console.info(`canPreconfig ${preconfigType} ${preconfigRatio} result is : ${result}`);
  } catch (error) {
    let err = error as BusinessError;
    console.error(`The canPreconfig call failed. error code: ${err.code}`);
  }
}

preconfig12+

preconfig(preconfigType: PreconfigType, preconfigRatio?: PreconfigRatio): void

对当前Session进行预配置。

原子化服务API: 从API version 19开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Camera.Core

参数:

参数名 类型 必填 说明
preconfigType PreconfigType 指定配置预期分辨率。
preconfigRatio PreconfigRatio 可选画幅比例,默认为16:9。

错误码:

以下错误码的详细介绍请参见Camera错误码

错误码ID 错误信息
7400201 Camera service fatal error.

示例:

function testPreconfig(videoSession: camera.VideoSession, preconfigType: camera.PreconfigType,
  preconfigRatio: camera.PreconfigRatio): void {
  try {
    videoSession.preconfig(preconfigType, preconfigRatio);
    console.info(`preconfig ${preconfigType} ${preconfigRatio} success`);
  } catch (error) {
    let err = error as BusinessError;
    console.error(`The preconfig call failed. error code: ${err.code}`);
  }
}

on(‘error’)11+

on(type: ‘error’, callback: ErrorCallback): void

监听普通录像会话的错误事件,通过注册回调函数获取结果。使用callback异步回调。

说明:

当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。

原子化服务API: 从API version 19开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Camera.Core

参数:

参数名 类型 必填 说明
type string 监听事件,固定为’error’,session创建成功之后可监听该接口。session调用相关接口出现错误时会触发该事件,比如调用beginConfigcommitConfigaddInput等接口发生错误时返回错误信息。
callback ErrorCallback 回调函数,用于获取错误信息。返回错误码,错误码类型CameraErrorCode

示例:

import { BusinessError } from '@kit.BasicServicesKit';

function callback(err: BusinessError): void {
  console.error(`Video session error code: ${err.code}`);
}

function registerSessionError(videoSession: camera.VideoSession): void {
  videoSession.on('error', callback);
}

off(‘error’)11+

off(type: ‘error’, callback?: ErrorCallback): void

注销监听普通录像会话的错误事件,通过注册回调函数获取结果。

原子化服务API: 从API version 19开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Camera.Core

参数:

参数名 类型 必填 说明
type string 监听事件,固定为’error’,session创建成功之后可监听该接口。
callback ErrorCallback 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。

示例:

function unregisterSessionError(videoSession: camera.VideoSession): void {
  videoSession.off('error');
}

on(‘focusStateChange’)11+

on(type: ‘focusStateChange’, callback: AsyncCallback<FocusState>): void

监听相机聚焦的状态变化,通过注册回调函数获取结果。使用callback异步回调。

说明:

当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。

原子化服务API: 从API version 19开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Camera.Core

参数:

参数名 类型 必填 说明
type string 监听事件,固定为’focusStateChange’,session创建成功可监听。仅当自动对焦模式时,且相机对焦状态发生改变时可触发该事件。
callback AsyncCallback<FocusState> 回调函数,用于获取当前对焦状态。

示例:

import { BusinessError } from '@kit.BasicServicesKit';

function callback(err: BusinessError, focusState: camera.FocusState): void {
  if (err !== undefined && err.code !== 0) {
    console.error(`Callback Error, errorCode: ${err.code}`);
    return;
  }
  console.info(`Focus state: ${focusState}`);
}

function registerFocusStateChange(videoSession: camera.VideoSession): void {
  videoSession.on('focusStateChange', callback);
}

off(‘focusStateChange’)11+

off(type: ‘focusStateChange’, callback?: AsyncCallback<FocusState>): void

注销监听相机聚焦的状态变化。

原子化服务API: 从API version 19开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Camera.Core

参数:

参数名 类型 必填 说明
type string 监听事件,固定为’focusStateChange’,session创建成功可监听。
callback AsyncCallback<FocusState> 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。

示例:

function unregisterFocusStateChange(videoSession: camera.VideoSession): void {
  videoSession.off('focusStateChange');
}

on(‘smoothZoomInfoAvailable’)11+

on(type: ‘smoothZoomInfoAvailable’, callback: AsyncCallback<SmoothZoomInfo>): void

监听相机平滑变焦的状态变化,通过注册回调函数获取结果。使用callback异步回调。

说明:

当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。

原子化服务API: 从API version 19开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Camera.Core

参数:

参数名 类型 必填 说明
type string 监听事件,固定为’smoothZoomInfoAvailable’,session创建成功可监听。
callback AsyncCallback<SmoothZoomInfo> 回调函数,用于获取当前平滑变焦状态。

示例:

import { BusinessError } from '@kit.BasicServicesKit';

function callback(err: BusinessError, smoothZoomInfo: camera.SmoothZoomInfo): void {
  if (err !== undefined && err.code !== 0) {
    console.error(`Callback Error, errorCode: ${err.code}`);
    return;
  }
  console.info(`The duration of smooth zoom: ${smoothZoomInfo.duration}`);
}

function registerSmoothZoomInfo(videoSession: camera.VideoSession): void {
  videoSession.on('smoothZoomInfoAvailable', callback);
}

off(‘smoothZoomInfoAvailable’)11+

off(type: ‘smoothZoomInfoAvailable’, callback?: AsyncCallback<SmoothZoomInfo>): void

注销监听相机平滑变焦的状态变化。

原子化服务API: 从API version 19开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Camera.Core

参数:

参数名 类型 必填 说明
type string 监听事件,固定为’smoothZoomInfoAvailable’,session创建成功可监听。
callback AsyncCallback<SmoothZoomInfo> 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。

示例:

function unregisterSmoothZoomInfo(videoSession: camera.VideoSession): void {
  videoSession.off('smoothZoomInfoAvailable');
}

on(‘autoDeviceSwitchStatusChange’)13+

on(type: ‘autoDeviceSwitchStatusChange’, callback: AsyncCallback<AutoDeviceSwitchStatus>): void

监听相机自动切换镜头状态变化,通过注册回调函数获取结果。使用callback异步回调。

说明:

当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。

原子化服务API: 从API version 19开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Camera.Core

参数:

参数名 类型 必填 说明
type string 监听事件,固定为’autoDeviceSwitchStatusChange’,session创建成功可监听。
callback AsyncCallback<AutoDeviceSwitchStatus> 回调函数,用于获取当前自动切换镜头的状态。

示例:

import { BusinessError } from '@kit.BasicServicesKit';

function callback(err: BusinessError, autoDeviceSwitchStatus: camera.AutoDeviceSwitchStatus): void {
  if (err !== undefined && err.code !== 0) {
    console.error(`Callback Error, errorCode: ${err.code}`);
    return;
  }
  console.info(`isDeviceSwitched: ${autoDeviceSwitchStatus.isDeviceSwitched}, isDeviceCapabilityChanged: ${autoDeviceSwitchStatus.isDeviceCapabilityChanged}`);
}

function registerAutoDeviceSwitchStatus(videoSession: camera.VideoSession): void {
  videoSession.on('autoDeviceSwitchStatusChange', callback);
}

off(‘autoDeviceSwitchStatusChange’)13+

off(type: ‘autoDeviceSwitchStatusChange’, callback?: AsyncCallback<AutoDeviceSwitchStatus>): void

注销监听相机自动切换镜头状态变化。

原子化服务API: 从API version 19开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Camera.Core

参数:

参数名 类型 必填 说明
type string 监听事件,固定为’autoDeviceSwitchStatusChange’,session创建成功可监听。
callback AsyncCallback<AutoDeviceSwitchStatus> 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。

示例:

function unregisterSmoothZoomInfo(videoSession: camera.VideoSession): void {
  videoSession.off('autoDeviceSwitchStatusChange');
}

setQualityPrioritization14+

setQualityPrioritization(quality : QualityPrioritization) : void;

设置录像质量优先级。

说明: 默认为高质量,设置为功耗平衡将降低录像质量以减少功耗。实际功耗收益因平台而异。

原子化服务API: 从API version 19开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Camera.Core

参数:

参数名 类型 必填 说明
quality QualityPrioritization 需要设置的视频质量优先级(默认为高质量)。

错误码:

以下错误码的详细介绍请参见Camera错误码

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
7400103 Session not config. The session has not been committed or configured.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

function setQualityPrioritization(videoSession: camera.VideoSession): void {
  try {
    videoSession.setQualityPrioritization(camera.QualityPrioritization.POWER_BALANCE);
  } catch (error) {
    // 失败返回错误码error.code并处理。
    let err = error as BusinessError;
    console.error(`The setQualityPrioritization call failed. error code: ${err.code}`);
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Camera Kit(相机服务)

harmony 鸿蒙Interface (AutoDeviceSwitch)

harmony 鸿蒙Interface (AutoDeviceSwitchQuery)

harmony 鸿蒙Interface (AutoExposure)

harmony 鸿蒙Interface (AutoExposureQuery)

harmony 鸿蒙Interface (CameraInput)

harmony 鸿蒙Interface (CameraManager)

harmony 鸿蒙Interface (CameraOutput)

harmony 鸿蒙废弃的Interface (CaptureSession, deprecated)

harmony 鸿蒙Interface (ColorManagement)

0  赞