• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Interface (VideoSession)
2<!--Kit: Camera Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @qano-->
5<!--SE: @leo_ysl-->
6<!--TSE: @xchaosioda-->
7
8> **NOTE**
9>
10> - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
11> - The initial APIs of this interface are supported since API version 11.
12
13VideoSession inhertis from [Session](arkts-apis-camera-Session.md), [Flash](arkts-apis-camera-Flash.md), [AutoExposure](arkts-apis-camera-AutoExposure.md), [WhiteBalance](arkts-apis-camera-WhiteBalance.md), [Focus](arkts-apis-camera-Focus.md), [Zoom](arkts-apis-camera-Zoom.md), [Stabilization](arkts-apis-camera-Stabilization.md), [ColorManagement](arkts-apis-camera-ColorManagement.md), [AutoDeviceSwitch](arkts-apis-camera-AutoDeviceSwitch.md), [Macro](arkts-apis-camera-Macro.md), and [ControlCenter](arkts-apis-camera-ControlCenter.md).
14
15It implements a video session, which provides operations on the flash, exposure, white balance, focus, zoom, video stabilization, color space, macro mode, and controller.
16
17> **NOTE**
18>
19> This class is provided for the default video recording mode. It applies to common scenarios. It supports recording at various resolutions (such as 720p and 1080p) and frame rates (such as 30 fps and 60 fps).
20
21## Modules to Import
22
23```ts
24import { camera } from '@kit.CameraKit';
25```
26
27## canPreconfig<sup>12+</sup>
28
29canPreconfig(preconfigType: PreconfigType, preconfigRatio?: PreconfigRatio): boolean
30
31Checks whether this session supports a preconfigured resolution.
32
33**Atomic service API**: This API can be used in atomic services since API version 19.
34
35**System capability**: SystemCapability.Multimedia.Camera.Core
36
37**Parameters**
38
39| Name           | Type                                 | Mandatory | Description             |
40|----------------|-------------------------------------|-----|-----------------|
41| preconfigType  | [PreconfigType](arkts-apis-camera-e.md#preconfigtype12)   | Yes  | Resolution type.     |
42| preconfigRatio | [PreconfigRatio](arkts-apis-camera-e.md#preconfigratio12) | No  | Aspect ratio. The default value is 16:9.|
43
44**Return value**
45
46| Type     | Description                                     |
47|---------|-----------------------------------------|
48| boolean | **true**: The preconfigured resolution is supported.<br>**false**: The preconfigured resolution is not supported.|
49
50**Error codes**
51
52For details about the error codes, see [Camera Error Codes](errorcode-camera.md).
53
54| ID  | Error Message                       |
55|---------|-----------------------------|
56| 7400201 | Camera service fatal error. |
57
58**Example**
59
60```ts
61function testCanPreconfig(videoSession: camera.VideoSession, preconfigType: camera.PreconfigType,
62  preconfigRatio: camera.PreconfigRatio): void {
63  try {
64    let result = videoSession.canPreconfig(preconfigType, preconfigRatio);
65    console.info(`canPreconfig ${preconfigType} ${preconfigRatio} result is : ${result}`);
66  } catch (error) {
67    let err = error as BusinessError;
68    console.error(`The canPreconfig call failed. error code: ${err.code}`);
69  }
70}
71```
72
73## preconfig<sup>12+</sup>
74
75preconfig(preconfigType: PreconfigType, preconfigRatio?: PreconfigRatio): void
76
77Preconfigures this session.
78
79**Atomic service API**: This API can be used in atomic services since API version 19.
80
81**System capability**: SystemCapability.Multimedia.Camera.Core
82
83**Parameters**
84
85| Name           | Type                                 | Mandatory | Description             |
86|----------------|-------------------------------------|-----|-----------------|
87| preconfigType  | [PreconfigType](arkts-apis-camera-e.md#preconfigtype12)   | Yes  | Resolution type.     |
88| preconfigRatio | [PreconfigRatio](arkts-apis-camera-e.md#preconfigratio12) | No  | Aspect ratio. The default value is 16:9.|
89
90**Error codes**
91
92For details about the error codes, see [Camera Error Codes](errorcode-camera.md).
93
94| ID  | Error Message                       |
95|---------|-----------------------------|
96| 7400201 | Camera service fatal error. |
97
98**Example**
99
100```ts
101function testPreconfig(videoSession: camera.VideoSession, preconfigType: camera.PreconfigType,
102  preconfigRatio: camera.PreconfigRatio): void {
103  try {
104    videoSession.preconfig(preconfigType, preconfigRatio);
105    console.info(`preconfig ${preconfigType} ${preconfigRatio} success`);
106  } catch (error) {
107    let err = error as BusinessError;
108    console.error(`The preconfig call failed. error code: ${err.code}`);
109  }
110}
111```
112
113## on('error')<sup>11+</sup>
114
115on(type: 'error', callback: ErrorCallback): void
116
117Subscribes to VideoSession error events. This API uses an asynchronous callback to return the result.
118
119> **NOTE**
120>
121> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**.
122
123**Atomic service API**: This API can be used in atomic services since API version 19.
124
125**System capability**: SystemCapability.Multimedia.Camera.Core
126
127**Parameters**
128
129| Name    | Type             | Mandatory| Description                          |
130| -------- | ------------------ | ---- | ------------------------------ |
131| type     | string             | Yes  | Event type. The value is fixed at **'error'**. The event can be listened for when a session is created. This event is triggered and the error message is returned when an error occurs during the calling of a session-related API such as [beginConfig](arkts-apis-camera-Session.md#beginconfig11), [commitConfig](arkts-apis-camera-Session.md#commitconfig11), and [addInput](arkts-apis-camera-Session.md#addinput11).|
132| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback)      | Yes  | Callback used to return an error code defined in [CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode).  |
133
134**Example**
135
136```ts
137import { BusinessError } from '@kit.BasicServicesKit';
138
139function callback(err: BusinessError): void {
140  console.error(`Video session error code: ${err.code}`);
141}
142
143function registerSessionError(videoSession: camera.VideoSession): void {
144  videoSession.on('error', callback);
145}
146```
147
148## off('error')<sup>11+</sup>
149
150off(type: 'error', callback?: ErrorCallback): void
151
152Unsubscribes from VideoSession error events. This API uses a callback to return the result.
153
154**Atomic service API**: This API can be used in atomic services since API version 19.
155
156**System capability**: SystemCapability.Multimedia.Camera.Core
157
158**Parameters**
159
160| Name    | Type                         | Mandatory| Description                          |
161| -------- | --------------------------- | ---- | ------------------------------ |
162| type     | string                    | Yes  | Event type. The value is fixed at **'error'**. The event can be listened for when a session is created.|
163| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback)| No  | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.|
164
165**Example**
166
167```ts
168function unregisterSessionError(videoSession: camera.VideoSession): void {
169  videoSession.off('error');
170}
171```
172
173## on('focusStateChange')<sup>11+</sup>
174
175on(type: 'focusStateChange', callback: AsyncCallback\<FocusState\>): void
176
177Subscribes to focus state change events. This API uses an asynchronous callback to return the result.
178
179> **NOTE**
180>
181> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**.
182
183**Atomic service API**: This API can be used in atomic services since API version 19.
184
185**System capability**: SystemCapability.Multimedia.Camera.Core
186
187**Parameters**
188
189| Name    | Type                   | Mandatory| Description                      |
190| -------- | ---------------- | ---- | ------------------------ |
191| type     | string                                    | Yes  | Event type. The value is fixed at **'focusStateChange'**. The event can be listened for when a session is created. This event is triggered only when the camera focus state changes in auto focus mode.|
192| callback | AsyncCallback\<[FocusState](arkts-apis-camera-e.md#focusstate)\> | Yes  | Callback used to return the focus state change. |
193
194**Example**
195
196```ts
197import { BusinessError } from '@kit.BasicServicesKit';
198
199function callback(err: BusinessError, focusState: camera.FocusState): void {
200  if (err !== undefined && err.code !== 0) {
201    console.error(`Callback Error, errorCode: ${err.code}`);
202    return;
203  }
204  console.info(`Focus state: ${focusState}`);
205}
206
207function registerFocusStateChange(videoSession: camera.VideoSession): void {
208  videoSession.on('focusStateChange', callback);
209}
210```
211
212## off('focusStateChange')<sup>11+</sup>
213
214off(type: 'focusStateChange', callback?: AsyncCallback\<FocusState\>): void
215
216Unsubscribes from focus state change events.
217
218**Atomic service API**: This API can be used in atomic services since API version 19.
219
220**System capability**: SystemCapability.Multimedia.Camera.Core
221
222**Parameters**
223
224| Name    | Type                                     | Mandatory| Description                      |
225| -------- | ----------------------------------------- | ---- | ------------------------ |
226| type     | string                                    | Yes  | Event type. The value is fixed at **'focusStateChange'**. The event can be listened for when a session is created.|
227| callback | AsyncCallback\<[FocusState](arkts-apis-camera-e.md#focusstate)\> | No | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.|
228
229**Example**
230
231```ts
232function unregisterFocusStateChange(videoSession: camera.VideoSession): void {
233  videoSession.off('focusStateChange');
234}
235```
236
237## on('smoothZoomInfoAvailable')<sup>11+</sup>
238
239on(type: 'smoothZoomInfoAvailable', callback: AsyncCallback\<SmoothZoomInfo\>): void
240
241Subscribes to smooth zoom state change events. This API uses an asynchronous callback to return the result.
242
243> **NOTE**
244>
245> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**.
246
247**Atomic service API**: This API can be used in atomic services since API version 19.
248
249**System capability**: SystemCapability.Multimedia.Camera.Core
250
251**Parameters**
252
253| Name    | Type                  | Mandatory| Description                      |
254| -------- | ----------------------- | ---- | ------------------------ |
255| type     | string                  | Yes  | Event type. The value is fixed at **'smoothZoomInfoAvailable'**. The event can be listened for when a session is created.|
256| callback | AsyncCallback\<[SmoothZoomInfo](arkts-apis-camera-i.md#smoothzoominfo11)\> | Yes  | Callback used to return the smooth zoom state change. |
257
258**Example**
259
260```ts
261import { BusinessError } from '@kit.BasicServicesKit';
262
263function callback(err: BusinessError, smoothZoomInfo: camera.SmoothZoomInfo): void {
264  if (err !== undefined && err.code !== 0) {
265    console.error(`Callback Error, errorCode: ${err.code}`);
266    return;
267  }
268  console.info(`The duration of smooth zoom: ${smoothZoomInfo.duration}`);
269}
270
271function registerSmoothZoomInfo(videoSession: camera.VideoSession): void {
272  videoSession.on('smoothZoomInfoAvailable', callback);
273}
274```
275
276## off('smoothZoomInfoAvailable')<sup>11+</sup>
277
278off(type: 'smoothZoomInfoAvailable', callback?: AsyncCallback\<SmoothZoomInfo\>): void
279
280Unsubscribes from smooth zoom state change events.
281
282**Atomic service API**: This API can be used in atomic services since API version 19.
283
284**System capability**: SystemCapability.Multimedia.Camera.Core
285
286**Parameters**
287
288| Name    | Type                                     | Mandatory| Description                      |
289| -------- | ----------------------------------------- | ---- | ------------------------ |
290| type     | string              | Yes  | Event type. The value is fixed at **'smoothZoomInfoAvailable'**. The event can be listened for when a session is created.|
291| callback | AsyncCallback\<[SmoothZoomInfo](arkts-apis-camera-i.md#smoothzoominfo11)\> | No  | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.|
292
293**Example**
294
295```ts
296function unregisterSmoothZoomInfo(videoSession: camera.VideoSession): void {
297  videoSession.off('smoothZoomInfoAvailable');
298}
299```
300
301## on('autoDeviceSwitchStatusChange')<sup>13+</sup>
302
303on(type: 'autoDeviceSwitchStatusChange', callback: AsyncCallback\<AutoDeviceSwitchStatus\>): void
304
305Subscribes to automatic camera switch status change events. This API uses an asynchronous callback to return the result.
306
307> **NOTE**
308>
309> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**.
310
311**Atomic service API**: This API can be used in atomic services since API version 19.
312
313**System capability**: SystemCapability.Multimedia.Camera.Core
314
315**Parameters**
316
317| Name    | Type                                                                  | Mandatory| Description                      |
318| -------- |----------------------------------------------------------------------| ---- | ------------------------ |
319| type     | string                                                               | Yes  | Event type. The value is fixed at **'autoDeviceSwitchStatusChange'**. The event can be listened for when a session is created.|
320| callback | AsyncCallback\<[AutoDeviceSwitchStatus](arkts-apis-camera-i.md#autodeviceswitchstatus13)\> | Yes  | Callback function, which is used to obtain the status of automatic camera switch. |
321
322**Example**
323
324```ts
325import { BusinessError } from '@kit.BasicServicesKit';
326
327function callback(err: BusinessError, autoDeviceSwitchStatus: camera.AutoDeviceSwitchStatus): void {
328  if (err !== undefined && err.code !== 0) {
329    console.error(`Callback Error, errorCode: ${err.code}`);
330    return;
331  }
332  console.info(`isDeviceSwitched: ${autoDeviceSwitchStatus.isDeviceSwitched}, isDeviceCapabilityChanged: ${autoDeviceSwitchStatus.isDeviceCapabilityChanged}`);
333}
334
335function registerAutoDeviceSwitchStatus(videoSession: camera.VideoSession): void {
336  videoSession.on('autoDeviceSwitchStatusChange', callback);
337}
338```
339
340## off('autoDeviceSwitchStatusChange')<sup>13+</sup>
341
342off(type: 'autoDeviceSwitchStatusChange', callback?: AsyncCallback\<AutoDeviceSwitchStatus\>): void
343
344Unsubscribes from automatic camera switch status change events.
345
346**Atomic service API**: This API can be used in atomic services since API version 19.
347
348**System capability**: SystemCapability.Multimedia.Camera.Core
349
350**Parameters**
351
352| Name    | Type                                          | Mandatory| Description                      |
353| -------- |----------------------------------------------| ---- | ------------------------ |
354| type     | string                                       | Yes  | Event type. The value is fixed at **'autoDeviceSwitchStatusChange'**. The event can be listened for when a session is created.|
355| callback | AsyncCallback\<[AutoDeviceSwitchStatus](arkts-apis-camera-i.md#autodeviceswitchstatus13)\> | No  | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.|
356
357**Example**
358
359```ts
360function unregisterSmoothZoomInfo(videoSession: camera.VideoSession): void {
361  videoSession.off('autoDeviceSwitchStatusChange');
362}
363```
364
365## setQualityPrioritization<sup>14+</sup>
366
367setQualityPrioritization(quality : QualityPrioritization) : void;
368
369Sets the priority level for video recording quality.
370
371> **NOTE**
372> The default value is **HIGH_QUALITY**. Switching to **POWER_BALANCE** will compromise video recording quality to achieve lower power usage. The extent of power conservation achieved varies depending on the platform.
373
374**Atomic service API**: This API can be used in atomic services since API version 19.
375
376**System capability**: SystemCapability.Multimedia.Camera.Core
377
378**Parameters**
379
380| Name | Type                                             | Mandatory| Description                                      |
381| ------- | ------------------------------------------------- | ---- | ------------------------------------------ |
382| quality | [QualityPrioritization](arkts-apis-camera-e.md#qualityprioritization14) | Yes  | Priority level to set. The default value is **HIGH_QUALITY**.|
383
384**Error codes**
385
386For details about the error codes, see [Camera Error Codes](errorcode-camera.md).
387
388| ID| Error Message                                                                                                                                       |
389| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
390| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
391| 7400103  | Session not config. The session has not been committed or configured.                                                                           |
392
393**Example**
394
395```ts
396import { BusinessError } from '@kit.BasicServicesKit';
397
398function setQualityPrioritization(videoSession: camera.VideoSession): void {
399  try {
400    videoSession.setQualityPrioritization(camera.QualityPrioritization.POWER_BALANCE);
401  } catch (error) {
402    // If the operation fails, error.code is returned and processed.
403    let err = error as BusinessError;
404    console.error(`The setQualityPrioritization call failed. error code: ${err.code}`);
405  }
406}
407```
408
409## on('systemPressureLevelChange')<sup>20+</sup>
410
411on(type: 'systemPressureLevelChange', callback: AsyncCallback\<SystemPressureLevel\>): void
412
413Subscribes to system pressure level change events. This API uses an asynchronous callback to return the result.
414
415> **NOTE**
416>
417> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**.
418
419**Atomic service API**: This API can be used in atomic services since API version 20.
420
421**System capability**: SystemCapability.Multimedia.Camera.Core
422
423**Parameters**
424
425| Name | Type                                                     | Mandatory| Description                                                             |
426|---------|----------------------------------------------------------|------|-------------------------------------------------------------------|
427|type     | string                                                   | Yes  | Event type. The value is fixed at **'systemPressureLevelChange'**. The event can be listened for when a session is created.|
428|callback | AsyncCallback\<[SystemPressureLevel](arkts-apis-camera-e.md#systempressurelevel20)\> | Yes| Callback used to return the current system pressure level.|
429
430**Example**
431
432```ts
433import { BusinessError } from '@kit.BasicServicesKit';
434
435function callback(err: BusinessError, systemPressureLevel: camera.SystemPressureLevel): void {
436  if (err !== undefined && err.code !== 0) {
437    console.error(`Callback Error, errorCode: ${err.code}`);
438    return;
439  }
440  console.info(`systemPressureLevel: ${systemPressureLevel}`);
441}
442
443function registerSystemPressureLevelChangeCallback(videoSession: camera.VideoSession): void {
444    videoSession.on('systemPressureLevelChange', callback);
445}
446```
447
448## off('systemPressureLevelChange')<sup>20+</sup>
449
450off(type: 'systemPressureLevelChange', callback?: AsyncCallback\<SystemPressureLevel\>): void
451
452Unsubscribes from system pressure level change events.
453
454**Atomic service API**: This API can be used in atomic services since API version 20.
455
456**System capability**: SystemCapability.Multimedia.Camera.Core
457
458**Parameters**
459
460| Name   | Type                          | Mandatory   | Description                                                            |
461|----------|--------------------------------|---------|------------------------------------------------------------------|
462| type     | string                         | Yes     | Event type. The value is fixed at **'systemPressureLevelChange'**. The event can be listened for when a session is created.|
463| callback | AsyncCallback\<[SystemPressureLevel](arkts-apis-camera-e.md#systempressurelevel20)\> | No| Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.|
464
465**Example**
466
467```ts
468function unregisterSystemPressureLevelChangeCallback(videoSession: camera.VideoSession): void {
469  videoSession.off('systemPressureLevelChange');
470}
471```
472
473## on('controlCenterEffectStatusChange')<sup>20+</sup>
474
475on(type: 'controlCenterEffectStatusChange', callback: AsyncCallback\<ControlCenterStatusInfo\>): void
476
477Subscribes to events indicating that the camera controller effect status changes. This API uses an asynchronous callback to return the result.
478
479> **NOTE**
480>
481> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**.
482
483**Atomic service API**: This API can be used in atomic services since API version 20.
484
485**System capability**: SystemCapability.Multimedia.Camera.Core
486
487**Parameters**
488
489| Name   | Type| Mandatory| Description|
490|----------|-------|----|------------------------------------------------------------------------|
491| type     | string | Yes| Event type. The value is fixed at **'controlCenterEffectStatusChange'**. The event can be listened for when a session is created.|
492| callback | AsyncCallback\<[ControlCenterStatusInfo](arkts-apis-camera-i.md#controlcenterstatusinfo20)\> | Yes| Callback used to return the effect status of the current controller.|
493
494**Example**
495```ts
496import { BusinessError } from '@kit.BasicServicesKit';
497
498function callback(err: BusinessError, status: camera.ControlCenterStatusInfo): void {
499  if (err !== undefined && err.code !== 0) {
500    console.error(`Callback Error, errorCode: ${err.code}`);
501    return;
502  }
503  console.info(`controlCenterEffectStatusChange: ${status}`);
504}
505
506function registerControlCenterEffectStatusChangeCallback(videoSession: camera.VideoSession): void {
507  videoSession.on('controlCenterEffectStatusChange', callback);
508}
509```
510
511## off('controlCenterEffectStatusChange')<sup>20+</sup>
512
513off(type: 'controlCenterEffectStatusChange', callback?: AsyncCallback\<ControlCenterStatusInfo\>): void
514
515Unsubscribes from events indicating that the camera controller effect status changes.
516
517**Atomic service API**: This API can be used in atomic services since API version 20.
518
519**System capability**: SystemCapability.Multimedia.Camera.Core
520
521**Parameters**
522
523| Name   | Type| Mandatory| Description|
524|----------|-------|----|------------------------------------------------------------------------|
525| type     | string | Yes| Event type. The value is fixed at **'controlCenterEffectStatusChange'**. The event can be listened for when a session is created.|
526| callback | AsyncCallback\<[ControlCenterStatusInfo](arkts-apis-camera-i.md#controlcenterstatusinfo20)\> | No| Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.|
527
528**Example**
529
530```ts
531function unregisterControlCenterEffectStatusChange(videoSession: camera.VideoSession): void {
532  videoSession.off('controlCenterEffectStatusChange');
533}
534```
535