1# @ohos.multimedia.media (Media) 2 3> **NOTE** 4> 5> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. 6 7The multimedia subsystem provides a set of simple and easy-to-use APIs for you to access the system and use media resources. 8 9This subsystem offers the following audio and video services: 10 11- Audio and video playback, implemented by the [AVPlayer](#avplayer9)<sup>9+</sup> class. This class has integrated [AudioPlayer](#audioplayerdeprecated)<sup>6+</sup> and [VideoPlayer](#videoplayer)<sup>8+</sup>, with the state machine and error codes upgraded. It is recommended. 12- Audio and video recording, implemented by the [AVRecorder](#avrecorder9)<sup>9+</sup> class. This class has integrated [AudioRecorder](#audiorecorderdeprecated)<sup>6+</sup> and [VideoRecorder](#videorecorder9)<sup>9+</sup>. It is recommended. 13- Audio playback, implemented by the [AudioPlayer](#audioplayerdeprecated)<sup>6+</sup> class. It is deprecated. You are advised to use [AVPlayer](#avplayer9)<sup>9+</sup>. 14- Video playback, implemented by the [VideoPlayer](#videoplayerdeprecated)<sup>8+</sup> class. It is deprecated. You are advised to use [AVPlayer](#avplayer9)<sup>9+</sup>. 15- Audio recording, implemented by the [AudioRecorder](#audiorecorderdeprecated)<sup>6+</sup> class. It is deprecated. You are advised to use [AVRecorder](#avrecorder9)<sup>9+</sup>. 16- Video recording, implemented by the [VideoRecorder](#videorecorder9)<sup>9+</sup> class. It is deprecated. You are advised to use [AVRecorder](#avrecorder9)<sup>9+</sup>. 17 18## Modules to Import 19 20```js 21import media from '@ohos.multimedia.media'; 22``` 23 24## media.createAVPlayer<sup>9+</sup> 25 26createAVPlayer(callback: AsyncCallback\<AVPlayer>): void 27 28Creates an **AVPlayer** instance. This API uses an asynchronous callback to return the result. 29 30**System capability**: SystemCapability.Multimedia.Media.AVPlayer 31 32**Parameters** 33 34| Name | Type | Mandatory| Description | 35| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 36| callback | AsyncCallback\<[AVPlayer](#avplayer9)> | Yes | Callback used to return the result. If the operation is successful, an **AVPlayer** instance is returned; otherwise, **null** is returned. The instance can be used to play audio and video.| 37 38**Error codes** 39 40For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 41 42| ID| Error Message | 43| -------- | ------------------------------ | 44| 5400101 | No memory. Return by callback. | 45 46**Example** 47 48```js 49let avPlayer 50 51media.createAVPlayer((error, video) => { 52 if (video != null) { 53 avPlayer = video; 54 console.info('createAVPlayer success'); 55 } else { 56 console.info(`createAVPlayer fail, error:${error}`); 57 } 58}); 59``` 60 61## media.createAVPlayer<sup>9+</sup> 62 63createAVPlayer(): Promise\<AVPlayer> 64 65Creates an **AVPlayer** instance. This API uses a promise to return the result. 66 67**System capability**: SystemCapability.Multimedia.Media.AVPlayer 68 69**Return value** 70 71| Type | Description | 72| ------------------------------- | ------------------------------------------------------------ | 73| Promise\<[AVPlayer](#avplayer9)> | Promise used to return the result. If the operation is successful, an **AVPlayer** instance is returned; otherwise, **null** is returned. The instance can be used to play audio and video.| 74 75**Error codes** 76 77For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 78 79| ID| Error Message | 80| -------- | ----------------------------- | 81| 5400101 | No memory. Return by promise. | 82 83**Example** 84 85```js 86let avPlayer 87 88media.createAVPlayer().then((video) => { 89 if (video != null) { 90 avPlayer = video; 91 console.info('createAVPlayer success'); 92 } else { 93 console.info('createAVPlayer fail'); 94 } 95}).catch((error) => { 96 console.info(`AVPlayer catchCallback, error:${error}`); 97}); 98``` 99 100## media.createAVRecorder<sup>9+</sup> 101 102createAVRecorder(callback: AsyncCallback\<AVRecorder>): void 103 104Creates an **AVRecorder** instance. This API uses an asynchronous callback to return the result. 105Only one **AVRecorder** instance can be created per device. 106 107To use the camera to record videos, the camera module is required. For details about how to use the APIs provided by the camera module, see [Camera Management](js-apis-camera.md). 108 109**System capability**: SystemCapability.Multimedia.Media.AVRecorder 110 111**Parameters** 112 113| Name | Type | Mandatory| Description | 114| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 115| callback | AsyncCallback\<[AVRecorder](#avrecorder9)> | Yes | Callback used to return the result. If the operation is successful, an **AVRecorder** instance is returned; otherwise, **null** is returned. The instance can be used to record audio and video.| 116 117**Error codes** 118 119For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 120 121| ID| Error Message | 122| -------- | ------------------------------ | 123| 5400101 | No memory. Return by callback. | 124 125**Example** 126 127```js 128let avRecorder 129 130media.createAVRecorder((error, recorder) => { 131 if (recorder != null) { 132 avRecorder = recorder; 133 console.info('createAVRecorder success'); 134 } else { 135 console.info(`createAVRecorder fail, error:${error}`); 136 } 137}); 138``` 139 140## media.createAVRecorder<sup>9+</sup> 141 142createAVRecorder(): Promise\<AVRecorder> 143 144Creates an **AVRecorder** instance. This API uses a promise to return the result. 145Only one **AVRecorder** instance can be created per device. 146 147To use the camera to record videos, the camera module is required. For details about how to use the APIs provided by the camera module, see [Camera Management](js-apis-camera.md). 148 149**System capability**: SystemCapability.Multimedia.Media.AVRecorder 150 151**Return value** 152 153| Type | Description | 154| ------------------------------------ | ------------------------------------------------------------ | 155| Promise\<[AVRecorder](#avrecorder9)> | Promise used to return the result. If the operation is successful, an **AVRecorder** instance is returned; otherwise, **null** is returned. The instance can be used to record audio and video.| 156 157**Error codes** 158 159For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 160 161| ID| Error Message | 162| -------- | ----------------------------- | 163| 5400101 | No memory. Return by promise. | 164 165**Example** 166 167```js 168let avRecorder 169 170media.createAVRecorder().then((recorder) => { 171 if (recorder != null) { 172 avRecorder = recorder; 173 console.info('createAVRecorder success'); 174 } else { 175 console.info('createAVRecorder fail'); 176 } 177}).catch((error) => { 178 console.info(`createAVRecorder catchCallback, error:${error}`); 179}); 180``` 181 182## media.createVideoRecorder<sup>9+</sup> 183 184createVideoRecorder(callback: AsyncCallback\<VideoRecorder>): void 185 186Creates a **VideoRecorder** instance. This API uses an asynchronous callback to return the result. 187Only one **VideoRecorder** instance can be created per device. 188 189**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 190 191**System API**: This is a system API. 192 193**Parameters** 194 195| Name | Type | Mandatory| Description | 196| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 197| callback | AsyncCallback<[VideoRecorder](#videorecorder9)> | Yes | Callback used to return the result. If the operation is successful, a **VideoRecorder** instance is returned; otherwise, **null** is returned. The instance can be used to record video.| 198 199**Error codes** 200 201For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 202 203| ID| Error Message | 204| -------- | ------------------------------ | 205| 5400101 | No memory. Return by callback. | 206 207**Example** 208 209```js 210let videoRecorder 211 212media.createVideoRecorder((error, video) => { 213 if (video != null) { 214 videoRecorder = video; 215 console.info('video createVideoRecorder success'); 216 } else { 217 console.info(`video createVideoRecorder fail, error:${error}`); 218 } 219}); 220``` 221 222## media.createVideoRecorder<sup>9+</sup> 223 224createVideoRecorder(): Promise\<VideoRecorder> 225 226Creates a **VideoRecorder** instance. This API uses a promise to return the result. 227Only one **VideoRecorder** instance can be created per device. 228 229**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 230 231**System API**: This is a system API. 232 233**Return value** 234 235| Type | Description | 236| ----------------------------------------- | ------------------------------------------------------------ | 237| Promise<[VideoRecorder](#videorecorder9)> | Promise used to return the result. If the operation is successful, a **VideoRecorder** instance is returned; otherwise, **null** is returned. The instance can be used to record video.| 238 239**Error codes** 240 241For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 242 243| ID| Error Message | 244| -------- | ----------------------------- | 245| 5400101 | No memory. Return by promise. | 246 247**Example** 248 249```js 250let videoRecorder 251 252media.createVideoRecorder().then((video) => { 253 if (video != null) { 254 videoRecorder = video; 255 console.info('video createVideoRecorder success'); 256 } else { 257 console.info('video createVideoRecorder fail'); 258 } 259}).catch((error) => { 260 console.info(`video catchCallback, error:${error}`); 261}); 262``` 263 264## AVErrorCode<sup>9+</sup><a name=averrorcode></a> 265 266Enumerates the [media error codes](../errorcodes/errorcode-media.md). 267 268**System capability**: SystemCapability.Multimedia.Media.Core 269 270| Name | Value | Description | 271| :------------------------- | ------- | ------------------------------------ | 272| AVERR_OK | 0 | The operation is successful. | 273| AVERR_NO_PERMISSION | 201 | You do not have the permission to perform the operation. | 274| AVERR_INVALID_PARAMETER | 401 | Invalid input parameter. | 275| AVERR_UNSUPPORT_CAPABILITY | 801 | Unsupported API. | 276| AVERR_NO_MEMORY | 5400101 | The system memory is insufficient or the number of services reaches the upper limit.| 277| AVERR_OPERATE_NOT_PERMIT | 5400102 | The operation is not allowed in the current state or you do not have the permission to perform the operation.| 278| AVERR_IO | 5400103 | The data stream is abnormal. | 279| AVERR_TIMEOUT | 5400104 | The system or network response times out. | 280| AVERR_SERVICE_DIED | 5400105 | The service process is dead. | 281| AVERR_UNSUPPORT_FORMAT | 5400106 | The format of the media asset is not supported. | 282 283## MediaType<sup>8+</sup> 284 285Enumerates the media types. 286 287**System capability**: SystemCapability.Multimedia.Media.Core 288 289| Name | Value | Description | 290| -------------- | ---- | ---------- | 291| MEDIA_TYPE_AUD | 0 | Media.| 292| MEDIA_TYPE_VID | 1 | Video.| 293 294## CodecMimeType<sup>8+</sup> 295 296Enumerates the codec MIME types. 297 298**System capability**: SystemCapability.Multimedia.Media.Core 299 300| Name | Value | Description | 301| ------------ | --------------------- | ------------------------ | 302| VIDEO_H263 | 'video/h263' | Video in H.263 format. | 303| VIDEO_AVC | 'video/avc' | Video in AVC format. | 304| VIDEO_MPEG2 | 'video/mpeg2' | Video in MPEG-2 format. | 305| VIDEO_MPEG4 | 'video/mp4v-es' | Video in MPEG-4 format. | 306| VIDEO_VP8 | 'video/x-vnd.on2.vp8' | Video in VP8 format. | 307| AUDIO_AAC | 'audio/mp4a-latm' | Audio in MP4A-LATM format.| 308| AUDIO_VORBIS | 'audio/vorbis' | Audio in Vorbis format. | 309| AUDIO_FLAC | 'audio/flac' | Audio in FLAC format. | 310 311## MediaDescriptionKey<sup>8+</sup> 312 313Enumerates the media description keys. 314 315**System capability**: SystemCapability.Multimedia.Media.Core 316 317| Name | Value | Description | 318| ------------------------ | --------------- | ------------------------------------------------------------ | 319| MD_KEY_TRACK_INDEX | 'track_index' | Track index, which is a number. | 320| MD_KEY_TRACK_TYPE | 'track_type' | Track type, which is a number. For details, see [MediaType](#mediatype8).| 321| MD_KEY_CODEC_MIME | 'codec_mime' | Codec MIME type, which is a string. | 322| MD_KEY_DURATION | 'duration' | Media duration, which is a number, in units of ms. | 323| MD_KEY_BITRATE | 'bitrate' | Bit rate, which is a number, in units of bit/s. | 324| MD_KEY_WIDTH | 'width' | Video width, which is a number, in units of pixel. | 325| MD_KEY_HEIGHT | 'height' | Video height, which is a number, in units of pixel. | 326| MD_KEY_FRAME_RATE | 'frame_rate' | Video frame rate, which is a number, in units of 100 fps.| 327| MD_KEY_AUD_CHANNEL_COUNT | 'channel_count' | Number of audio channels, which is a number. | 328| MD_KEY_AUD_SAMPLE_RATE | 'sample_rate' | Sampling rate, which is a number, in units of Hz. | 329 330## BufferingInfoType<sup>8+</sup> 331 332Enumerates the buffering event types. 333 334**System capability**: SystemCapability.Multimedia.Media.Core 335 336| Name | Value | Description | 337| ----------------- | ---- | -------------------------------- | 338| BUFFERING_START | 1 | Buffering starts. | 339| BUFFERING_END | 2 | Buffering ends. | 340| BUFFERING_PERCENT | 3 | Buffering progress, in percent. | 341| CACHED_DURATION | 4 | Cache duration, in ms.| 342 343## StateChangeReason<sup>9+</sup> 344 345Enumerates the reasons for the state transition of the **AVPlayer** or **AVRecorder** instance. The enum value is reported together with **state**. 346 347**System capability**: SystemCapability.Multimedia.Media.Core 348 349| Name | Value | Description | 350| ---------- | ---- | ------------------------------------------------------------ | 351| USER | 1 | State transition triggered by user behavior. It happens when a user or the client calls an API.| 352| BACKGROUND | 2 | State transition caused by system behavior. For example, if an application does not have the permission of Media Controller, the application is forcibly suspended or stopped by the system when it switches to the background.| 353 354## AVPlayer<sup>9+</sup> 355 356A playback management class that provides APIs to manage and play media assets. Before calling any API in **AVPlayer**, you must use [createAVPlayer()](#mediacreateavplayer9) to create an **AVPlayer** instance. 357 358For details about the audio and video playback demo, see [Audio Playback](../../media/using-avplayer-for-playback.md) and [Video Playback](../../media/video-playback.md). 359 360### Attributes<a name=avplayer_attributes></a> 361 362**System capability**: SystemCapability.Multimedia.Media.AVPlayer 363 364| Name | Type | Readable| Writable| Description | 365| --------------------------------------------------- | ------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 366| url<sup>9+</sup> | string | Yes | Yes | URL of the media asset. It is a static attribute and can be set only when the AVPlayer is in the idle state. <br>The video formats MP4, MPEG-TS, WebM, and MKV are supported.<br>The audio formats M4A, AAC, MP3, OGG, and WAV are supported.<br>**Examples of supported URLs**:<br>1. FD: fd://xx<br>![](figures/en-us_image_url.png)<br>2. HTTP: http://xx<br>3. HTTPS: https://xx<br>4. HLS: http://xx or https://xx| 367| fdSrc<sup>9+</sup> | [AVFileDescriptor](#avfiledescriptor9) | Yes | Yes | FD of the media asset. It is a static attribute and can be set only when the AVPlayer is in the idle state.<br>This attribute is required when media assets of an application are continuously stored in a file.<br>**Example:**<br>Assume that a media file that stores continuous assets consists of the following:<br>Video 1 (address offset: 0, byte length: 100)<br>Video 2 (address offset: 101; byte length: 50)<br>Video 3 (address offset: 151, byte length: 150)<br>1. To play video 1: AVFileDescriptor {fd = resource handle; offset = 0; length = 100; }<br>2. To play video 2: AVFileDescriptor {fd = resource handle; offset = 101; length = 50; }<br>3. To play video 3: AVFileDescriptor {fd = resource handle; offset = 151; length = 150; }<br>To play an independent media file, use **src=fd://xx**.| 368| surfaceId<sup>9+</sup> | string | Yes | Yes | Video window ID. By default, there is no video window. It is a static attribute and can be set only when the AVPlayer is in the initialized state.<br>It is used to render the window for video playback and therefore is not required in audio-only playback scenarios.<br>**Example:**<br>[Create a surface ID through XComponent](../arkui-ts/ts-basic-components-xcomponent.md#getxcomponentsurfaceid).| 369| loop<sup>9+</sup> | boolean | Yes | Yes | Whether to loop playback. The value **true** means to loop playback, and **false** (default) means the opposite. It is a dynamic attribute<br>and can be set only when the AVPlayer is in the prepared, playing, paused, or completed state.| 370| videoScaleType<sup>9+</sup> | [VideoScaleType](#videoscaletype9) | Yes | Yes | Video scaling type. The default value is **VIDEO_SCALE_TYPE_FIT_CROP**. It is a dynamic attribute<br>and can be set only when the AVPlayer is in the prepared, playing, paused, or completed state.| 371| audioInterruptMode<sup>9+</sup> | [audio.InterruptMode](js-apis-audio.md#interruptmode9) | Yes | Yes | Audio interruption mode. The default value is **SHARE_MODE**. It is a dynamic attribute<br>and can be set only when the AVPlayer is in the prepared, playing, paused, or completed state.| 372| state<sup>9+</sup> | [AVPlayerState](#avplayerstate9) | Yes | No | AVPlayer state. It can be used as a query parameter when the AVPlayer is in any state. | 373| currentTime<sup>9+</sup> | number | Yes | No | Current video playback position, in ms. It can be used as a query parameter when the AVPlayer is in the prepared, playing, paused, or completed state.<br>The value **-1** indicates an invalid value.| 374| duration<sup>9+</sup><a name=avplayer_duration></a> | number | Yes | No | Video duration, in ms. It can be used as a query parameter when the AVPlayer is in the prepared, playing, paused, or completed state.<br>The value **-1** indicates an invalid value.<br>In live streaming scenarios, **-1** is returned by default.| 375| width<sup>9+</sup> | number | Yes | No | Video width, in pixels. It can be used as a query parameter when the AVPlayer is in the prepared, playing, paused, or completed state.<br>The value **0** indicates an invalid value.| 376| height<sup>9+</sup> | number | Yes | No | Video height, in pixels. It can be used as a query parameter when the AVPlayer is in the prepared, playing, paused, or completed state.<br>The value **0** indicates an invalid value.| 377 378**NOTE** 379 380After the resource handle (FD) is transferred to the AVPlayer, do not use the resource handle to perform read and write operations, including but not limited to transferring it to multiple AVPlayers. Competition occurs when multiple AVPlayers use the same resource handle to read and write files at the same time, resulting in playback errors. 381 382### on('stateChange')<sup>9+</sup><a name = stateChange_on></a> 383 384on(type: 'stateChange', callback: (state: AVPlayerState, reason: StateChangeReason) => void): void 385 386Subscribes to AVPlayer state changes. 387 388**System capability**: SystemCapability.Multimedia.Media.AVPlayer 389 390**Parameters** 391 392| Name | Type | Mandatory| Description | 393| -------- | -------- | ---- | ------------------------------------------------------------ | 394| type | string | Yes | Event type, which is **'stateChange'** in this case. This event can be triggered by both user operations and the system.| 395| callback | function | Yes | Callback invoked when the event is triggered. It reports the following information:<br>state: [AVPlayerState](#avplayerstate9), indicating the AVPlayer state.<br>reason: [StateChangeReason](#statechangereason9), indicating the reason for the state transition.| 396 397**Example** 398 399```js 400avPlayer.on('stateChange', async (state, reason) => { 401 switch (state) { 402 case 'idle': 403 console.info('state idle called') 404 break; 405 case 'initialized': 406 console.info('initialized prepared called') 407 break; 408 case 'prepared': 409 console.info('state prepared called') 410 break; 411 case 'playing': 412 console.info('state playing called') 413 break; 414 case 'paused': 415 console.info('state paused called') 416 break; 417 case 'completed': 418 console.info('state completed called') 419 break; 420 case 'stopped': 421 console.info('state stopped called') 422 break; 423 case 'released': 424 console.info('state released called') 425 break; 426 case 'error': 427 console.info('state error called') 428 break; 429 default: 430 console.info('unkown state :' + state) 431 break; 432 } 433}) 434``` 435 436### off('stateChange')<sup>9+</sup><a name = stateChange_off></a> 437 438off(type: 'stateChange'): void 439 440Unsubscribes from AVPlayer state changes. 441 442**System capability**: SystemCapability.Multimedia.Media.AVPlayer 443 444**Parameters** 445 446| Name| Type | Mandatory| Description | 447| ------ | ------ | ---- | ----------------------------------------------------- | 448| type | string | Yes | Event type, which is **'stateChange'** in this case.| 449 450**Example** 451 452```js 453avPlayer.off('stateChange') 454``` 455 456### on('error')<sup>9+</sup><a name = error_on></a> 457 458on(type: 'error', callback: ErrorCallback): void 459 460Subscribes to AVPlayer errors. This event is used only for error prompt and does not require the user to stop playback control. If the [AVPlayer state](#avplayerstate9) is also switched to error, call **reset()** or **release()** to exit the playback. 461 462**System capability**: SystemCapability.Multimedia.Media.AVPlayer 463 464**Parameters** 465 466| Name | Type | Mandatory| Description | 467| -------- | -------- | ---- | ------------------------------------------------------------ | 468| type | string | Yes | Event type, which is **'error'** in this case. This event can be triggered by both user operations and the system.| 469| callback | function | Yes | Callback used to return the error code ID and error message.| 470 471The AVPlayer provides the following error types<a name = error_info></a>: 472 473| ID| Error Message | Description | 474| -------- | --------------------- | ------------------------------------------------------------ | 475| 201 | No Permission: | No permission to perform the operation. The [AVPlayer state](#avplayerstate9) is error.| 476| 401 | Invalid Parameter: | Incorrect input parameter, causing an invalid call. | 477| 801 | Unsupport Capability: | Unsupported API, causing an invalid call. | 478| 5400101 | No Memory: | Insufficient memory. The [AVPlayer state](#avplayerstate9) is error.| 479| 5400102 | Operate Not Permit: | Unsupported operation in the current state, causing an invalid call. | 480| 5400103 | IO Error: | A stream exception is detected during playback. The [AVPlayer state](#avplayerstate9) is error.| 481| 5400104 | Network Timeout: | The response times out due to a network error. The [AVPlayer state](#avplayerstate9) is error.| 482| 5400105 | Service Died: | The playback process is dead. The [AVPlayer state](#avplayerstate9) is error. In this case, you need to release the instance and then create an instance again.| 483| 5400106 | Unsupport Format: | Unsupported file format. The [AVPlayer state](#avplayerstate9) is error.| 484 485**Example** 486 487```js 488avPlayer.on('error', (error) => { 489 console.info('error happened,and error message is :' + error.message) 490 console.info('error happened,and error code is :' + error.code) 491}) 492``` 493 494### off('error')<sup>9+</sup><a name = error_off></a> 495 496off(type: 'error'): void 497 498Unsubscribes from AVPlayer errors. 499 500**System capability**: SystemCapability.Multimedia.Media.AVPlayer 501 502**Parameters** 503 504| Name| Type | Mandatory| Description | 505| ------ | ------ | ---- | ----------------------------------------- | 506| type | string | Yes | Event type, which is **'error'** in this case.| 507 508**Example** 509 510```js 511avPlayer.off('error') 512``` 513 514### prepare<sup>9+</sup><a name=avplayer_prepare></a> 515 516prepare(callback: AsyncCallback\<void>): void 517 518Prepares for audio and video playback. This API uses an asynchronous callback to return the result. It can be called only when the AVPlayer is in the initialized state. 519 520**System capability**: SystemCapability.Multimedia.Media.AVPlayer 521 522**Parameters** 523 524| Name | Type | Mandatory| Description | 525| -------- | -------- | ---- | -------------------- | 526| callback | function | Yes | Callback used to return the result.| 527 528**Error codes** 529 530For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 531 532| ID| Error Message | 533| -------- | ------------------------------------------ | 534| 5400102 | Operation not allowed. Return by callback. | 535| 5400106 | Unsupport format. Return by callback. | 536 537**Example** 538 539```js 540avPlayer.prepare((err) => { 541 if (err == null) { 542 console.info('prepare success'); 543 } else { 544 console.error('prepare filed,error message is :' + err.message) 545 } 546}) 547``` 548 549### prepare<sup>9+</sup> 550 551prepare(): Promise\<void> 552 553Prepares for audio and video playback. This API uses a promise to return the result. It can be called only when the AVPlayer is in the initialized state. 554 555**System capability**: SystemCapability.Multimedia.Media.AVPlayer 556 557**Return value** 558 559| Type | Description | 560| -------------- | ------------------------- | 561| Promise\<void> | Promise used to return the result.| 562 563**Error codes** 564 565For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 566 567| ID| Error Message | 568| -------- | ----------------------------------------- | 569| 5400102 | Operation not allowed. Return by promise. | 570| 5400106 | Unsupport format. Return by promise. | 571 572**Example** 573 574```js 575avPlayer.prepare().then(() => { 576 console.info('prepare success'); 577}, (err) => { 578 console.error('prepare filed,error message is :' + err.message) 579}) 580``` 581 582### play<sup>9+</sup><a name=avplayer_play></a> 583 584play(callback: AsyncCallback\<void>): void 585 586Starts to play an audio and video asset. This API uses an asynchronous callback to return the result. It can be called only when the AVPlayer is in the prepared, paused, or completed state. 587 588**System capability**: SystemCapability.Multimedia.Media.AVPlayer 589 590**Parameters** 591 592| Name | Type | Mandatory| Description | 593| -------- | -------- | ---- | -------------------- | 594| callback | function | Yes | Callback used to return the result.| 595 596**Error codes** 597 598For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 599 600| ID| Error Message | 601| -------- | ------------------------------------------ | 602| 5400102 | Operation not allowed. Return by callback. | 603 604**Example** 605 606```js 607avPlayer.play((err) => { 608 if (err == null) { 609 console.info('play success'); 610 } else { 611 console.error('play filed,error message is :' + err.message) 612 } 613}) 614``` 615 616### play<sup>9+</sup> 617 618play(): Promise\<void> 619 620Starts to play an audio and video asset. This API uses a promise to return the result. It can be called only when the AVPlayer is in the prepared, paused, or completed state. 621 622**System capability**: SystemCapability.Multimedia.Media.AVPlayer 623 624**Return value** 625 626| Type | Description | 627| -------------- | ------------------------- | 628| Promise\<void> | Promise used to return the result.| 629 630**Error codes** 631 632For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 633 634| ID| Error Message | 635| -------- | ----------------------------------------- | 636| 5400102 | Operation not allowed. Return by promise. | 637 638**Example** 639 640```js 641avPlayer.play().then(() => { 642 console.info('play success'); 643}, (err) => { 644 console.error('play filed,error message is :' + err.message) 645}) 646``` 647 648### pause<sup>9+</sup><a name=avplayer_pause></a> 649 650pause(callback: AsyncCallback\<void>): void 651 652Pauses audio and video playback. This API uses an asynchronous callback to return the result. It can be called only when the AVPlayer is in the playing state. 653 654**System capability**: SystemCapability.Multimedia.Media.AVPlayer 655 656**Parameters** 657 658| Name | Type | Mandatory| Description | 659| -------- | -------- | ---- | -------------------- | 660| callback | function | Yes | Callback used to return the result.| 661 662**Error codes** 663 664For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 665 666| ID| Error Message | 667| -------- | ------------------------------------------ | 668| 5400102 | Operation not allowed. Return by callback. | 669 670**Example** 671 672```js 673avPlayer.pause((err) => { 674 if (err == null) { 675 console.info('pause success'); 676 } else { 677 console.error('pause filed,error message is :' + err.message) 678 } 679}) 680``` 681 682### pause<sup>9+</sup> 683 684pause(): Promise\<void> 685 686Pauses audio and video playback. This API uses a promise to return the result. It can be called only when the AVPlayer is in the playing state. 687 688**System capability**: SystemCapability.Multimedia.Media.AVPlayer 689 690**Return value** 691 692| Type | Description | 693| -------------- | ------------------------- | 694| Promise\<void> | Promise used to return the result.| 695 696**Error codes** 697 698For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 699 700| ID| Error Message | 701| -------- | ----------------------------------------- | 702| 5400102 | Operation not allowed. Return by promise. | 703 704**Example** 705 706```js 707avPlayer.pause().then(() => { 708 console.info('pause success'); 709}, (err) => { 710 console.error('pause filed,error message is :' + err.message) 711}) 712``` 713 714### stop<sup>9+</sup><a name=avplayer_stop></a> 715 716stop(callback: AsyncCallback\<void>): void 717 718Stops audio and video playback. This API uses an asynchronous callback to return the result. It can be called only when the AVPlayer is in the prepared, playing, paused, or completed state. 719 720**System capability**: SystemCapability.Multimedia.Media.AVPlayer 721 722**Parameters** 723 724| Name | Type | Mandatory| Description | 725| -------- | -------- | ---- | -------------------- | 726| callback | function | Yes | Callback used to return the result.| 727 728**Error codes** 729 730For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 731 732| ID| Error Message | 733| -------- | ------------------------------------------ | 734| 5400102 | Operation not allowed. Return by callback. | 735 736**Example** 737 738```js 739avPlayer.stop((err) => { 740 if (err == null) { 741 console.info('stop success'); 742 } else { 743 console.error('stop filed,error message is :' + err.message) 744 } 745}) 746``` 747 748### stop<sup>9+</sup> 749 750stop(): Promise\<void> 751 752Stops audio and video playback. This API uses a promise to return the result. It can be called only when the AVPlayer is in the prepared, playing, paused, or completed state. 753 754**System capability**: SystemCapability.Multimedia.Media.AVPlayer 755 756**Return value** 757 758| Type | Description | 759| -------------- | ------------------------- | 760| Promise\<void> | Promise used to return the result.| 761 762**Error codes** 763 764For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 765 766| ID| Error Message | 767| -------- | ----------------------------------------- | 768| 5400102 | Operation not allowed. Return by promise. | 769 770**Example** 771 772```js 773avPlayer.stop().then(() => { 774 console.info('stop success'); 775}, (err) => { 776 console.error('stop filed,error message is :' + err.message) 777}) 778``` 779 780### reset<sup>9+</sup><a name=avplayer_reset></a> 781 782reset(callback: AsyncCallback\<void>): void 783 784Resets audio and video playback. This API uses an asynchronous callback to return the result. It can be called only when the AVPlayer is in the initialized, prepared, playing, paused, completed, stopped, or error state. 785 786**System capability**: SystemCapability.Multimedia.Media.AVPlayer 787 788**Parameters** 789 790| Name | Type | Mandatory| Description | 791| -------- | -------- | ---- | -------------------- | 792| callback | function | Yes | Callback used to return the result.| 793 794**Error codes** 795 796For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 797 798| ID| Error Message | 799| -------- | ------------------------------------------ | 800| 5400102 | Operation not allowed. Return by callback. | 801 802**Example** 803 804```js 805avPlayer.reset((err) => { 806 if (err == null) { 807 console.info('reset success'); 808 } else { 809 console.error('reset filed,error message is :' + err.message) 810 } 811}) 812``` 813 814### reset<sup>9+</sup> 815 816reset(): Promise\<void> 817 818Resets audio and video playback. This API uses a promise to return the result. It can be called only when the AVPlayer is in the initialized, prepared, playing, paused, completed, stopped, or error state. 819 820**System capability**: SystemCapability.Multimedia.Media.AVPlayer 821 822**Return value** 823 824| Type | Description | 825| -------------- | ------------------------- | 826| Promise\<void> | Promise used to return the result.| 827 828**Error codes** 829 830For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 831 832| ID| Error Message | 833| -------- | ----------------------------------------- | 834| 5400102 | Operation not allowed. Return by promise. | 835 836**Example** 837 838```js 839avPlayer.reset().then(() => { 840 console.info('reset success'); 841}, (err) => { 842 console.error('reset filed,error message is :' + err.message) 843}) 844``` 845 846### release<sup>9+</sup><a name=avplayer_release></a> 847 848release(callback: AsyncCallback\<void>): void 849 850Releases the playback resources. This API uses an asynchronous callback to return the result. It can be called when the AVPlayer is in any state except released. 851 852**System capability**: SystemCapability.Multimedia.Media.AVPlayer 853 854**Parameters** 855 856| Name | Type | Mandatory| Description | 857| -------- | -------- | ---- | -------------------- | 858| callback | function | Yes | Callback used to return the result.| 859 860**Error codes** 861 862For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 863 864| ID| Error Message | 865| -------- | ------------------------------------------ | 866| 5400102 | Operation not allowed. Return by callback. | 867 868**Example** 869 870```js 871avPlayer.release((err) => { 872 if (err == null) { 873 console.info('reset success'); 874 } else { 875 console.error('release filed,error message is :' + err.message) 876 } 877}) 878``` 879 880### release<sup>9+</sup> 881 882release(): Promise\<void> 883 884Releases the playback resources. This API uses a promise to return the result. It can be called when the AVPlayer is in any state except released. 885 886**System capability**: SystemCapability.Multimedia.Media.AVPlayer 887 888**Return value** 889 890| Type | Description | 891| -------------- | ------------------------- | 892| Promise\<void> | Promise used to return the result.| 893 894**Error codes** 895 896For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 897 898| ID| Error Message | 899| -------- | ----------------------------------------- | 900| 5400102 | Operation not allowed. Return by promise. | 901 902**Example** 903 904```js 905avPlayer.release().then(() => { 906 console.info('release success'); 907}, (err) => { 908 console.error('release filed,error message is :' + err.message) 909}) 910``` 911 912### getTrackDescription<sup>9+</sup> 913 914getTrackDescription(callback: AsyncCallback\<Array\<MediaDescription>>): void 915 916Obtains the audio and video track information. This API uses an asynchronous callback to return the result. It can be called only when the AVPlayer is in the prepared, playing, or paused state. 917 918**System capability**: SystemCapability.Multimedia.Media.AVPlayer 919 920**Parameters** 921 922| Name | Type | Mandatory| Description | 923| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------- | 924| callback | AsyncCallback<Array<[MediaDescription](#mediadescription8)>> | Yes | Callback used to return a **MediaDescription** array, which records the audio and video track information.| 925 926**Error codes** 927 928For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 929 930| ID| Error Message | 931| -------- | ------------------------------------------ | 932| 5400102 | Operation not allowed. Return by callback. | 933 934**Example** 935 936```js 937printfDescription(obj) { 938 for (let item in obj) { 939 let property = obj[item]; 940 console.info('audio key is ' + item); 941 console.info('audio value is ' + property); 942 } 943} 944 945avPlayer.getTrackDescription((error, arrList) => { 946 if ((arrList) != null) { 947 for (let i = 0; i < arrList.length; i++) { 948 printfDescription(arrList[i]); 949 } 950 } else { 951 console.log(`video getTrackDescription fail, error:${error}`); 952 } 953}); 954``` 955 956### getTrackDescription<sup>9+</sup> 957 958getTrackDescription(): Promise\<Array\<MediaDescription>> 959 960Obtains the audio and video track information. This API uses a promise to return the result. It can be called only when the AVPlayer is in the prepared, playing, or paused state. 961 962**System capability**: SystemCapability.Multimedia.Media.AVPlayer 963 964**Return value** 965 966| Type | Description | 967| ------------------------------------------------------ | ------------------------------------------------- | 968| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return a **MediaDescription** array, which records the audio and video track information.| 969 970**Error codes** 971 972For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 973 974| ID| Error Message | 975| -------- | ----------------------------------------- | 976| 5400102 | Operation not allowed. Return by promise. | 977 978**Example** 979 980```js 981let arrayDescription; 982 983printfDescription(obj) { 984 for (let item in obj) { 985 let property = obj[item]; 986 console.info('audio key is ' + item); 987 console.info('audio value is ' + property); 988 } 989} 990avPlayer.getTrackDescription().then((arrList) => { 991 if (arrList != null) { 992 arrayDescription = arrList; 993 } else { 994 console.log('video getTrackDescription fail'); 995 } 996}).catch((error) => { 997 console.info(`video catchCallback, error:${error}`); 998}); 999for (let i = 0; i < arrayDescription.length; i++) { 1000 printfDescription(arrayDescription[i]); 1001} 1002``` 1003 1004### seek<sup>9+</sup><a name=avplayer_seek></a> 1005 1006seek(timeMs: number, mode?:SeekMode): void 1007 1008Seeks to the specified playback position. This API can be called only when the AVPlayer is in the prepared, playing, paused, or completed state. You can check whether the seek operation takes effect by subscribing to the [seekDone](#seekDone_on) event. 1009 1010**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1011 1012**Parameters** 1013 1014| Name| Type | Mandatory| Description | 1015| ------ | ---------------------- | ---- | ------------------------------------------------------------ | 1016| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, [duration](#avplayer_duration)].| 1017| mode | [SeekMode](#seekmode8) | No | Seek mode based on the video I frame. **Set this parameter only for video playback.** | 1018 1019**Example** 1020 1021```js 1022let seekTime = 1000 1023avPlayer.seek(seekTime, media.SeekMode.SEEK_PREV_SYNC) 1024``` 1025 1026### on('seekDone')<sup>9+</sup><a name = seekDone_on></a> 1027 1028on(type: 'seekDone', callback: Callback\<number>): void 1029 1030Subscribes to the event to check whether the seek operation takes effect. 1031 1032**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1033 1034**Parameters** 1035 1036| Name | Type | Mandatory| Description | 1037| -------- | -------- | ---- | ------------------------------------------------------------ | 1038| type | string | Yes | Event type, which is **'seekDone'** in this case. This event is triggered each time **seek()** is called.| 1039| callback | Callback\<number> | Yes | Callback invoked when the event is triggered. It reports the time position requested by the user.<br>For video playback, [SeekMode](#seekmode8) may cause the actual position to be different from that requested by the user. The exact position can be obtained from the **currentTime** attribute. The time in this callback only means that the requested seek operation is complete.| 1040 1041**Example** 1042 1043```js 1044avPlayer.on('seekDone', (seekDoneTime:number) => { 1045 console.info('seekDone success,and seek time is:' + seekDoneTime) 1046}) 1047``` 1048 1049### off('seekDone')<sup>9+</sup><a name = seekDone_off></a> 1050 1051off(type: 'seekDone'): void 1052 1053Unsubscribes from the event that checks whether the seek operation takes effect. 1054 1055**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1056 1057**Parameters** 1058 1059| Name| Type | Mandatory| Description | 1060| ------ | ------ | ---- | ---------------------------------------------------- | 1061| type | string | Yes | Event type, which is **'seekDone'** in this case.| 1062 1063**Example** 1064 1065```js 1066avPlayer.off('seekDone') 1067``` 1068 1069### setSpeed<sup>9+</sup> 1070 1071setSpeed(speed: PlaybackSpeed): void 1072 1073Sets the playback speed. This API can be called only when the AVPlayer is in the prepared, playing, paused, or completed state. You can check whether the setting takes effect by subscribing to the [speedDone](#speedDone_on) event. 1074 1075**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1076 1077**Parameters** 1078 1079| Name| Type | Mandatory| Description | 1080| ------ | -------------------------------- | ---- | ------------------ | 1081| speed | [PlaybackSpeed](#playbackspeed8) | Yes | Playback speed to set.| 1082 1083**Example** 1084 1085```js 1086avPlayer.setSpeed(media.PlaybackSpeed.SPEED_FORWARD_2_00_X) 1087``` 1088 1089### on('speedDone')<sup>9+</sup><a name = speedDone_on></a> 1090 1091on(type: 'speedDone', callback: Callback\<number>): void 1092 1093Subscribes to the event to check whether the playback speed is successfully set. 1094 1095**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1096 1097**Parameters** 1098 1099| Name | Type | Mandatory| Description | 1100| -------- | -------- | ---- | ------------------------------------------------------------ | 1101| type | string | Yes | Event type, which is **'speedDone'** in this case. This event is triggered each time **setSpeed()** is called.| 1102| callback | Callback\<number> | Yes | Callback invoked when the event is triggered. It reports the speed set. For details, see [PlaybackSpeed](#playbackspeed8).| 1103 1104**Example** 1105 1106```js 1107avPlayer.on('speedDone', (speed:number) => { 1108 console.info('speedDone success,and speed value is:' + speed) 1109}) 1110``` 1111 1112### off('speedDone')<sup>9+</sup><a name = speedDone_off></a> 1113 1114off(type: 'speedDone'): void 1115 1116Unsubscribes from the event that checks whether the playback speed is successfully set. 1117 1118**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1119 1120**Parameters** 1121 1122| Name| Type | Mandatory| Description | 1123| ------ | ------ | ---- | --------------------------------------------------------- | 1124| type | string | Yes | Event type, which is **'speedDone'** in this case.| 1125 1126**Example** 1127 1128```js 1129avPlayer.off('speedDone') 1130``` 1131 1132### setBitrate<sup>9+</sup> 1133 1134setBitrate(bitrate: number): void 1135 1136Sets the bit rate, which is valid only for HTTP Live Streaming (HLS) streams. This API can be called only when the AVPlayer is in the prepared, playing, paused, or completed state. You can check whether the setting takes effect by subscribing to the [bitrateDone](#bitrateDone_on) event. 1137 1138**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1139 1140**Parameters** 1141 1142| Name | Type | Mandatory| Description | 1143| ------- | ------ | ---- | ------------------------------------------------------------ | 1144| bitrate | number | Yes | Bit rate to set. You can obtain the available bit rates of the current HLS stream by subscribing to the [availableBitrates](#availableBitrates_on) event. If the bit rate to set is not in the list of the available bit rates, the AVPlayer selects from the list the minimum bit rate that is closed to the bit rate to set. If the length of the available bit rate list obtained through the event is 0, no bit rate can be set and the **bitrateDone** callback will not be triggered.| 1145 1146**Example** 1147 1148```js 1149let bitrate = 96000 1150avPlayer.setBitrate(bitrate) 1151``` 1152 1153### on('bitrateDone')<sup>9+</sup><a name = bitrateDone_on></a> 1154 1155on(type: 'bitrateDone', callback: Callback\<number>): void 1156 1157Subscribes to the event to check whether the bit rate is successfully set. 1158 1159**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1160 1161**Parameters** 1162 1163| Name | Type | Mandatory| Description | 1164| -------- | -------- | ---- | ------------------------------------------------------------ | 1165| type | string | Yes | Event type, which is **'bitrateDone'** in this case. This event is triggered each time **setBitrate()** is called.| 1166| callback | function | Yes | Callback invoked when the event is triggered. It reports the effective bit rate. | 1167 1168**Example** 1169 1170```js 1171avPlayer.on('bitrateDone', (bitrate:number) => { 1172 console.info('bitrateDone success,and bitrate value is:' + bitrate) 1173}) 1174``` 1175 1176### off('bitrateDone')<sup>9+</sup><a name = bitrateDone_off></a> 1177 1178off(type: 'bitrateDone'): void 1179 1180Unsubscribes from the event that checks whether the bit rate is successfully set. 1181 1182**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1183 1184**Parameters** 1185 1186| Name| Type | Mandatory| Description | 1187| ------ | ------ | ---- | ------------------------------------------------------------ | 1188| type | string | Yes | Event type, which is **'bitrateDone'** in this case.| 1189 1190**Example** 1191 1192```js 1193avPlayer.off('bitrateDone') 1194``` 1195 1196### on('availableBitrates')<sup>9+</sup><a name = availableBitrates_on></a> 1197 1198on(type: 'availableBitrates', callback: (bitrates: Array\<number>) => void): void 1199 1200Subscribes to available bit rates of HLS streams. This event is reported only after the AVPlayer switches to the prepared state. 1201 1202**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1203 1204**Parameters** 1205 1206| Name | Type | Mandatory| Description | 1207| -------- | -------- | ---- | ------------------------------------------------------------ | 1208| type | string | Yes | Event type, which is **'availableBitrates'** in this case. This event is triggered once after the AVPlayer switches to the prepared state.| 1209| callback | function | Yes | Callback invoked when the event is triggered. It returns an array that holds the available bit rates. If the array length is 0, no bit rate can be set.| 1210 1211**Example** 1212 1213```js 1214avPlayer.on('availableBitrates', (bitrates: Array<number>) => { 1215 console.info('availableBitrates success,and availableBitrates length is:' + bitrates.length) 1216}) 1217``` 1218 1219### off('availableBitrates')<sup>9+</sup><a name = availableBitrates_off></a> 1220 1221off(type: 'availableBitrates'): void 1222 1223Unsubscribes from available bit rates of HLS streams. 1224 1225**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1226 1227**Parameters** 1228 1229| Name| Type | Mandatory| Description | 1230| ------ | ------ | ---- | ------------------------------------------------------------ | 1231| type | string | Yes | Event type, which is **'availableBitrates'** in this case.| 1232 1233**Example** 1234 1235```js 1236avPlayer.off('availableBitrates') 1237``` 1238 1239### setVolume<sup>9+</sup> 1240 1241setVolume(volume: number): void 1242 1243Sets the volume. This API can be called only when the AVPlayer is in the prepared, playing, paused, or completed state. You can check whether the setting takes effect by subscribing to the [volumeChange](#volumeChange_on) event. 1244 1245**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1246 1247**Parameters** 1248 1249| Name| Type | Mandatory| Description | 1250| ------ | ------ | ---- | ------------------------------------------------------------ | 1251| volume | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1.00** indicates the maximum volume (100%).| 1252 1253**Example** 1254 1255```js 1256let volume = 1.0 1257avPlayer.setVolume(volume) 1258``` 1259 1260### on('volumeChange')<sup>9+</sup><a name = volumeChange_on></a> 1261 1262on(type: 'volumeChange', callback: Callback\<number>): void 1263 1264Subscribes to the event to check whether the volume is successfully set. 1265 1266**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1267 1268**Parameters** 1269 1270| Name | Type | Mandatory| Description | 1271| -------- | -------- | ---- | ------------------------------------------------------------ | 1272| type | string | Yes | Event type, which is **'volumeChange'** in this case. This event is triggered each time **setVolume()** is called.| 1273| callback | function | Yes | Callback invoked when the event is triggered. It reports the effective volume. | 1274 1275**Example** 1276 1277```js 1278avPlayer.on('volumeChange', (vol:number) => { 1279 console.info('volumeChange success,and new volume is :' + vol) 1280}) 1281``` 1282 1283### off('volumeChange')<sup>9+</sup><a name = volumeChange_off></a> 1284 1285off(type: 'volumeChange'): void 1286 1287Unsubscribes from the event that checks whether the volume is successfully set. 1288 1289**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1290 1291**Parameters** 1292 1293| Name| Type | Mandatory| Description | 1294| ------ | ------ | ---- | ------------------------------------------------------------ | 1295| type | string | Yes | Event type, which is **'volumeChange'** in this case.| 1296 1297**Example** 1298 1299```js 1300avPlayer.off('volumeChange') 1301``` 1302 1303### on('endOfStream')<sup>9+</sup><a name = endOfStream_on></a> 1304 1305on(type: 'endOfStream', callback: Callback\<void>): void 1306 1307Subscribes to the event that indicates the end of the stream being played. If **loop=1** is set, the AVPlayer seeks to the beginning of the stream and plays the stream again. If **loop** is not set, the completed state is reported through the [stateChange](#stateChange_on) event. 1308 1309**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1310 1311**Parameters** 1312 1313| Name | Type | Mandatory| Description | 1314| -------- | -------- | ---- | ------------------------------------------------------------ | 1315| type | string | Yes | Event type, which is **'endOfStream'** in this case. This event is triggered when the AVPlayer finishes playing the media asset.| 1316| callback | Callback\<void> | Yes | Callback invoked when the event is triggered. | 1317 1318**Example** 1319 1320```js 1321avPlayer.on('endOfStream', () => { 1322 console.info('endOfStream success') 1323}) 1324``` 1325 1326### off('endOfStream')<sup>9+</sup><a name = endOfStream_off></a> 1327 1328off(type: 'endOfStream'): void 1329 1330Unsubscribes from the event that indicates the end of the stream being played. 1331 1332**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1333 1334**Parameters** 1335 1336| Name| Type | Mandatory| Description | 1337| ------ | ------ | ---- | ------------------------------------------------------------ | 1338| type | string | Yes | Event type, which is **'endOfStream'** in this case.| 1339 1340**Example** 1341 1342```js 1343avPlayer.off('endOfStream') 1344``` 1345 1346### on('timeUpdate')<sup>9+</sup><a name = timeUpdate_on></a> 1347 1348on(type: 'timeUpdate', callback: Callback\<number>): void 1349 1350Subscribes to playback position changes. It is used to refresh the current position of the progress bar. By default, this event is reported every 1 second. However, it is reported immediately upon a successful seek operation. 1351 1352**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1353 1354**Parameters** 1355 1356| Name | Type | Mandatory| Description | 1357| -------- | -------- | ---- | ---------------------------------------------- | 1358| type | string | Yes | Event type, which is **'timeUpdate'** in this case.| 1359| callback | function | Yes | Callback invoked when the event is triggered. It reports the current playback position, in ms. | 1360 1361**Example** 1362 1363```js 1364avPlayer.on('timeUpdate', (time:number) => { 1365 console.info('timeUpdate success,and new time is :' + time) 1366}) 1367``` 1368 1369### off('timeUpdate')<sup>9+</sup><a name = timeUpdate_off></a> 1370 1371off(type: 'timeUpdate'): void 1372 1373Unsubscribes from playback position changes. 1374 1375**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1376 1377**Parameters** 1378 1379| Name| Type | Mandatory| Description | 1380| ------ | ------ | ---- | -------------------------------------------------- | 1381| type | string | Yes | Event type, which is **'timeUpdate'** in this case.| 1382 1383**Example** 1384 1385```js 1386avPlayer.off('timeUpdate') 1387``` 1388 1389### on('durationUpdate')<sup>9+</sup><a name = durationUpdate_on></a> 1390 1391on(type: 'durationUpdate', callback: Callback\<number>): void 1392 1393Subscribes to media asset duration changes. It is used to refresh the length of the progress bar. By default, this event is reported once when the AVPlayer switches to the prepared state. However, it can be repeatedly reported for special streams that trigger duration changes. 1394 1395**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1396 1397**Parameters** 1398 1399| Name | Type | Mandatory| Description | 1400| -------- | -------- | ---- | -------------------------------------------------- | 1401| type | string | Yes | Event type, which is **'durationUpdate'** in this case.| 1402| callback | function | Yes | Callback invoked when the event is triggered. It reports the media asset duration, in ms. | 1403 1404**Example** 1405 1406```js 1407avPlayer.on('durationUpdate', (duration) => { 1408 console.info('durationUpdate success,new duration is :' + duration) 1409}) 1410``` 1411 1412### off('durationUpdate')<sup>9+</sup><a name = durationUpdate_off></a> 1413 1414off(type: 'durationUpdate'): void 1415 1416Unsubscribes from media asset duration changes. 1417 1418**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1419 1420**Parameters** 1421 1422| Name| Type | Mandatory| Description | 1423| ------ | ------ | ---- | ------------------------------------------------------ | 1424| type | string | Yes | Event type, which is **'durationUpdate'** in this case.| 1425 1426**Example** 1427 1428```js 1429avPlayer.off('durationUpdate') 1430``` 1431 1432### on('bufferingUpdate')<sup>9+</sup><a name = bufferingUpdate_on></a> 1433 1434on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void 1435 1436Subscribes to audio and video buffer changes. This subscription is supported only in network playback scenarios. 1437 1438**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1439 1440**Parameters** 1441 1442| Name | Type | Mandatory| Description | 1443| -------- | -------- | ---- | ------------------------------------------------------------ | 1444| type | string | Yes | Event type, which is **'bufferingUpdate'** in this case. | 1445| callback | function | Yes | Callback invoked when the event is triggered.<br>When [BufferingInfoType](#bufferinginfotype8) is set to **BUFFERING_PERCENT** or **CACHED_DURATION**, **value** is valid. Otherwise, **value** is fixed at **0**.| 1446 1447**Example** 1448 1449```js 1450avPlayer.on('bufferingUpdate', (infoType: media.BufferingInfoType, value: number) => { 1451 console.info('bufferingUpdate success,and infoType value is:' + infoType + ', value is :' + value) 1452}) 1453``` 1454 1455### off('bufferingUpdate')<sup>9+</sup><a name = bufferingUpdate_off></a> 1456 1457off(type: 'bufferingUpdate'): void 1458 1459Unsubscribes from audio and video buffer changes. 1460 1461**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1462 1463**Parameters** 1464 1465| Name| Type | Mandatory| Description | 1466| ------ | ------ | ---- | --------------------------------------------------------- | 1467| type | string | Yes | Event type, which is **'bufferingUpdate'** in this case.| 1468 1469**Example** 1470 1471```js 1472avPlayer.off('bufferingUpdate') 1473``` 1474 1475### on('startRenderFrame')<sup>9+</sup><a name = startRenderFrame_on></a> 1476 1477on(type: 'startRenderFrame', callback: Callback\<void>): void 1478 1479Subscribes to the event that indicates rendering starts for the first frame. This subscription is supported only in the video playback scenarios. This event only means that the playback service sends the first frame to the display module. The actual rendering effect depends on the rendering performance of the display service. 1480 1481**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1482 1483**Parameters** 1484 1485| Name | Type | Mandatory| Description | 1486| -------- | -------- | ---- | ------------------------------------------------------------ | 1487| type | string | Yes | Event type, which is **'startRenderFrame'** in this case.| 1488| callback | Callback\<void> | Yes | Callback invoked when the event is triggered. | 1489 1490**Example** 1491 1492```js 1493avPlayer.on('startRenderFrame', () => { 1494 console.info('startRenderFrame success') 1495}) 1496``` 1497 1498### off('startRenderFrame')<sup>9+</sup><a name = startRenderFrame_off></a> 1499 1500off(type: 'startRenderFrame'): void 1501 1502Unsubscribes from the event that indicates rendering starts for the first frame. 1503 1504**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1505 1506**Parameters** 1507 1508| Name| Type | Mandatory| Description | 1509| ------ | ------ | ---- | ------------------------------------------------------------ | 1510| type | string | Yes | Event type, which is **'startRenderFrame'** in this case.| 1511 1512**Example** 1513 1514```js 1515avPlayer.off('startRenderFrame') 1516``` 1517 1518### on('videoSizeChange')<sup>9+</sup><a name = videoSizeChange_on></a> 1519 1520on(type: 'videoSizeChange', callback: (width: number, height: number) => void): void 1521 1522Subscribes to video size (width and height) changes. This subscription is supported only in the video playback scenarios. By default, this event is reported only once in the prepared state. However, it is also reported upon resolution changes in the case of HLS streams. 1523 1524**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1525 1526**Parameters** 1527 1528| Name | Type | Mandatory| Description | 1529| -------- | -------- | ---- | ------------------------------------------------------------ | 1530| type | string | Yes | Event type, which is **'videoSizeChange'** in this case.| 1531| callback | function | Yes | Callback invoked when the event is triggered. **width** indicates the video width, and **height** indicates the video height. | 1532 1533**Example** 1534 1535```js 1536avPlayer.on('videoSizeChange', (width: number, height: number) => { 1537 console.info('videoSizeChange success,and width is:' + width + ', height is :' + height) 1538}) 1539``` 1540 1541### off('videoSizeChange')<sup>9+</sup><a name = videoSizeChange_off></a> 1542 1543off(type: 'videoSizeChange'): void 1544 1545Unsubscribes from video size changes. 1546 1547**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1548 1549**Parameters** 1550 1551| Name| Type | Mandatory| Description | 1552| ------ | ------ | ---- | ------------------------------------------------------------ | 1553| type | string | Yes | Event type, which is **'videoSizeChange'** in this case.| 1554 1555**Example** 1556 1557```js 1558avPlayer.off('videoSizeChange') 1559``` 1560 1561### on('audioInterrupt')<sup>9+</sup><a name = audioInterrupt_on></a> 1562 1563on(type: 'audioInterrupt', callback: (info: audio.InterruptEvent) => void): void 1564 1565Subscribes to the audio interruption event. When multiple audio and video assets are played at the same time, this event is triggered based on the audio interruption mode [audio.InterruptMode](js-apis-audio.md#interruptmode9). 1566 1567**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1568 1569**Parameters** 1570 1571| Name | Type | Mandatory| Description | 1572| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------------------- | 1573| type | string | Yes | Event type, which is **'audioInterrupt'** in this case.| 1574| callback | [audio.InterruptEvent<sup>9+</sup>](js-apis-audio.md#interruptevent9) | Yes | Callback invoked when the event is triggered. | 1575 1576**Example** 1577 1578```js 1579import audio from '@ohos.multimedia.audio'; 1580 1581avPlayer.on('audioInterrupt', (info: audio.InterruptEvent) => { 1582 console.info('audioInterrupt success,and InterruptEvent info is:' + info) 1583}) 1584``` 1585 1586### off('audioInterrupt')<sup>9+</sup><a name = audioInterrupt_off></a> 1587 1588off(type: 'audioInterrupt'): void 1589 1590Unsubscribes from the audio interruption event. 1591 1592**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1593 1594**Parameters** 1595 1596| Name| Type | Mandatory| Description | 1597| ------ | ------ | ---- | ------------------------------------------------------------ | 1598| type | string | Yes | Event type, which is **'audioInterrupt'** in this case.| 1599 1600**Example** 1601 1602```js 1603avPlayer.off('audioInterrupt') 1604``` 1605 1606## AVPlayerState<sup>9+</sup><a name = avplayerstate></a> 1607 1608Enumerates the states of the [AVPlayer](#avplayer9). Your application can proactively obtain the AVPlayer state through the **state** attribute or obtain the reported AVPlayer state by subscribing to the [stateChange](#stateChange_on) event. For details about the rules for state transition, see [Audio Playback](../../media/using-avplayer-for-playback.md). 1609 1610**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1611 1612| Name | Type | Description | 1613| :-----------------------------: | :----: | :----------------------------------------------------------- | 1614| idle | string | The AVPlayer enters this state after [createAVPlayer()](#mediacreateavplayer9) or **reset()** is called.<br>In case **createAVPlayer()** is used, all attributes are set to their default values.<br>In case **reset()** is called, the **url<sup>9+</sup>** or **fdSrc<sup>9+</sup>** attribute is reset, and other attributes are retained.| 1615| initialized | string | The AVPlayer enters this state after **url<sup>9+</sup>** or **fdSrc<sup>9+</sup>** attribute is set in the idle state. In this case, you can configure static attributes such as the window and audio.| 1616| prepared | string | The AVPlayer enters this state when **prepare()** is called in the initialized state. In this case, the playback engine has prepared the resources.| 1617| playing | string | The AVPlayer enters this state when **play()** is called in the prepared, paused, or completed state.| 1618| paused | string | The AVPlayer enters this state when **pause()** is called in the playing state.| 1619| completed | string | The AVPlayer enters this state when a media asset finishes playing and loop playback is not set (no **loop = 1**). In this case, if **play()** is called, the AVPlayer enters the playing state and replays the media asset; if **stop()** is called, the AVPlayer enters the stopped state.| 1620| stopped | string | The AVPlayer enters this state when **stop()** is called in the prepared, playing, paused, or completed state. In this case, the playback engine retains the attributes but releases the memory resources. You can call **prepare()** to prepare the resources again, call **reset()** to reset the attributes, or call **release()** to destroy the playback engine.| 1621| released | string | The AVPlayer enters this state when **release()** is called. The playback engine associated with the **AVPlayer** instance is destroyed, and the playback process ends. This is the final state.| 1622| error<a name = error_state></a> | string | The AVPlayer enters this state when an irreversible error occurs in the playback engine. You can call **reset()** to reset the attributes or call **release()** to destroy the playback engine. For details on the errors, see [Error Classification](#error_info).<br>**NOTE** Relationship between the error state and the [on('error')](#error_on) event<br>1. When the AVPlayer enters the error state, the [on('error')](#error_on) event is triggered. You can obtain the detailed error information through this event.<br>2. When the AVPlayer enters the error state, the playback service stops. This requires the client to design a fault tolerance mechanism to call **reset()** or **release()**.<br>3. The client receives [on('error')](#error_on) event but the AVPlayer does not enter the error state. This situation occurs due to either of the following reasons:<br>Cause 1: The client calls an API in an incorrect state or passes in an incorrect parameter, and the AVPlayer intercepts the call. If this is the case, the client must correct its code logic.<br>Cause 2: A stream error is detected during playback. As a result, the container and decoding are abnormal for a short period of time, but continuous playback and playback control operations are not affected. If this is the case, the client does not need to design a fault tolerance mechanism.| 1623 1624## AVFileDescriptor<sup>9+</sup> 1625 1626Describes an audio and video file asset. It is used to specify a particular asset for playback based on its offset and length within a file. 1627 1628**System capability**: SystemCapability.Multimedia.Media.Core 1629 1630| Name | Type | Mandatory| Description | 1631| ------ | ------ | ---- | ------------------------------------------------------------ | 1632| fd | number | Yes | Resource handle, which is obtained by calling **resourceManager.getRawFileDescriptor**. | 1633| offset | number | Yes | Resource offset, which needs to be entered based on the preset asset information. An invalid value causes a failure to parse audio and video assets.| 1634| length | number | Yes | Resource length, which needs to be entered based on the preset asset information. An invalid value causes a failure to parse audio and video assets.| 1635 1636## SeekMode<sup>8+</sup> 1637 1638Enumerates the video playback seek modes, which can be passed in the **seek** API. 1639 1640**System capability**: SystemCapability.Multimedia.Media.Core 1641 1642| Name | Value | Description | 1643| -------------- | ---- | ------------------------------------------------------------ | 1644| SEEK_NEXT_SYNC | 0 | Seeks to the next key frame at the specified position. You are advised to use this value for the rewind operation.| 1645| SEEK_PREV_SYNC | 1 | Seeks to the previous key frame at the specified position. You are advised to use this value for the fast-forward operation.| 1646 1647## PlaybackSpeed<sup>8+</sup> 1648 1649Enumerates the video playback speeds, which can be passed in the **setSpeed** API. 1650 1651**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1652 1653| Name | Value | Description | 1654| -------------------- | ---- | ------------------------------ | 1655| SPEED_FORWARD_0_75_X | 0 | Plays the video at 0.75 times the normal speed.| 1656| SPEED_FORWARD_1_00_X | 1 | Plays the video at the normal speed. | 1657| SPEED_FORWARD_1_25_X | 2 | Plays the video at 1.25 times the normal speed.| 1658| SPEED_FORWARD_1_75_X | 3 | Plays the video at 1.75 times the normal speed.| 1659| SPEED_FORWARD_2_00_X | 4 | Plays the video at 2.00 times the normal speed.| 1660 1661## VideoScaleType<sup>9+</sup> 1662 1663Enumerates the video scale modes. 1664 1665**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1666 1667| Name | Value | Description | 1668| ------------------------- | ---- | ------------------------------------------------ | 1669| VIDEO_SCALE_TYPE_FIT | 0 | The video will be stretched to fit the window. | 1670| VIDEO_SCALE_TYPE_FIT_CROP | 1 | The video will be stretched to fit the window, without changing its aspect ratio. The content may be cropped.| 1671 1672## MediaDescription<sup>8+</sup> 1673 1674Defines media information in key-value mode. 1675 1676**System capability**: SystemCapability.Multimedia.Media.Core 1677 1678**Example** 1679 1680```js 1681import media from '@ohos.multimedia.media' 1682function printfItemDescription(obj, key) { 1683 let property = obj[key]; 1684 console.info('audio key is ' + key); // Specify a key. For details about the keys, see [MediaDescriptionKey]. 1685 console.info('audio value is ' + property); // Obtain the value of the key. The value can be any type. For details about the types, see [MediaDescriptionKey]. 1686} 1687let audioPlayer = media.createAudioPlayer(); 1688audioPlayer.getTrackDescription((error, arrList) => { 1689 if (arrList != null) { 1690 for (let i = 0; i < arrList.length; i++) { 1691 printfItemDescription(arrList[i], media.MediaDescriptionKey.MD_KEY_TRACK_TYPE); // Print the MD_KEY_TRACK_TYPE value of each track. 1692 } 1693 } else { 1694 console.log(`audio getTrackDescription fail, error:${error}`); 1695 } 1696}); 1697``` 1698 1699## AVRecorder<sup>9+</sup> 1700 1701A recording management class that provides APIs to record media assets. Before calling any API in **AVRecorder**, you must use **createAVRecorder()** to create an **AVRecorder** instance. 1702 1703For details about the audio and video recording demo, see [Audio Recording](../../media/using-avrecorder-for-recording.md) and [Video Recording](../../media/video-recording.md). 1704 1705> **NOTE** 1706> 1707> To use the camera to record videos, the camera module is required. For details about how to use the APIs provided by the camera module, see [Camera Management](js-apis-camera.md). 1708 1709### Attributes 1710 1711**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1712 1713| Name | Type | Readable| Writable| Description | 1714| ------- | ------------------------------------ | ---- | ---- | ------------------ | 1715| state9+ | [AVRecorderState](#avrecorderstate9) | Yes | No | AVRecorder state.| 1716 1717### prepare<sup>9+</sup><a name=avrecorder_prepare></a> 1718 1719prepare(config: AVRecorderConfig, callback: AsyncCallback\<void>): void 1720 1721Sets audio and video recording parameters. This API uses an asynchronous callback to return the result. 1722 1723**Required permissions:** ohos.permission.MICROPHONE 1724 1725This permission is required only if audio recording is involved. 1726 1727**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1728 1729**Parameters** 1730 1731| Name | Type | Mandatory| Description | 1732| -------- | -------------------------------------- | ---- | ------------------------------------- | 1733| config | [AVRecorderConfig](#avrecorderconfig9) | Yes | Audio and video recording parameters to set. | 1734| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 1735 1736**Error codes** 1737 1738For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1739 1740| ID| Error Message | 1741| -------- | --------------------------------------- | 1742| 201 | Permission denied. Return by callback. | 1743| 401 | Parameter error. Return by callback. | 1744| 5400102 | Operate not permit. Return by callback. | 1745| 5400105 | Service died. Return by callback. | 1746 1747**Example** 1748 1749```js 1750// Configure the parameters based on those supported by the hardware device. 1751let AVRecorderProfile = { 1752 audioBitrate : 48000, 1753 audioChannels : 2, 1754 audioCodec : media.CodecMimeType.AUDIO_AAC, 1755 audioSampleRate : 48000, 1756 fileFormat : media.ContainerFormatType.CFT_MPEG_4, 1757 videoBitrate : 2000000, 1758 videoCodec : media.CodecMimeType.VIDEO_AVC, 1759 videoFrameWidth : 640, 1760 videoFrameHeight : 480, 1761 videoFrameRate : 30 1762} 1763let AVRecorderConfig = { 1764 audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, 1765 videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 1766 profile : AVRecorderProfile, 1767 url : 'fd://', // Before passing in an FD to this parameter, the file must be created by the caller and granted with the read and write permissions. Example value: eg.fd://45. 1768 rotation: 0, // The value can be 0, 90, 180, or 270. If any other value is used, prepare() reports an error. 1769 location : { latitude : 30, longitude : 130 } 1770} 1771 1772avRecorder.prepare(AVRecorderConfig, (err) => { 1773 if (err == null) { 1774 console.info('prepare success'); 1775 } else { 1776 console.info('prepare failed and error is ' + err.message); 1777 } 1778}) 1779``` 1780 1781### prepare<sup>9+</sup> 1782 1783prepare(config: AVRecorderConfig): Promise\<void> 1784 1785Sets audio and video recording parameters. This API uses a promise to return the result. 1786 1787**Required permissions:** ohos.permission.MICROPHONE 1788 1789This permission is required only if audio recording is involved. 1790 1791**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1792 1793**Parameters** 1794 1795| Name| Type | Mandatory| Description | 1796| ------ | -------------------------------------- | ---- | -------------------------- | 1797| config | [AVRecorderConfig](#avrecorderconfig9) | Yes | Audio and video recording parameters to set.| 1798 1799**Return value** 1800 1801| Type | Description | 1802| -------------- | ------------------------------------------ | 1803| Promise\<void> | Promise used to return the result.| 1804 1805**Error codes** 1806 1807For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1808 1809| ID| Error Message | 1810| -------- | -------------------------------------- | 1811| 201 | Permission denied. Return by promise. | 1812| 401 | Parameter error. Return by promise. | 1813| 5400102 | Operate not permit. Return by promise. | 1814| 5400105 | Service died. Return by promise. | 1815 1816**Example** 1817 1818```js 1819// Configure the parameters based on those supported by the hardware device. 1820let AVRecorderProfile = { 1821 audioBitrate : 48000, 1822 audioChannels : 2, 1823 audioCodec : media.CodecMimeType.AUDIO_AAC, 1824 audioSampleRate : 48000, 1825 fileFormat : media.ContainerFormatType.CFT_MPEG_4, 1826 videoBitrate : 2000000, 1827 videoCodec : media.CodecMimeType.VIDEO_AVC, 1828 videoFrameWidth : 640, 1829 videoFrameHeight : 480, 1830 videoFrameRate : 30 1831} 1832let AVRecorderConfig = { 1833 audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, 1834 videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 1835 profile : AVRecorderProfile, 1836 url : 'fd://', // Before passing in an FD to this parameter, the file must be created by the caller and granted with the read and write permissions. Example value: eg.fd://45. 1837 rotation: 0, // The value can be 0, 90, 180, or 270. If any other value is used, prepare() reports an error. 1838 location : { latitude : 30, longitude : 130 } 1839} 1840 1841avRecorder.prepare(AVRecorderConfig).then(() => { 1842 console.info('prepare success'); 1843}).catch((err) => { 1844 console.info('prepare failed and catch error is ' + err.message); 1845}); 1846 1847``` 1848 1849### getInputSurface<sup>9+</sup><a name=avrecorder_getinputsurface></a> 1850 1851getInputSurface(callback: AsyncCallback\<string>): void 1852 1853Obtains the surface required for recording. This API uses an asynchronous callback to return the result. The caller obtains the **surfaceBuffer** from this surface and fills in the corresponding video data. 1854 1855Note that the video data must carry the timestamp (in ns) and buffer size, and the start time of the timestamp must be based on the system startup time. 1856 1857This API can be called only after the **prepare()** API is called. 1858 1859**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1860 1861**Parameters** 1862 1863| Name | Type | Mandatory| Description | 1864| -------- | ---------------------- | ---- | --------------------------- | 1865| callback | AsyncCallback\<string> | Yes | Callback used to obtain the result.| 1866 1867**Error codes** 1868 1869For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1870 1871| ID| Error Message | 1872| -------- | --------------------------------------- | 1873| 5400102 | Operate not permit. Return by callback. | 1874| 5400103 | IO error. Return by callback. | 1875| 5400105 | Service died. Return by callback. | 1876 1877**Example** 1878 1879```js 1880let surfaceID = null; // The surfaceID is transferred to the camera API to create a videoOutput instance. 1881 1882avRecorder.getInputSurface((err, surfaceId) => { 1883 if (err == null) { 1884 console.info('getInputSurface success'); 1885 surfaceID = surfaceId; 1886 } else { 1887 console.info('getInputSurface failed and error is ' + err.message); 1888 } 1889}); 1890 1891``` 1892 1893### getInputSurface<sup>9+</sup> 1894 1895getInputSurface(): Promise\<string> 1896 1897Obtains the surface required for recording. This API uses a promise to return the result. The caller obtains the **surfaceBuffer** from this surface and fills in the corresponding video data. 1898 1899Note that the video data must carry the timestamp (in ns) and buffer size, and the start time of the timestamp must be based on the system startup time. 1900 1901This API can be called only after the **prepare()** API is called. 1902 1903**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1904 1905**Return value** 1906 1907| Type | Description | 1908| ---------------- | -------------------------------- | 1909| Promise\<string> | Promise used to return the result.| 1910 1911**Error codes** 1912 1913For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1914 1915| ID| Error Message | 1916| -------- | -------------------------------------- | 1917| 5400102 | Operate not permit. Return by promise. | 1918| 5400103 | IO error. Return by promise. | 1919| 5400105 | Service died. Return by promise. | 1920 1921**Example** 1922 1923```js 1924let surfaceID = null; // The surfaceID is transferred to the camera API to create a videoOutput instance. 1925 1926avRecorder.getInputSurface().then((surfaceId) => { 1927 console.info('getInputSurface success'); 1928 surfaceID = surfaceId; 1929}).catch((err) => { 1930 console.info('getInputSurface failed and catch error is ' + err.message); 1931}); 1932``` 1933 1934### start<sup>9+</sup><a name=avrecorder_start></a> 1935 1936start(callback: AsyncCallback\<void>): void 1937 1938Starts recording. This API uses an asynchronous callback to return the result. 1939 1940For audio-only recording, this API can be called only after the **prepare()** API is called. For video-only recording, this API can be called only after the **getInputSurface()** API is called. 1941 1942**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1943 1944**Parameters** 1945 1946| Name | Type | Mandatory| Description | 1947| -------- | -------------------- | ---- | ---------------------------- | 1948| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 1949 1950**Error codes** 1951 1952For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1953 1954| ID| Error Message | 1955| -------- | --------------------------------------- | 1956| 5400102 | Operate not permit. Return by callback. | 1957| 5400103 | IO error. Return by callback. | 1958| 5400105 | Service died. Return by callback. | 1959 1960**Example** 1961 1962```js 1963avRecorder.start((err) => { 1964 if (err == null) { 1965 console.info('start AVRecorder success'); 1966 } else { 1967 console.info('start AVRecorder failed and error is ' + err.message); 1968 } 1969}); 1970``` 1971 1972### start<sup>9+</sup> 1973 1974start(): Promise\<void> 1975 1976Starts recording. This API uses a promise to return the result. 1977 1978For audio-only recording, this API can be called only after the **prepare()** API is called. For video-only recording, this API can be called only after the **getInputSurface()** API is called. 1979 1980**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1981 1982**Return value** 1983 1984| Type | Description | 1985| -------------- | ------------------------------------- | 1986| Promise\<void> | Promise used to return the result.| 1987 1988**Error codes** 1989 1990For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1991 1992| ID| Error Message | 1993| -------- | -------------------------------------- | 1994| 5400102 | Operate not permit. Return by promise. | 1995| 5400103 | IO error. Return by promise. | 1996| 5400105 | Service died. Return by promise. | 1997 1998**Example** 1999 2000```js 2001avRecorder.start().then(() => { 2002 console.info('start AVRecorder success'); 2003}).catch((err) => { 2004 console.info('start AVRecorder failed and catch error is ' + err.message); 2005}); 2006``` 2007 2008### pause<sup>9+</sup><a name=avrecorder_pause></a> 2009 2010pause(callback: AsyncCallback\<void>): void 2011 2012Pauses recording. This API uses an asynchronous callback to return the result. 2013 2014This API can be called only after the **start()** API is called. You can call **resume()** to resume recording. 2015 2016**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2017 2018**Parameters** 2019 2020| Name | Type | Mandatory| Description | 2021| -------- | -------------------- | ---- | --------------------------- | 2022| callback | AsyncCallback\<void> | Yes | Callback used to obtain the result.| 2023 2024**Error codes** 2025 2026For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2027 2028| ID| Error Message | 2029| -------- | --------------------------------------- | 2030| 5400102 | Operate not permit. Return by callback. | 2031| 5400103 | IO error. Return by callback. | 2032| 5400105 | Service died. Return by callback. | 2033 2034**Example** 2035 2036```js 2037avRecorder.pause((err) => { 2038 if (err == null) { 2039 console.info('pause AVRecorder success'); 2040 } else { 2041 console.info('pause AVRecorder failed and error is ' + err.message); 2042 } 2043}); 2044``` 2045 2046### pause<sup>9+</sup> 2047 2048pause(): Promise\<void> 2049 2050Pauses recording. This API uses a promise to return the result. 2051 2052This API can be called only after the **start()** API is called. You can call **resume()** to resume recording. 2053 2054**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2055 2056**Return value** 2057 2058| Type | Description | 2059| -------------- | ------------------------------------- | 2060| Promise\<void> | Promise used to return the result.| 2061 2062**Error codes** 2063 2064For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2065 2066| ID| Error Message | 2067| -------- | -------------------------------------- | 2068| 5400102 | Operate not permit. Return by promise. | 2069| 5400103 | IO error. Return by promise. | 2070| 5400105 | Service died. Return by promise. | 2071 2072**Example** 2073 2074```js 2075avRecorder.pause().then(() => { 2076 console.info('pause AVRecorder success'); 2077}).catch((err) => { 2078 console.info('pause AVRecorder failed and catch error is ' + err.message); 2079}); 2080``` 2081 2082### resume<sup>9+</sup><a name=avrecorder_resume></a> 2083 2084resume(callback: AsyncCallback\<void>): void 2085 2086Resumes recording. This API uses an asynchronous callback to return the result. 2087 2088This API can be called only after the **pause()** API is called. 2089 2090**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2091 2092**Parameters** 2093 2094| Name | Type | Mandatory| Description | 2095| -------- | -------------------- | ---- | ---------------------------- | 2096| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2097 2098**Error codes** 2099 2100For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2101 2102| ID| Error Message | 2103| -------- | --------------------------------------- | 2104| 5400102 | Operate not permit. Return by callback. | 2105| 5400103 | IO error. Return by callback. | 2106| 5400105 | Service died. Return by callback. | 2107 2108**Example** 2109 2110```js 2111avRecorder.resume((err) => { 2112 if (err == null) { 2113 console.info('resume AVRecorder success'); 2114 } else { 2115 console.info('resume AVRecorder failed and error is ' + err.message); 2116 } 2117}); 2118``` 2119 2120### resume<sup>9+</sup> 2121 2122resume(): Promise\<void> 2123 2124Resumes recording. This API uses a promise to return the result. 2125 2126This API can be called only after the **pause()** API is called. 2127 2128**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2129 2130**Return value** 2131 2132| Type | Description | 2133| -------------- | ------------------------------------- | 2134| Promise\<void> | Promise used to return the result.| 2135 2136**Error codes** 2137 2138For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2139 2140| ID| Error Message | 2141| -------- | -------------------------------------- | 2142| 5400102 | Operate not permit. Return by promise. | 2143| 5400103 | IO error. Return by promise. | 2144| 5400105 | Service died. Return by promise. | 2145 2146**Example** 2147 2148```js 2149avRecorder.resume().then(() => { 2150 console.info('resume AVRecorder success'); 2151}).catch((err) => { 2152 console.info('resume AVRecorder failed and catch error is ' + err.message); 2153}); 2154``` 2155 2156### stop<sup>9+</sup><a name=avrecorder_stop></a> 2157 2158stop(callback: AsyncCallback\<void>): void 2159 2160Stops recording. This API uses an asynchronous callback to return the result. 2161 2162This API can be called only after the **start()** or **pause()** API is called. 2163 2164For audio-only recording, you can call **prepare()** again for re-recording. For video-only recording or audio and video recording, you can call **prepare()** and **getInputSurface()** again for re-recording. 2165 2166**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2167 2168**Parameters** 2169 2170| Name | Type | Mandatory| Description | 2171| -------- | -------------------- | ---- | ---------------------------- | 2172| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2173 2174**Error codes** 2175 2176For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2177 2178| ID| Error Message | 2179| -------- | --------------------------------------- | 2180| 5400102 | Operate not permit. Return by callback. | 2181| 5400103 | IO error. Return by callback. | 2182| 5400105 | Service died. Return by callback. | 2183 2184**Example** 2185 2186```js 2187avRecorder.stop((err) => { 2188 if (err == null) { 2189 console.info('stop AVRecorder success'); 2190 } else { 2191 console.info('stop AVRecorder failed and error is ' + err.message); 2192 } 2193}); 2194``` 2195 2196### stop<sup>9+</sup> 2197 2198stop(): Promise\<void> 2199 2200Stops recording. This API uses a promise to return the result. 2201 2202This API can be called only after the **start()** or **pause()** API is called. 2203 2204For audio-only recording, you can call **prepare()** again for re-recording. For video-only recording or audio and video recording, you can call **prepare()** and **getInputSurface()** again for re-recording. 2205 2206**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2207 2208**Return value** 2209 2210| Type | Description | 2211| -------------- | ------------------------------------- | 2212| Promise\<void> | Promise used to return the result.| 2213 2214**Error codes** 2215 2216For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2217 2218| ID| Error Message | 2219| -------- | -------------------------------------- | 2220| 5400102 | Operate not permit. Return by promise. | 2221| 5400103 | IO error. Return by promise. | 2222| 5400105 | Service died. Return by promise. | 2223 2224**Example** 2225 2226```js 2227avRecorder.stop().then(() => { 2228 console.info('stop AVRecorder success'); 2229}).catch((err) => { 2230 console.info('stop AVRecorder failed and catch error is ' + err.message); 2231}); 2232``` 2233 2234### reset<sup>9+</sup><a name=avrecorder_reset></a> 2235 2236reset(callback: AsyncCallback\<void>): void 2237 2238Resets audio and video recording. This API uses an asynchronous callback to return the result. 2239 2240For audio-only recording, you can call **prepare()** again for re-recording. For video-only recording or audio and video recording, you can call **prepare()** and **getInputSurface()** again for re-recording. 2241 2242**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2243 2244**Parameters** 2245 2246| Name | Type | Mandatory| Description | 2247| -------- | -------------------- | ---- | ------------------------------ | 2248| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2249 2250**Error codes** 2251 2252For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2253 2254| ID| Error Message | 2255| -------- | --------------------------------- | 2256| 5400103 | IO error. Return by callback. | 2257| 5400105 | Service died. Return by callback. | 2258 2259**Example** 2260 2261```js 2262avRecorder.reset((err) => { 2263 if (err == null) { 2264 console.info('reset AVRecorder success'); 2265 } else { 2266 console.info('reset AVRecorder failed and error is ' + err.message); 2267 } 2268}); 2269``` 2270 2271### reset<sup>9+</sup> 2272 2273reset(): Promise\<void> 2274 2275Resets audio and video recording. This API uses a promise to return the result. 2276 2277For audio-only recording, you can call **prepare()** again for re-recording. For video-only recording or audio and video recording, you can call **prepare()** and **getInputSurface()** again for re-recording. 2278 2279**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2280 2281**Return value** 2282 2283| Type | Description | 2284| -------------- | --------------------------------------- | 2285| Promise\<void> | Promise used to return the result.| 2286 2287**Error codes** 2288 2289For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2290 2291| ID| Error Message | 2292| -------- | -------------------------------- | 2293| 5400103 | IO error. Return by promise. | 2294| 5400105 | Service died. Return by promise. | 2295 2296**Example** 2297 2298```js 2299avRecorder.reset().then(() => { 2300 console.info('reset AVRecorder success'); 2301}).catch((err) => { 2302 console.info('reset AVRecorder failed and catch error is ' + err.message); 2303}); 2304``` 2305 2306### release<sup>9+</sup><a name=avrecorder_release></a> 2307 2308release(callback: AsyncCallback\<void>): void 2309 2310Releases the audio and video recording resources. This API uses an asynchronous callback to return the result. 2311 2312After the resources are released, you can no longer perform any operation on the **AVRecorder** instance. 2313 2314**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2315 2316**Parameters** 2317 2318| Name | Type | Mandatory| Description | 2319| -------- | -------------------- | ---- | ---------------------------------- | 2320| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2321 2322**Error codes** 2323 2324For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2325 2326| ID| Error Message | 2327| -------- | --------------------------------- | 2328| 5400105 | Service died. Return by callback. | 2329 2330**Example** 2331 2332```js 2333avRecorder.release((err) => { 2334 if (err == null) { 2335 console.info('release AVRecorder success'); 2336 } else { 2337 console.info('release AVRecorder failed and error is ' + err.message); 2338 } 2339}); 2340``` 2341 2342### release<sup>9+</sup> 2343 2344release(): Promise\<void> 2345 2346Releases the audio and video recording resources. This API uses a promise to return the result. 2347 2348After the resources are released, you can no longer perform any operation on the **AVRecorder** instance. 2349 2350**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2351 2352**Return value** 2353 2354| Type | Description | 2355| -------------- | ------------------------------------------- | 2356| Promise\<void> | Promise used to return the result.| 2357 2358**Error codes** 2359 2360For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2361 2362| ID| Error Message | 2363| -------- | --------------------------------- | 2364| 5400105 | Service died. Return by callback. | 2365 2366**Example** 2367 2368```js 2369avRecorder.release().then(() => { 2370 console.info('release AVRecorder success'); 2371}).catch((err) => { 2372 console.info('release AVRecorder failed and catch error is ' + err.message); 2373}); 2374``` 2375 2376### on('stateChange')<sup>9+</sup><a name=avrecorder_onstatechange></a> 2377 2378on(type: 'stateChange', callback: (state: AVRecorderState, reason: StateChangeReason) => void): void 2379 2380Subscribes to AVRecorder state changes. An application can subscribe to only one AVRecorder state change event. When the application initiates multiple subscriptions to this event, the last subscription prevails. 2381 2382**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2383 2384**Parameters** 2385 2386| Name | Type | Mandatory| Description | 2387| -------- | -------- | ---- | ------------------------------------------------------------ | 2388| type | string | Yes | Event type, which is **'stateChange'** in this case. This event can be triggered by both user operations and the system.| 2389| callback | function | Yes | Callback invoked when the event is triggered. It reports the following information:<br>**state**: [AVRecorderState](#avrecorderstate9), indicating the AVRecorder state.<br>**reason**: [StateChangeReason](#statechangereason9), indicating the reason for the state transition.| 2390 2391**Example** 2392 2393```js 2394avRecorder.on('stateChange', async (state, reason) => { 2395 console.info('case state has changed, new state is :' + state + ',and new reason is : ' + reason); 2396}); 2397``` 2398 2399### off('stateChange')<sup>9+</sup> 2400 2401off(type: 'stateChange'): void 2402 2403Unsubscribes from AVRecorder state changes. 2404 2405**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2406 2407**Parameters** 2408 2409| Name| Type | Mandatory| Description | 2410| ------ | ------ | ---- | ------------------------------------------------------------ | 2411| type | string | Yes | Event type, which is **'stateChange'** in this case. This event can be triggered by both user operations and the system.| 2412 2413**Example** 2414 2415```js 2416avRecorder.off('stateChange'); 2417``` 2418 2419### on('error')<sup>9+</sup><a name=avrecorder_onerror></a> 2420 2421on(type: 'error', callback: ErrorCallback): void 2422 2423Subscribes to AVRecorder errors. This event is used only for error prompt and does not require the user to stop recording control. If the [AVRecorderState](#avrecorderstate9) is also switched to error, call **reset()** or **release()** to exit the recording. 2424 2425An application can subscribe to only one AVRecorder error event. When the application initiates multiple subscriptions to this event, the last subscription prevails. 2426 2427**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2428 2429**Parameters** 2430 2431| Name | Type | Mandatory| Description | 2432| -------- | ------------- | ---- | ------------------------------------------------------------ | 2433| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during recording.| 2434| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 2435 2436**Error codes** 2437 2438For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2439 2440| ID| Error Message | 2441| -------- | --------------------------------- | 2442| 5400103 | IO error. Return by callback. | 2443| 5400105 | Service died. Return by callback. | 2444 2445**Example** 2446 2447```js 2448avRecorder.on('error', (err) => { 2449 console.info('case avRecorder.on(error) called, errMessage is ' + err.message); 2450}); 2451``` 2452 2453### off('error')<sup>9+</sup> 2454 2455off(type: 'error'): void 2456 2457Unsubscribes from AVRecorder errors. After the unsubscription, your application can no longer receive AVRecorder errors. 2458 2459**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2460 2461**Parameters** 2462 2463| Name| Type | Mandatory| Description | 2464| ------ | ------ | ---- | ------------------------------------------------------------ | 2465| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during recording.| 2466 2467**Error codes** 2468 2469For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2470 2471| ID| Error Message | 2472| -------- | --------------------------------- | 2473| 5400103 | IO error. Return by callback. | 2474| 5400105 | Service died. Return by callback. | 2475 2476**Example** 2477 2478```js 2479avRecorder.off('error'); 2480``` 2481 2482## AVRecorderState<sup>9+</sup> 2483 2484Enumerates the AVRecorder states. You can obtain the state through the **state** attribute. 2485 2486**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2487 2488| Name | Type | Description | 2489| -------- | ------ | ------------------------------------------------------------ | 2490| idle | string | The AVRecorder enters this state when the AVRecorder is just created or the [reset()](#avrecorder_reset) API is called in any non-released state. In this state, you can call [prepare()](#avrecorder_prepare) to set recording parameters. | 2491| prepared | string | The AVRecorder enters this state when the parameters are set. In this state, you can call [start()](#avrecorder_start) to start recording.| 2492| started | string | The AVRecorder enters this state when the recording starts. In this state, you can call [pause()](#avrecorder_pause) to pause the recording or call [stop()](#avrecorder_stop) to stop recording.| 2493| paused | string | The AVRecorder enters this state when the recording is paused. In this state, you can call [resume()](#avrecorder_resume) to continue the recording or call [stop()](#avrecorder_stop) to stop recording.| 2494| stopped | string | The AVRecorder enters this state when the recording stops. In this state, you can call [prepare()](#avrecorder_prepare) to set recording parameters.| 2495| released | string | The AVRecorder enters this state when the recording resources are released. In this state, no operation can be performed. In any other state, you can call [release()](#avrecorder_release) to enter the released state.| 2496| error | string | The AVRecorder enters this state when an irreversible error occurs in the **AVRecorder** instance. In this state, the [on('error') event](#avrecorder_onerror) is reported, with the detailed error cause. In the error state, you must call [reset()](#avrecorder_reset) to reset the **AVRecorder** instance or call [release()](#avrecorder_release) to release the resources.| 2497 2498## AVRecorderConfig<sup>9+</sup> 2499 2500Describes the audio and video recording parameters. 2501 2502**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2503 2504| Name | Type | Mandatory| Description | 2505| --------------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | 2506| audioSourceType | [AudioSourceType](#audiosourcetype9) | No | Type of the audio source to record. This parameter is mandatory for audio recording. | 2507| videoSourceType | [VideoSourceType](#videosourcetype9) | No | Type of the video source to record. This parameter is mandatory for video recording. | 2508| profile | [AVRecorderProfile](#avrecorderprofile9) | Yes | Recording profile. This parameter is mandatory. | 2509| url | string | Yes | Recording output URL: fd://xx (fd number).<br>![img](figures/en-us_image_url.png)<br>This parameter is mandatory.| 2510| rotation | number | No | Rotation angle of the recorded video. The value can only be 0, 90, 180, or 270. | 2511| location | [Location](#location) | No | Geographical location of the recorded video. | 2512 2513The **audioSourceType** and **videoSourceType** parameters are used to distinguish audio-only recording, video-only recording, and audio and video recording. For audio-only recording, set only **audioSourceType**. For video-only recording, set only **videoSourceType**. For audio and video recording, set both **audioSourceType** and **videoSourceType**. 2514 2515## AVRecorderProfile<sup>9+</sup> 2516 2517Describes the audio and video recording profile. 2518 2519**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2520 2521| Name | Type | Mandatory| Description | 2522| ---------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 2523| audioBitrate | number | No | Audio encoding bit rate. This parameter is mandatory for audio recording. | 2524| audioChannels | number | No | Number of audio channels. This parameter is mandatory for audio recording. | 2525| audioCodec | [CodecMimeType](#codecmimetype8) | No | Audio encoding format. This parameter is mandatory for audio recording. Only **AUDIO_AAC** is supported. | 2526| audioSampleRate | number | No | Audio sampling rate. This parameter is mandatory for audio recording. | 2527| fileFormat | [ContainerFormatType](#containerformattype8) | Yes | Container format of a file. This parameter is mandatory. | 2528| videoBitrate | number | No | Video encoding bit rate. This parameter is mandatory for video recording. | 2529| videoCodec | [CodecMimeType](#codecmimetype8) | No | Video encoding format. This parameter is mandatory for video recording. You need to query the encoding capabilities (including the encoding format and resolution) supported by the device.| 2530| videoFrameWidth | number | No | Width of a video frame. This parameter is mandatory for video recording. | 2531| videoFrameHeight | number | No | Height of a video frame. This parameter is mandatory for video recording. | 2532| videoFrameRate | number | No | Video frame rate. This parameter is mandatory for video recording. | 2533 2534## AudioSourceType<sup>9+</sup> 2535 2536Enumerates the audio source types for video recording. 2537 2538**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2539 2540| Name | Value | Description | 2541| ------------------------- | ---- | ---------------------- | 2542| AUDIO_SOURCE_TYPE_DEFAULT | 0 | Default audio input source.| 2543| AUDIO_SOURCE_TYPE_MIC | 1 | Mic audio input source. | 2544 2545## VideoSourceType<sup>9+</sup> 2546 2547Enumerates the video source types for video recording. 2548 2549**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2550 2551| Name | Value | Description | 2552| ----------------------------- | ---- | ------------------------------- | 2553| VIDEO_SOURCE_TYPE_SURFACE_YUV | 0 | The input surface carries raw data.| 2554| VIDEO_SOURCE_TYPE_SURFACE_ES | 1 | The input surface carries ES data. | 2555 2556## ContainerFormatType<sup>8+</sup> 2557 2558Enumerates the container format types (CFTs). 2559 2560**System capability**: SystemCapability.Multimedia.Media.Core 2561 2562| Name | Value | Description | 2563| ----------- | ----- | --------------------- | 2564| CFT_MPEG_4 | 'mp4' | Video container format MP4.| 2565| CFT_MPEG_4A | 'm4a' | Audio container format M4A.| 2566 2567## Location 2568 2569Describes the geographical location of the recorded video. 2570 2571**System capability**: SystemCapability.Multimedia.Media.Core 2572 2573| Name | Type | Mandatory| Description | 2574| --------- | ------ | ---- | ---------------- | 2575| latitude | number | Yes | Latitude of the geographical location.| 2576| longitude | number | Yes | Longitude of the geographical location.| 2577 2578## VideoRecorder<sup>9+</sup> 2579 2580> **NOTE** 2581> 2582> This class is deprecated after AVRecorder<sup>9+</sup> is released. You are advised to use [AVRecorder](#avrecorder9) instead. 2583 2584Implements video recording. Before calling any API in the **VideoRecorder** class, you must use [createVideoRecorder()](#mediacreatevideorecorder9) to create a [VideoRecorder](#videorecorder9) instance. 2585 2586### Attributes 2587 2588**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 2589 2590**System API**: This is a system API. 2591 2592| Name | Type | Readable| Writable| Description | 2593| ------------------ | -------------------------------------- | ---- | ---- | ---------------- | 2594| state<sup>9+</sup> | [VideoRecordState](#videorecordstate9) | Yes | No | Video recording state.| 2595 2596### prepare<sup>9+</sup><a name=videorecorder_prepare1></a> 2597 2598prepare(config: VideoRecorderConfig, callback: AsyncCallback\<void>): void; 2599 2600Sets video recording parameters. This API uses an asynchronous callback to return the result. 2601 2602**Required permissions:** ohos.permission.MICROPHONE 2603 2604**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 2605 2606**System API**: This is a system API. 2607 2608**Parameters** 2609 2610| Name | Type | Mandatory| Description | 2611| -------- | -------------------------------------------- | ---- | ----------------------------------- | 2612| config | [VideoRecorderConfig](#videorecorderconfig9) | Yes | Video recording parameters to set. | 2613| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2614 2615**Error codes** 2616 2617For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2618 2619| ID| Error Message | 2620| -------- | ------------------------------------------ | 2621| 201 | Permission denied. Return by callback. | 2622| 401 | Parameter error. Return by callback. | 2623| 5400102 | Operation not allowed. Return by callback. | 2624| 5400105 | Service died. Return by callback. | 2625 2626**Example** 2627 2628```js 2629// Configure the parameters based on those supported by the hardware device. 2630let videoProfile = { 2631 audioBitrate : 48000, 2632 audioChannels : 2, 2633 audioCodec : 'audio/mp4a-latm', 2634 audioSampleRate : 48000, 2635 fileFormat : 'mp4', 2636 videoBitrate : 2000000, 2637 videoCodec : 'video/avc', 2638 videoFrameWidth : 640, 2639 videoFrameHeight : 480, 2640 videoFrameRate : 30 2641} 2642 2643let videoConfig = { 2644 audioSourceType : 1, 2645 videoSourceType : 0, 2646 profile : videoProfile, 2647 url : 'fd://xx', // The file must be created by the caller and granted with proper permissions. 2648 orientationHint : 0, 2649 location : { latitude : 30, longitude : 130 }, 2650} 2651 2652// asyncallback 2653videoRecorder.prepare(videoConfig, (err) => { 2654 if (err == null) { 2655 console.info('prepare success'); 2656 } else { 2657 console.info('prepare failed and error is ' + err.message); 2658 } 2659}) 2660``` 2661 2662### prepare<sup>9+</sup><a name=videorecorder_prepare2></a> 2663 2664prepare(config: VideoRecorderConfig): Promise\<void>; 2665 2666Sets video recording parameters. This API uses a promise to return the result. 2667 2668**Required permissions:** ohos.permission.MICROPHONE 2669 2670**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 2671 2672**System API**: This is a system API. 2673 2674**Parameters** 2675 2676| Name| Type | Mandatory| Description | 2677| ------ | -------------------------------------------- | ---- | ------------------------ | 2678| config | [VideoRecorderConfig](#videorecorderconfig9) | Yes | Video recording parameters to set.| 2679 2680**Return value** 2681 2682| Type | Description | 2683| -------------- | ---------------------------------------- | 2684| Promise\<void> | Promise used to return the result.| 2685 2686**Error codes** 2687 2688For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2689 2690| ID| Error Message | 2691| -------- | ----------------------------------------- | 2692| 201 | Permission denied. Return by promise. | 2693| 401 | Parameter error. Return by promise. | 2694| 5400102 | Operation not allowed. Return by promise. | 2695| 5400105 | Service died. Return by promise. | 2696 2697**Example** 2698 2699```js 2700// Configure the parameters based on those supported by the hardware device. 2701let videoProfile = { 2702 audioBitrate : 48000, 2703 audioChannels : 2, 2704 audioCodec : 'audio/mp4a-latm', 2705 audioSampleRate : 48000, 2706 fileFormat : 'mp4', 2707 videoBitrate : 2000000, 2708 videoCodec : 'video/avc', 2709 videoFrameWidth : 640, 2710 videoFrameHeight : 480, 2711 videoFrameRate : 30 2712} 2713 2714let videoConfig = { 2715 audioSourceType : 1, 2716 videoSourceType : 0, 2717 profile : videoProfile, 2718 url : 'fd://xx', // The file must be created by the caller and granted with proper permissions. 2719 orientationHint : 0, 2720 location : { latitude : 30, longitude : 130 }, 2721} 2722 2723// promise 2724videoRecorder.prepare(videoConfig).then(() => { 2725 console.info('prepare success'); 2726}).catch((err) => { 2727 console.info('prepare failed and catch error is ' + err.message); 2728}); 2729``` 2730 2731### getInputSurface<sup>9+</sup> 2732 2733getInputSurface(callback: AsyncCallback\<string>): void; 2734 2735Obtains the surface required for recording. This API uses an asynchronous callback to return the result. The caller obtains the **surfaceBuffer** from this surface and fills in the corresponding data. 2736 2737Note that the video data must carry the timestamp (in ns) and buffer size, and the start time of the timestamp must be based on the system startup time. 2738 2739This API can be called only after [prepare()](#videorecorder_prepare1) is called. 2740 2741**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 2742 2743**System API**: This is a system API. 2744 2745**Parameters** 2746 2747| Name | Type | Mandatory| Description | 2748| -------- | ---------------------- | ---- | --------------------------- | 2749| callback | AsyncCallback\<string> | Yes | Callback used to obtain the result.| 2750 2751**Error codes** 2752 2753For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2754 2755| ID| Error Message | 2756| -------- | ------------------------------------------ | 2757| 5400102 | Operation not allowed. Return by callback. | 2758| 5400103 | I/O error. Return by callback. | 2759| 5400105 | Service died. Return by callback. | 2760 2761**Example** 2762 2763```js 2764// asyncallback 2765let surfaceID = null; // Surface ID passed to the external system. 2766videoRecorder.getInputSurface((err, surfaceId) => { 2767 if (err == null) { 2768 console.info('getInputSurface success'); 2769 surfaceID = surfaceId; 2770 } else { 2771 console.info('getInputSurface failed and error is ' + err.message); 2772 } 2773}); 2774``` 2775 2776### getInputSurface<sup>9+</sup> 2777 2778getInputSurface(): Promise\<string>; 2779 2780 Obtains the surface required for recording. This API uses a promise to return the result. The caller obtains the **surfaceBuffer** from this surface and fills in the corresponding data. 2781 2782Note that the video data must carry the timestamp (in ns) and buffer size, and the start time of the timestamp must be based on the system startup time. 2783 2784This API can be called only after [prepare()](#videorecorder_prepare1) is called. 2785 2786**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 2787 2788**System API**: This is a system API. 2789 2790**Return value** 2791 2792| Type | Description | 2793| ---------------- | -------------------------------- | 2794| Promise\<string> | Promise used to return the result.| 2795 2796**Error codes** 2797 2798For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2799 2800| ID| Error Message | 2801| -------- | ----------------------------------------- | 2802| 5400102 | Operation not allowed. Return by promise. | 2803| 5400103 | I/O error. Return by promise. | 2804| 5400105 | Service died. Return by promise. | 2805 2806**Example** 2807 2808```js 2809// promise 2810let surfaceID = null; // Surface ID passed to the external system. 2811videoRecorder.getInputSurface().then((surfaceId) => { 2812 console.info('getInputSurface success'); 2813 surfaceID = surfaceId; 2814}).catch((err) => { 2815 console.info('getInputSurface failed and catch error is ' + err.message); 2816}); 2817``` 2818 2819### start<sup>9+</sup><a name=videorecorder_start1></a> 2820 2821start(callback: AsyncCallback\<void>): void; 2822 2823Starts video recording. This API uses an asynchronous callback to return the result. 2824 2825This API can be called only after [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface9) are called, because the data source must pass data to the surface first. 2826 2827**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 2828 2829**System API**: This is a system API. 2830 2831**Parameters** 2832 2833| Name | Type | Mandatory| Description | 2834| -------- | -------------------- | ---- | ---------------------------- | 2835| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2836 2837**Error codes** 2838 2839For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2840 2841| ID| Error Message | 2842| -------- | ------------------------------------------ | 2843| 5400102 | Operation not allowed. Return by callback. | 2844| 5400103 | I/O error. Return by callback. | 2845| 5400105 | Service died. Return by callback. | 2846 2847**Example** 2848 2849```js 2850// asyncallback 2851videoRecorder.start((err) => { 2852 if (err == null) { 2853 console.info('start videorecorder success'); 2854 } else { 2855 console.info('start videorecorder failed and error is ' + err.message); 2856 } 2857}); 2858``` 2859 2860### start<sup>9+</sup><a name=videorecorder_start2></a> 2861 2862start(): Promise\<void>; 2863 2864Starts video recording. This API uses a promise to return the result. 2865 2866This API can be called only after [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface9) are called, because the data source must pass data to the surface first. 2867 2868**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 2869 2870**System API**: This is a system API. 2871 2872**Return value** 2873 2874| Type | Description | 2875| -------------- | ------------------------------------- | 2876| Promise\<void> | Promise used to return the result.| 2877 2878**Error codes** 2879 2880For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2881 2882| ID| Error Message | 2883| -------- | ----------------------------------------- | 2884| 5400102 | Operation not allowed. Return by promise. | 2885| 5400103 | I/O error. Return by promise. | 2886| 5400105 | Service died. Return by promise. | 2887 2888**Example** 2889 2890```js 2891// promise 2892videoRecorder.start().then(() => { 2893 console.info('start videorecorder success'); 2894}).catch((err) => { 2895 console.info('start videorecorder failed and catch error is ' + err.message); 2896}); 2897``` 2898 2899### pause<sup>9+</sup><a name=videorecorder_pause1></a> 2900 2901pause(callback: AsyncCallback\<void>): void; 2902 2903Pauses video recording. This API uses an asynchronous callback to return the result. 2904 2905This API can be called only after [start()](#videorecorder_start1) is called. You can resume recording by calling [resume()](#videorecorder_resume1). 2906 2907**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 2908 2909**System API**: This is a system API. 2910 2911**Parameters** 2912 2913| Name | Type | Mandatory| Description | 2914| -------- | -------------------- | ---- | ---------------------------- | 2915| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2916 2917**Error codes** 2918 2919For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2920 2921| ID| Error Message | 2922| -------- | ------------------------------------------ | 2923| 5400102 | Operation not allowed. Return by callback. | 2924| 5400103 | I/O error. Return by callback. | 2925| 5400105 | Service died. Return by callback. | 2926 2927**Example** 2928 2929```js 2930// asyncallback 2931videoRecorder.pause((err) => { 2932 if (err == null) { 2933 console.info('pause videorecorder success'); 2934 } else { 2935 console.info('pause videorecorder failed and error is ' + err.message); 2936 } 2937}); 2938``` 2939 2940### pause<sup>9+</sup><a name=videorecorder_pause2></a> 2941 2942pause(): Promise\<void>; 2943 2944Pauses video recording. This API uses a promise to return the result. 2945 2946This API can be called only after [start()](#videorecorder_start1) is called. You can resume recording by calling [resume()](#videorecorder_resume1). 2947 2948**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 2949 2950**System API**: This is a system API. 2951 2952**Return value** 2953 2954| Type | Description | 2955| -------------- | ------------------------------------- | 2956| Promise\<void> | Promise used to return the result.| 2957 2958**Error codes** 2959 2960For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2961 2962| ID| Error Message | 2963| -------- | ----------------------------------------- | 2964| 5400102 | Operation not allowed. Return by promise. | 2965| 5400103 | I/O error. Return by promise. | 2966| 5400105 | Service died. Return by promise. | 2967 2968**Example** 2969 2970```js 2971// promise 2972videoRecorder.pause().then(() => { 2973 console.info('pause videorecorder success'); 2974}).catch((err) => { 2975 console.info('pause videorecorder failed and catch error is ' + err.message); 2976}); 2977``` 2978 2979### resume<sup>9+</sup><a name=videorecorder_resume1></a> 2980 2981resume(callback: AsyncCallback\<void>): void; 2982 2983Resumes video recording. This API uses an asynchronous callback to return the result. 2984 2985**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 2986 2987**System API**: This is a system API. 2988 2989**Parameters** 2990 2991| Name | Type | Mandatory| Description | 2992| -------- | -------------------- | ---- | ---------------------------- | 2993| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2994 2995**Error codes** 2996 2997For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2998 2999| ID| Error Message | 3000| -------- | ------------------------------------------ | 3001| 5400102 | Operation not allowed. Return by callback. | 3002| 5400103 | I/O error. Return by callback. | 3003| 5400105 | Service died. Return by callback. | 3004 3005**Example** 3006 3007```js 3008// asyncallback 3009videoRecorder.resume((err) => { 3010 if (err == null) { 3011 console.info('resume videorecorder success'); 3012 } else { 3013 console.info('resume videorecorder failed and error is ' + err.message); 3014 } 3015}); 3016``` 3017 3018### resume<sup>9+</sup><a name=videorecorder_resume2></a> 3019 3020resume(): Promise\<void>; 3021 3022Resumes video recording. This API uses a promise to return the result. 3023 3024**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3025 3026**System API**: This is a system API. 3027 3028**Return value** 3029 3030| Type | Description | 3031| -------------- | ------------------------------------- | 3032| Promise\<void> | Promise used to return the result.| 3033 3034**Error codes** 3035 3036For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3037 3038| ID| Error Message | 3039| -------- | ----------------------------------------- | 3040| 5400102 | Operation not allowed. Return by promise. | 3041| 5400103 | I/O error. Return by promise. | 3042| 5400105 | Service died. Return by promise. | 3043 3044**Example** 3045 3046```js 3047// promise 3048videoRecorder.resume().then(() => { 3049 console.info('resume videorecorder success'); 3050}).catch((err) => { 3051 console.info('resume videorecorder failed and catch error is ' + err.message); 3052}); 3053``` 3054 3055### stop<sup>9+</sup><a name=videorecorder_stop1></a> 3056 3057stop(callback: AsyncCallback\<void>): void; 3058 3059Stops video recording. This API uses an asynchronous callback to return the result. 3060 3061To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface9) again. 3062 3063**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3064 3065**System API**: This is a system API. 3066 3067**Parameters** 3068 3069| Name | Type | Mandatory| Description | 3070| -------- | -------------------- | ---- | ---------------------------- | 3071| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3072 3073**Error codes** 3074 3075For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3076 3077| ID| Error Message | 3078| -------- | ------------------------------------------ | 3079| 5400102 | Operation not allowed. Return by callback. | 3080| 5400103 | I/O error. Return by callback. | 3081| 5400105 | Service died. Return by callback. | 3082 3083**Example** 3084 3085```js 3086// asyncallback 3087videoRecorder.stop((err) => { 3088 if (err == null) { 3089 console.info('stop videorecorder success'); 3090 } else { 3091 console.info('stop videorecorder failed and error is ' + err.message); 3092 } 3093}); 3094``` 3095 3096### stop<sup>9+</sup><a name=videorecorder_stop2></a> 3097 3098stop(): Promise\<void>; 3099 3100Stops video recording. This API uses a promise to return the result. 3101 3102To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface9) again. 3103 3104**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3105 3106**System API**: This is a system API. 3107 3108**Return value** 3109 3110| Type | Description | 3111| -------------- | ------------------------------------- | 3112| Promise\<void> | Promise used to return the result.| 3113 3114**Error codes** 3115 3116For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3117 3118| ID| Error Message | 3119| -------- | ----------------------------------------- | 3120| 5400102 | Operation not allowed. Return by promise. | 3121| 5400103 | I/O error. Return by promise. | 3122| 5400105 | Service died. Return by promise. | 3123 3124**Example** 3125 3126```js 3127// promise 3128videoRecorder.stop().then(() => { 3129 console.info('stop videorecorder success'); 3130}).catch((err) => { 3131 console.info('stop videorecorder failed and catch error is ' + err.message); 3132}); 3133``` 3134 3135### release<sup>9+</sup><a name=videorecorder_release1></a> 3136 3137release(callback: AsyncCallback\<void>): void; 3138 3139Releases the video recording resources. This API uses an asynchronous callback to return the result. 3140 3141**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3142 3143**System API**: This is a system API. 3144 3145**Parameters** 3146 3147| Name | Type | Mandatory| Description | 3148| -------- | -------------------- | ---- | -------------------------------- | 3149| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3150 3151**Error codes** 3152 3153For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3154 3155| ID| Error Message | 3156| -------- | --------------------------------- | 3157| 5400105 | Service died. Return by callback. | 3158 3159**Example** 3160 3161```js 3162// asyncallback 3163videoRecorder.release((err) => { 3164 if (err == null) { 3165 console.info('release videorecorder success'); 3166 } else { 3167 console.info('release videorecorder failed and error is ' + err.message); 3168 } 3169}); 3170``` 3171 3172### release<sup>9+</sup><a name=videorecorder_release2></a> 3173 3174release(): Promise\<void>; 3175 3176Releases the video recording resources. This API uses a promise to return the result. 3177 3178**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3179 3180**System API**: This is a system API. 3181 3182**Return value** 3183 3184| Type | Description | 3185| -------------- | ----------------------------------------- | 3186| Promise\<void> | Promise used to return the result.| 3187 3188**Error codes** 3189 3190For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3191 3192| ID| Error Message | 3193| -------- | --------------------------------- | 3194| 5400105 | Service died. Return by callback. | 3195 3196**Example** 3197 3198```js 3199// promise 3200videoRecorder.release().then(() => { 3201 console.info('release videorecorder success'); 3202}).catch((err) => { 3203 console.info('release videorecorder failed and catch error is ' + err.message); 3204}); 3205``` 3206 3207### reset<sup>9+</sup><a name=videorecorder_reset1></a> 3208 3209reset(callback: AsyncCallback\<void>): void; 3210 3211Resets video recording. This API uses an asynchronous callback to return the result. 3212 3213To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface9) again. 3214 3215**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3216 3217**System API**: This is a system API. 3218 3219**Parameters** 3220 3221| Name | Type | Mandatory| Description | 3222| -------- | -------------------- | ---- | ---------------------------- | 3223| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3224 3225**Error codes** 3226 3227For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3228 3229| ID| Error Message | 3230| -------- | --------------------------------- | 3231| 5400103 | I/O error. Return by callback. | 3232| 5400105 | Service died. Return by callback. | 3233 3234**Example** 3235 3236```js 3237// asyncallback 3238videoRecorder.reset((err) => { 3239 if (err == null) { 3240 console.info('reset videorecorder success'); 3241 } else { 3242 console.info('reset videorecorder failed and error is ' + err.message); 3243 } 3244}); 3245``` 3246 3247### reset<sup>9+</sup><a name=videorecorder_reset2></a> 3248 3249reset(): Promise\<void>; 3250 3251Resets video recording. This API uses a promise to return the result. 3252 3253To start another recording, you must call [prepare()](#videorecorder_prepare1) and [getInputSurface()](#getinputsurface9) again. 3254 3255**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3256 3257**System API**: This is a system API. 3258 3259**Return value** 3260 3261| Type | Description | 3262| -------------- | ------------------------------------- | 3263| Promise\<void> | Promise used to return the result.| 3264 3265**Error codes** 3266 3267For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3268 3269| ID| Error Message | 3270| -------- | -------------------------------- | 3271| 5400103 | I/O error. Return by promise. | 3272| 5400105 | Service died. Return by promise. | 3273 3274**Example** 3275 3276```js 3277// promise 3278videoRecorder.reset().then(() => { 3279 console.info('reset videorecorder success'); 3280}).catch((err) => { 3281 console.info('reset videorecorder failed and catch error is ' + err.message); 3282}); 3283``` 3284 3285### on('error')<sup>9+</sup> 3286 3287on(type: 'error', callback: ErrorCallback): void 3288 3289Subscribes to video recording error events. After an error event is reported, you must handle the event and exit the recording. 3290 3291**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3292 3293**Parameters** 3294 3295| Name | Type | Mandatory| Description | 3296| -------- | ------------- | ---- | ------------------------------------------------------------ | 3297| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during video recording.| 3298| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 3299 3300**Error codes** 3301 3302For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3303 3304| ID| Error Message | 3305| -------- | --------------------------------- | 3306| 5400103 | I/O error. Return by callback. | 3307| 5400105 | Service died. Return by callback. | 3308 3309**Example** 3310 3311```js 3312// This event is reported when an error occurs during the retrieval of videoRecordState. 3313videoRecorder.on('error', (error) => { // Set the 'error' event callback. 3314 console.info(`audio error called, error: ${error}`); 3315}) 3316``` 3317 3318## VideoRecordState<sup>9+</sup> 3319 3320Enumerates the video recording states. You can obtain the state through the **state** attribute. 3321 3322**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3323 3324**System API**: This is a system API. 3325 3326| Name | Type | Description | 3327| -------- | ------ | ---------------------- | 3328| idle | string | The video recorder is idle. | 3329| prepared | string | The video recording parameters are set.| 3330| playing | string | Video recording is in progress. | 3331| paused | string | Video recording is paused. | 3332| stopped | string | Video recording is stopped. | 3333| error | string | Video recording is in the error state. | 3334 3335## VideoRecorderConfig<sup>9+</sup> 3336 3337Describes the video recording parameters. 3338 3339**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3340 3341**System API**: This is a system API. 3342 3343| Name | Type | Mandatory| Description | 3344| --------------- | ---------------------------------------------- | ---- | ------------------------------------------------------------ | 3345| audioSourceType | [AudioSourceType](#audiosourcetype9) | Yes | Type of the audio source for video recording. | 3346| videoSourceType | [VideoSourceType](#videosourcetype9) | Yes | Type of the video source for video recording. | 3347| profile | [VideoRecorderProfile](#videorecorderprofile9) | Yes | Video recording profile. | 3348| rotation | number | No | Rotation angle of the recorded video. | 3349| location | [Location](#location) | No | Geographical location of the recorded video. | 3350| url | string | Yes | Video output URL. Supported: fd://xx (fd number)<br>![](figures/en-us_image_url.png) | 3351 3352## VideoRecorderProfile<sup>9+</sup> 3353 3354Describes the video recording profile. 3355 3356**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3357 3358**System API**: This is a system API. 3359 3360| Name | Type | Mandatory| Description | 3361| ---------------- | -------------------------------------------- | ---- | ---------------- | 3362| audioBitrate | number | Yes | Audio encoding bit rate.| 3363| audioChannels | number | Yes | Number of audio channels.| 3364| audioCodec | [CodecMimeType](#codecmimetype8) | Yes | Audio encoding format. | 3365| audioSampleRate | number | Yes | Audio sampling rate. | 3366| fileFormat | [ContainerFormatType](#containerformattype8) | Yes | Container format of a file.| 3367| videoBitrate | number | Yes | Video encoding bit rate.| 3368| videoCodec | [CodecMimeType](#codecmimetype8) | Yes | Video encoding format. | 3369| videoFrameWidth | number | Yes | Width of the recorded video frame.| 3370| videoFrameHeight | number | Yes | Height of the recorded video frame.| 3371| videoFrameRate | number | Yes | Video frame rate. | 3372 3373## media.createAudioPlayer<sup>(deprecated)</sup><a name=createaudioplayer></a> 3374 3375createAudioPlayer(): AudioPlayer 3376 3377Creates an **AudioPlayer** instance in synchronous mode. 3378 3379> **NOTE** 3380> 3381> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [createAVPlayer](#mediacreateavplayer9) instead. 3382 3383**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3384 3385**Return value** 3386 3387| Type | Description | 3388| --------------------------- | ------------------------------------------------------------ | 3389| [AudioPlayer](#audioplayerdeprecated) | If the operation is successful, an **AudioPlayer** instance is returned; otherwise, **null** is returned. After the instance is created, you can start, pause, or stop audio playback.| 3390 3391**Example** 3392 3393```js 3394let audioPlayer = media.createAudioPlayer(); 3395``` 3396 3397## media.createVideoPlayer<sup>(deprecated)</sup><a name=createvideoplayer></a> 3398 3399createVideoPlayer(callback: AsyncCallback\<VideoPlayer>): void 3400 3401Creates a **VideoPlayer** instance. This API uses an asynchronous callback to return the result. 3402 3403> **NOTE** 3404> 3405> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [createAVPlayer](#mediacreateavplayer9) instead. 3406 3407**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 3408 3409**Parameters** 3410 3411| Name | Type | Mandatory| Description | 3412| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 3413| callback | AsyncCallback<[VideoPlayer](#videoplayerdeprecated)> | Yes | Callback used to return the result. If the operation is successful, a **VideoPlayer** instance is returned; otherwise, **null** is returned. The instance can be used to manage and play video.| 3414 3415**Example** 3416 3417```js 3418let videoPlayer 3419 3420media.createVideoPlayer((error, video) => { 3421 if (video != null) { 3422 videoPlayer = video; 3423 console.info('video createVideoPlayer success'); 3424 } else { 3425 console.info(`video createVideoPlayer fail, error:${error}`); 3426 } 3427}); 3428``` 3429 3430## media.createVideoPlayer<sup>(deprecated)</sup> 3431 3432createVideoPlayer(): Promise\<VideoPlayer> 3433 3434Creates a **VideoPlayer** instance. This API uses a promise to return the result. 3435 3436> **NOTE** 3437> 3438> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [createAVPlayer](#mediacreateavplayer9-1) instead. 3439 3440**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 3441 3442**Return value** 3443 3444| Type | Description | 3445| ------------------------------------ | ------------------------------------------------------------ | 3446| Promise<[VideoPlayer](#videoplayerdeprecated)> | Promise used to return the result. If the operation is successful, a **VideoPlayer** instance is returned; otherwise, **null** is returned. The instance can be used to manage and play video.| 3447 3448**Example** 3449 3450```js 3451let videoPlayer 3452 3453media.createVideoPlayer().then((video) => { 3454 if (video != null) { 3455 videoPlayer = video; 3456 console.info('video createVideoPlayer success'); 3457 } else { 3458 console.info('video createVideoPlayer fail'); 3459 } 3460}).catch((error) => { 3461 console.info(`video catchCallback, error:${error}`); 3462}); 3463``` 3464 3465## media.createAudioRecorder<sup>(deprecated)</sup><a name=createaudiorecorder></a> 3466 3467createAudioRecorder(): AudioRecorder 3468 3469Creates an **AudioRecorder** instance to control audio recording. 3470Only one **AudioRecorder** instance can be created per device. 3471 3472> **NOTE** 3473> 3474> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [createAVRecorder](#mediacreateavrecorder9) instead. 3475 3476**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 3477 3478**Return value** 3479 3480| Type | Description | 3481| ------------------------------- | ------------------------------------------------------------ | 3482| [AudioRecorder](#audiorecorderdeprecated) | If the operation is successful, the **AudioRecorder** instance is returned; otherwise, **null** is returned. The instance can be used to record audio.| 3483 3484**Example** 3485 3486```js 3487let audioRecorder = media.createAudioRecorder(); 3488``` 3489 3490## MediaErrorCode<sup>(deprecated)</sup><a name=mediaerrorcode></a> 3491 3492Enumerates the media error codes. 3493 3494> **NOTE** 3495> 3496> This enum is supported since API version 8 and deprecated since API version 9. You are advised to use [Media Error Codes](../errorcodes/errorcode-media.md) instead. 3497 3498**System capability**: SystemCapability.Multimedia.Media.Core 3499 3500| Name | Value | Description | 3501| -------------------------- | ---- | -------------------------------------- | 3502| MSERR_OK | 0 | The operation is successful. | 3503| MSERR_NO_MEMORY | 1 | Failed to allocate memory. The system may have no available memory.| 3504| MSERR_OPERATION_NOT_PERMIT | 2 | No permission to perform the operation. | 3505| MSERR_INVALID_VAL | 3 | Invalid input parameter. | 3506| MSERR_IO | 4 | An I/O error occurs. | 3507| MSERR_TIMEOUT | 5 | The operation times out. | 3508| MSERR_UNKNOWN | 6 | An unknown error occurs. | 3509| MSERR_SERVICE_DIED | 7 | Invalid server. | 3510| MSERR_INVALID_STATE | 8 | The operation is not allowed in the current state. | 3511| MSERR_UNSUPPORTED | 9 | The operation is not supported in the current version. | 3512 3513## AudioPlayer<sup>(deprecated)</sup> 3514 3515> **NOTE** 3516> 3517> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayer](#avplayer9) instead. 3518 3519Provides APIs to manage and play audio. Before calling any API in **AudioPlayer**, you must use [createAudioPlayer()](#mediacreateaudioplayerdeprecated) to create an **AudioPlayer** instance. 3520 3521### Attributes<a name=audioplayer_attributes></a> 3522 3523**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3524 3525| Name | Type | Readable| Writable| Description | 3526| ------------------------------- | ------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 3527| src | string | Yes | Yes | Audio file URI. The mainstream audio formats (M4A, AAC, MP3, OGG, and WAV) are supported.<br>**Examples of supported URLs**:<br>1. FD: fd://xx<br>![](figures/en-us_image_url.png)<br>2. HTTP: http://xx<br>3. HTTPS: https://xx<br>4. HLS: http://xx or https://xx<br>**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.INTERNET| 3528| fdSrc<sup>9+</sup> | [AVFileDescriptor](#avfiledescriptor9) | Yes | Yes | Description of the audio file. This attribute is required when audio assets of an application are continuously stored in a file.<br>**Example:**<br>Assume that a music file that stores continuous music assets consists of the following:<br>Music 1 (address offset: 0, byte length: 100)<br>Music 2 (address offset: 101; byte length: 50)<br>Music 3 (address offset: 151, byte length: 150)<br>1. To play music 1: AVFileDescriptor {fd = resource handle; offset = 0; length = 100; }<br>2. To play music 2: AVFileDescriptor {fd = resource handle; offset = 101; length = 50; }<br>3. To play music 3: AVFileDescriptor {fd = resource handle; offset = 151; length = 150; }<br>To play an independent music file, use **src=fd://xx**.<br>| 3529| loop | boolean | Yes | Yes | Whether to loop audio playback. The value **true** means to loop audio playback, and **false** means the opposite. | 3530| audioInterruptMode<sup>9+</sup> | [audio.InterruptMode](js-apis-audio.md#interruptmode9) | Yes | Yes | Audio interruption mode. | 3531| currentTime | number | Yes | No | Current audio playback position, in ms. | 3532| duration | number | Yes | No | Audio duration, in ms. | 3533| state | [AudioState](#audiostate) | Yes | No | Audio playback state. This state cannot be used as the condition for triggering the call of **play()**, **pause()**, or **stop()**.| 3534 3535### play<a name=audioplayer_play></a> 3536 3537play(): void 3538 3539Starts to play an audio asset. This API can be called only after the [dataLoad](#audioplayer_on) event is triggered. 3540 3541**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3542 3543**Example** 3544 3545```js 3546audioPlayer.on('play', () => { // Set the 'play' event callback. 3547 console.log('audio play success'); 3548}); 3549audioPlayer.play(); 3550``` 3551 3552### pause<a name=audioplayer_pause></a> 3553 3554pause(): void 3555 3556Pauses audio playback. 3557 3558**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3559 3560**Example** 3561 3562```js 3563audioPlayer.on('pause', () => { // Set the 'pause' event callback. 3564 console.log('audio pause success'); 3565}); 3566audioPlayer.pause(); 3567``` 3568 3569### stop<a name=audioplayer_stop></a> 3570 3571stop(): void 3572 3573Stops audio playback. 3574 3575**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3576 3577**Example** 3578 3579```js 3580audioPlayer.on('stop', () => { // Set the 'stop' event callback. 3581 console.log('audio stop success'); 3582}); 3583audioPlayer.stop(); 3584``` 3585 3586### reset<sup>7+</sup><a name=audioplayer_reset></a> 3587 3588reset(): void 3589 3590Resets the audio asset to be played. 3591 3592**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3593 3594**Example** 3595 3596```js 3597audioPlayer.on('reset', () => { // Set the 'reset' event callback. 3598 console.log('audio reset success'); 3599}); 3600audioPlayer.reset(); 3601``` 3602 3603### seek<a name=audioplayer_seek></a> 3604 3605seek(timeMs: number): void 3606 3607Seeks to the specified playback position. 3608 3609**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3610 3611**Parameters** 3612 3613| Name| Type | Mandatory| Description | 3614| ------ | ------ | ---- | ----------------------------------------------------------- | 3615| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, duration].| 3616 3617**Example** 3618 3619```js 3620audioPlayer.on('timeUpdate', (seekDoneTime) => { // Set the 'timeUpdate' event callback. 3621 if (seekDoneTime == null) { 3622 console.info('audio seek fail'); 3623 return; 3624 } 3625 console.log('audio seek success. seekDoneTime: ' + seekDoneTime); 3626}); 3627audioPlayer.seek(30000); // Seek to 30000 ms. 3628``` 3629 3630### setVolume<a name=audioplayer_setvolume></a> 3631 3632setVolume(vol: number): void 3633 3634Sets the volume. 3635 3636**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3637 3638**Parameters** 3639 3640| Name| Type | Mandatory| Description | 3641| ------ | ------ | ---- | ------------------------------------------------------------ | 3642| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1.00** indicates the maximum volume (100%).| 3643 3644**Example** 3645 3646```js 3647audioPlayer.on('volumeChange', () => { // Set the 'volumeChange' event callback. 3648 console.log('audio volumeChange success'); 3649}); 3650audioPlayer.setVolume(1); // Set the volume to 100%. 3651``` 3652 3653### release<a name=audioplayer_release></a> 3654 3655release(): void 3656 3657Releases the audio playback resources. 3658 3659**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3660 3661**Example** 3662 3663```js 3664audioPlayer.release(); 3665audioPlayer = undefined; 3666``` 3667 3668### getTrackDescription<sup>8+</sup><a name=audioplayer_gettrackdescription1></a> 3669 3670getTrackDescription(callback: AsyncCallback\<Array\<MediaDescription>>): void 3671 3672Obtains the audio track information. This API uses an asynchronous callback to return the result. It can be called only after the [dataLoad](#audioplayer_on) event is triggered. 3673 3674**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3675 3676**Parameters** 3677 3678| Name | Type | Mandatory| Description | 3679| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------ | 3680| callback | AsyncCallback\<Array\<[MediaDescription](#mediadescription8)>> | Yes | Callback used to return a **MediaDescription** array, which records the audio track information.| 3681 3682**Example** 3683 3684```js 3685function printfDescription(obj) { 3686 for (let item in obj) { 3687 let property = obj[item]; 3688 console.info('audio key is ' + item); 3689 console.info('audio value is ' + property); 3690 } 3691} 3692 3693audioPlayer.getTrackDescription((error, arrList) => { 3694 if (arrList != null) { 3695 for (let i = 0; i < arrList.length; i++) { 3696 printfDescription(arrList[i]); 3697 } 3698 } else { 3699 console.log(`audio getTrackDescription fail, error:${error}`); 3700 } 3701}); 3702``` 3703 3704### getTrackDescription<sup>8+</sup><a name=audioplayer_gettrackdescription2></a> 3705 3706getTrackDescription(): Promise\<Array\<MediaDescription>> 3707 3708Obtains the audio track information. This API uses a promise to return the result. It can be called only after the [dataLoad](#audioplayer_on) event is triggered. 3709 3710**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3711 3712**Return value** 3713 3714| Type | Description | 3715| ------------------------------------------------------ | ----------------------------------------------- | 3716| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return a **MediaDescription** array, which records the audio track information.| 3717 3718**Example** 3719 3720```js 3721function printfDescription(obj) { 3722 for (let item in obj) { 3723 let property = obj[item]; 3724 console.info('audio key is ' + item); 3725 console.info('audio value is ' + property); 3726 } 3727} 3728let arrayDescription = null 3729audioPlayer.getTrackDescription().then((arrList) => { 3730 if (arrList != null) { 3731 arrayDescription = arrList; 3732 } else { 3733 console.log('audio getTrackDescription fail'); 3734 } 3735}).catch((error) => { 3736 console.info(`audio catchCallback, error:${error}`); 3737}); 3738 3739for (let i = 0; i < arrayDescription.length; i++) { 3740 printfDescription(arrayDescription[i]); 3741} 3742``` 3743 3744### on('bufferingUpdate')<sup>8+</sup> 3745 3746on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void 3747 3748Subscribes to the audio buffering update event. This API works only under online playback. 3749 3750**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3751 3752**Parameters** 3753 3754| Name | Type | Mandatory| Description | 3755| -------- | -------- | ---- | ------------------------------------------------------------ | 3756| type | string | Yes | Event type, which is **'bufferingUpdate'** in this case. | 3757| callback | function | Yes | Callback invoked when the event is triggered.<br>When [BufferingInfoType](#bufferinginfotype8) is set to **BUFFERING_PERCENT** or **CACHED_DURATION**, **value** is valid. Otherwise, **value** is fixed at **0**.| 3758 3759**Example** 3760 3761```js 3762audioPlayer.on('bufferingUpdate', (infoType, value) => { 3763 console.log('audio bufferingInfo type: ' + infoType); 3764 console.log('audio bufferingInfo value: ' + value); 3765}); 3766``` 3767 3768 ### on('play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeChange')<a name = audioplayer_on></a> 3769 3770on(type: 'play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeChange', callback: () => void): void 3771 3772Subscribes to the audio playback events. 3773 3774**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3775 3776**Parameters** 3777 3778| Name | Type | Mandatory| Description | 3779| -------- | ---------- | ---- | ------------------------------------------------------------ | 3780| type | string | Yes | Event type. The following events are supported:<br>- 'play': triggered when the [play()](#audioplayer_play) API is called and audio playback starts.<br>- 'pause': triggered when the [pause()](#audioplayer_pause) API is called and audio playback is paused.<br>- 'stop': triggered when the [stop()](#audioplayer_stop) API is called and audio playback stops.<br>- 'reset': triggered when the [reset()](#audioplayer_reset) API is called and audio playback is reset.<br>- 'dataLoad': triggered when the audio data is loaded, that is, when the **src** attribute is configured.<br>- 'finish': triggered when the audio playback is finished.<br>- 'volumeChange': triggered when the [setVolume()](#audioplayer_setvolume) API is called and the playback volume is changed.| 3781| callback | () => void | Yes | Callback invoked when the event is triggered. | 3782 3783**Example** 3784 3785```js 3786import fs from '@ohos.file.fs'; 3787 3788let audioPlayer = media.createAudioPlayer(); // Create an AudioPlayer instance. 3789audioPlayer.on('dataLoad', () => { // Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully. 3790 console.info('audio set source success'); 3791 audioPlayer.play(); // Start the playback and trigger the 'play' event callback. 3792}); 3793audioPlayer.on('play', () => { // Set the 'play' event callback. 3794 console.info('audio play success'); 3795 audioPlayer.seek(30000); // Call the seek() API and trigger the 'timeUpdate' event callback. 3796}); 3797audioPlayer.on('pause', () => { // Set the 'pause' event callback. 3798 console.info('audio pause success'); 3799 audioPlayer.stop(); // Stop the playback and trigger the 'stop' event callback. 3800}); 3801audioPlayer.on('reset', () => { // Set the 'reset' event callback. 3802 console.info('audio reset success'); 3803 audioPlayer.release(); // Release the AudioPlayer instance. 3804 audioPlayer = undefined; 3805}); 3806audioPlayer.on('timeUpdate', (seekDoneTime) => { // Set the 'timeUpdate' event callback. 3807 if (seekDoneTime == null) { 3808 console.info('audio seek fail'); 3809 return; 3810 } 3811 console.info('audio seek success, and seek time is ' + seekDoneTime); 3812 audioPlayer.setVolume(0.5); // Set the volume to 50% and trigger the 'volumeChange' event callback. 3813}); 3814audioPlayer.on('volumeChange', () => { // Set the 'volumeChange' event callback. 3815 console.info('audio volumeChange success'); 3816 audioPlayer.pause(); // Pause the playback and trigger the 'pause' event callback. 3817}); 3818audioPlayer.on('finish', () => { // Set the 'finish' event callback. 3819 console.info('audio play finish'); 3820 audioPlayer.stop(); // Stop the playback and trigger the 'stop' event callback. 3821}); 3822audioPlayer.on('error', (error) => { // Set the 'error' event callback. 3823 console.info(`audio error called, error: ${error}`); 3824}); 3825 3826// Set the FD (local playback) of the audio file selected by the user. 3827let fdPath = 'fd://'; 3828// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/accounts/account_0/appdata" command. 3829let path = '/data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3'; 3830fs.open(path).then((file) => { 3831 fdPath = fdPath + '' + file.fd; 3832 console.info('open fd success fd is' + fdPath); 3833 audioPlayer.src = fdPath; // Set the src attribute and trigger the 'dataLoad' event callback. 3834}, (err) => { 3835 console.info('open fd failed err is' + err); 3836}).catch((err) => { 3837 console.info('open fd failed err is' + err); 3838}); 3839``` 3840 3841### on('timeUpdate') 3842 3843on(type: 'timeUpdate', callback: Callback\<number>): void 3844 3845Subscribes to the **'timeUpdate'** event. This event is reported every second when the audio playback is in progress. 3846 3847**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3848 3849**Parameters** 3850 3851| Name | Type | Mandatory| Description | 3852| -------- | ----------------- | ---- | ------------------------------------------------------------ | 3853| type | string | Yes | Event type, which is **'timeUpdate'** in this case.<br>The **'timeUpdate'** event is triggered when the audio playback starts after an audio playback timestamp update.| 3854| callback | Callback\<number> | Yes | Callback invoked when the event is triggered. The input parameter is the updated timestamp. | 3855 3856**Example** 3857 3858```js 3859audioPlayer.on('timeUpdate', (newTime) => { // Set the 'timeUpdate' event callback. 3860 if (newTime == null) { 3861 console.info('audio timeUpadate fail'); 3862 return; 3863 } 3864 console.log('audio timeUpadate success. seekDoneTime: ' + newTime); 3865}); 3866audioPlayer.play(); // The 'timeUpdate' event is triggered when the playback starts. 3867``` 3868 3869### on('error') 3870 3871on(type: 'error', callback: ErrorCallback): void 3872 3873Subscribes to audio playback error events. After an error event is reported, you must handle the event and exit the playback. 3874 3875**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3876 3877**Parameters** 3878 3879| Name | Type | Mandatory| Description | 3880| -------- | ------------- | ---- | ------------------------------------------------------------ | 3881| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during audio playback.| 3882| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 3883 3884**Example** 3885 3886```js 3887audioPlayer.on('error', (error) => { // Set the 'error' event callback. 3888 console.info(`audio error called, error: ${error}`); 3889}); 3890audioPlayer.setVolume(3); // Set volume to an invalid value to trigger the 'error' event. 3891``` 3892 3893## AudioState<sup>(deprecated)</sup> 3894 3895Enumerates the audio playback states. You can obtain the state through the **state** attribute. 3896 3897> **NOTE** 3898> 3899> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayerState](#avplayerstate9) instead. 3900 3901**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 3902 3903| Name | Type | Description | 3904| ------- | ------ | ---------------------------------------------- | 3905| idle | string | No audio playback is in progress. The audio player is in this state after the **'dataload'** or **'reset'** event is triggered.| 3906| playing | string | Audio playback is in progress. The audio player is in this state after the **'play'** event is triggered. | 3907| paused | string | Audio playback is paused. The audio player is in this state after the **'pause'** event is triggered. | 3908| stopped | string | Audio playback is stopped. The audio player is in this state after the **'stop'** event is triggered. | 3909| error | string | Audio playback is in the error state. | 3910 3911## VideoPlayer<sup>(deprecated)</sup><a name=videoplayer></a> 3912 3913> **NOTE** 3914> 3915> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer](#avplayer9) instead. 3916 3917Provides APIs to manage and play video. Before calling any API of **VideoPlayer**, you must use [createVideoPlayer()](#createvideoplayer) to create a **VideoPlayer** instance. 3918 3919### Attributes<a name=videoplayer_attributes></a> 3920 3921**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 3922 3923| Name | Type | Readable| Writable| Description | 3924| ------------------------------- | ------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 3925| url<sup>8+</sup> | string | Yes | Yes | Video URL. The mainstream video formats (MP4, MPEG-TS, WebM, and MKV) are supported.<br>**Example of supported URLs**:<br>1. FD: fd://xx<br>![](figures/en-us_image_url.png)<br>2. HTTP: http://xx<br>3. HTTPS: https://xx<br>4. HLS: http://xx or https://xx<br>| 3926| fdSrc<sup>9+</sup> | [AVFileDescriptor](#avfiledescriptor9) | Yes | Yes | Description of a video file. This attribute is required when video assets of an application are continuously stored in a file.<br>**Example:**<br>Assume that a music file that stores continuous music assets consists of the following:<br>Video 1 (address offset: 0, byte length: 100)<br>Video 2 (address offset: 101; byte length: 50)<br>Video 3 (address offset: 151, byte length: 150)<br>1. To play video 1: AVFileDescriptor {fd = resource handle; offset = 0; length = 100; }<br>2. To play video 2: AVFileDescriptor {fd = resource handle; offset = 101; length = 50; }<br>3. To play video 3: AVFileDescriptor {fd = resource handle; offset = 151; length = 150; }<br>To play an independent video file, use **src=fd://xx**.<br>| 3927| loop<sup>8+</sup> | boolean | Yes | Yes | Whether to loop video playback. The value **true** means to loop video playback, and **false** means the opposite. | 3928| videoScaleType<sup>9+</sup> | [VideoScaleType](#videoscaletype9) | Yes | Yes | Video scale type. | 3929| audioInterruptMode<sup>9+</sup> | [audio.InterruptMode](js-apis-audio.md#interruptmode9) | Yes | Yes | Audio interruption mode. | 3930| currentTime<sup>8+</sup> | number | Yes | No | Current video playback position, in ms. | 3931| duration<sup>8+</sup> | number | Yes | No | Video duration, in ms. The value **-1** indicates the live mode. | 3932| state<sup>8+</sup> | [VideoPlayState](#videoplayerstate) | Yes | No | Video playback state. | 3933| width<sup>8+</sup> | number | Yes | No | Video width, in pixels. | 3934| height<sup>8+</sup> | number | Yes | No | Video height, in pixels. | 3935 3936### setDisplaySurface<sup>8+</sup> 3937 3938setDisplaySurface(surfaceId: string, callback: AsyncCallback\<void>): void 3939 3940Sets **SurfaceId**. This API uses an asynchronous callback to return the result. 3941 3942**SetDisplaySurface** must be called between the URL setting and the calling of **prepare**. A surface must be set for video streams without audio. Otherwise, the calling of **prepare** fails. 3943 3944**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 3945 3946**Parameters** 3947 3948| Name | Type | Mandatory| Description | 3949| --------- | -------------------- | ---- | ------------------------- | 3950| surfaceId | string | Yes | Surface ID to set. | 3951| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3952 3953**Example** 3954 3955```js 3956let surfaceId = null; 3957videoPlayer.setDisplaySurface(surfaceId, (err) => { 3958 if (err == null) { 3959 console.info('setDisplaySurface success!'); 3960 } else { 3961 console.info('setDisplaySurface fail!'); 3962 } 3963}); 3964``` 3965 3966### setDisplaySurface<sup>8+</sup> 3967 3968setDisplaySurface(surfaceId: string): Promise\<void> 3969 3970Sets **SurfaceId**. This API uses a promise to return the result. 3971 3972**SetDisplaySurface** must be called between the URL setting and the calling of **prepare**. A surface must be set for video streams without audio. Otherwise, the calling of **prepare** fails. 3973 3974**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 3975 3976**Parameters** 3977 3978| Name | Type | Mandatory| Description | 3979| --------- | ------ | ---- | --------- | 3980| surfaceId | string | Yes | Surface ID to set.| 3981 3982**Return value** 3983 3984| Type | Description | 3985| -------------- | ------------------------------ | 3986| Promise\<void> | Promise used to return the result.| 3987 3988**Example** 3989 3990```js 3991let surfaceId = null; 3992videoPlayer.setDisplaySurface(surfaceId).then(() => { 3993 console.info('setDisplaySurface success'); 3994}).catch((error) => { 3995 console.info(`video catchCallback, error:${error}`); 3996}); 3997``` 3998 3999### prepare<sup>8+</sup> 4000 4001prepare(callback: AsyncCallback\<void>): void 4002 4003Prepares for video playback. This API uses an asynchronous callback to return the result. 4004 4005**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4006 4007**Parameters** 4008 4009| Name | Type | Mandatory| Description | 4010| -------- | -------------------- | ---- | ------------------------ | 4011| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4012 4013**Example** 4014 4015```js 4016videoPlayer.prepare((err) => { 4017 if (err == null) { 4018 console.info('prepare success!'); 4019 } else { 4020 console.info('prepare fail!'); 4021 } 4022}); 4023``` 4024 4025### prepare<sup>8+</sup> 4026 4027prepare(): Promise\<void> 4028 4029Prepares for video playback. This API uses a promise to return the result. 4030 4031**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4032 4033**Return value** 4034 4035| Type | Description | 4036| -------------- | ----------------------------- | 4037| Promise\<void> | Promise used to return the result.| 4038 4039**Example** 4040 4041```js 4042videoPlayer.prepare().then(() => { 4043 console.info('prepare success'); 4044}).catch((error) => { 4045 console.info(`video catchCallback, error:${error}`); 4046}); 4047``` 4048 4049### play<sup>8+</sup> 4050 4051play(callback: AsyncCallback\<void>): void; 4052 4053Starts to play video assets. This API uses an asynchronous callback to return the result. 4054 4055**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4056 4057**Parameters** 4058 4059| Name | Type | Mandatory| Description | 4060| -------- | -------------------- | ---- | ------------------------ | 4061| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4062 4063**Example** 4064 4065```js 4066videoPlayer.play((err) => { 4067 if (err == null) { 4068 console.info('play success!'); 4069 } else { 4070 console.info('play fail!'); 4071 } 4072}); 4073``` 4074 4075### play<sup>8+</sup> 4076 4077play(): Promise\<void>; 4078 4079Starts to play video assets. This API uses a promise to return the result. 4080 4081**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4082 4083**Return value** 4084 4085| Type | Description | 4086| -------------- | ----------------------------- | 4087| Promise\<void> | Promise used to return the result.| 4088 4089**Example** 4090 4091```js 4092videoPlayer.play().then(() => { 4093 console.info('play success'); 4094}).catch((error) => { 4095 console.info(`video catchCallback, error:${error}`); 4096}); 4097``` 4098 4099### pause<sup>8+</sup> 4100 4101pause(callback: AsyncCallback\<void>): void 4102 4103Pauses video playback. This API uses an asynchronous callback to return the result. 4104 4105**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4106 4107**Parameters** 4108 4109| Name | Type | Mandatory| Description | 4110| -------- | -------------------- | ---- | ------------------------ | 4111| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4112 4113**Example** 4114 4115```js 4116videoPlayer.pause((err) => { 4117 if (err == null) { 4118 console.info('pause success!'); 4119 } else { 4120 console.info('pause fail!'); 4121 } 4122}); 4123``` 4124 4125### pause<sup>8+</sup> 4126 4127pause(): Promise\<void> 4128 4129Pauses video playback. This API uses a promise to return the result. 4130 4131**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4132 4133**Return value** 4134 4135| Type | Description | 4136| -------------- | ----------------------------- | 4137| Promise\<void> | Promise used to return the result.| 4138 4139**Example** 4140 4141```js 4142videoPlayer.pause().then(() => { 4143 console.info('pause success'); 4144}).catch((error) => { 4145 console.info(`video catchCallback, error:${error}`); 4146}); 4147``` 4148 4149### stop<sup>8+</sup> 4150 4151stop(callback: AsyncCallback\<void>): void 4152 4153Stops video playback. This API uses an asynchronous callback to return the result. 4154 4155**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4156 4157**Parameters** 4158 4159| Name | Type | Mandatory| Description | 4160| -------- | -------------------- | ---- | ------------------------ | 4161| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4162 4163**Example** 4164 4165```js 4166videoPlayer.stop((err) => { 4167 if (err == null) { 4168 console.info('stop success!'); 4169 } else { 4170 console.info('stop fail!'); 4171 } 4172}); 4173``` 4174 4175### stop<sup>8+</sup> 4176 4177stop(): Promise\<void> 4178 4179Stops video playback. This API uses a promise to return the result. 4180 4181**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4182 4183**Return value** 4184 4185| Type | Description | 4186| -------------- | ----------------------------- | 4187| Promise\<void> | Promise used to return the result.| 4188 4189**Example** 4190 4191```js 4192videoPlayer.stop().then(() => { 4193 console.info('stop success'); 4194}).catch((error) => { 4195 console.info(`video catchCallback, error:${error}`); 4196}); 4197``` 4198 4199### reset<sup>8+</sup> 4200 4201reset(callback: AsyncCallback\<void>): void 4202 4203Resets the video asset to be played. This API uses an asynchronous callback to return the result. 4204 4205**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4206 4207**Parameters** 4208 4209| Name | Type | Mandatory| Description | 4210| -------- | -------------------- | ---- | ------------------------ | 4211| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4212 4213**Example** 4214 4215```js 4216videoPlayer.reset((err) => { 4217 if (err == null) { 4218 console.info('reset success!'); 4219 } else { 4220 console.info('reset fail!'); 4221 } 4222}); 4223``` 4224 4225### reset<sup>8+</sup> 4226 4227reset(): Promise\<void> 4228 4229Resets the video asset to be played. This API uses a promise to return the result. 4230 4231**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4232 4233**Return value** 4234 4235| Type | Description | 4236| -------------- | ----------------------------- | 4237| Promise\<void> | Promise used to return the result.| 4238 4239**Example** 4240 4241```js 4242videoPlayer.reset().then(() => { 4243 console.info('reset success'); 4244}).catch((error) => { 4245 console.info(`video catchCallback, error:${error}`); 4246}); 4247``` 4248 4249### seek<sup>8+</sup> 4250 4251seek(timeMs: number, callback: AsyncCallback\<number>): void 4252 4253Seeks to the specified playback position. The previous key frame at the specified position is played. This API uses an asynchronous callback to return the result. 4254 4255**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4256 4257**Parameters** 4258 4259| Name | Type | Mandatory| Description | 4260| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 4261| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, duration].| 4262| callback | AsyncCallback\<number> | Yes | Callback used to return the result. | 4263 4264**Example** 4265 4266```js 4267let seekTime = 5000; 4268videoPlayer.seek(seekTime, (err, result) => { 4269 if (err == null) { 4270 console.info('seek success!'); 4271 } else { 4272 console.info('seek fail!'); 4273 } 4274}); 4275``` 4276 4277### seek<sup>8+</sup> 4278 4279seek(timeMs: number, mode:SeekMode, callback: AsyncCallback\<number>): void 4280 4281Seeks to the specified playback position. This API uses an asynchronous callback to return the result. 4282 4283**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4284 4285**Parameters** 4286 4287| Name | Type | Mandatory| Description | 4288| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 4289| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, duration].| 4290| mode | [SeekMode](#seekmode8) | Yes | Seek mode. | 4291| callback | AsyncCallback\<number> | Yes | Callback used to return the result. | 4292 4293**Example** 4294 4295```js 4296import media from '@ohos.multimedia.media' 4297let seekTime = 5000; 4298videoPlayer.seek(seekTime, media.SeekMode.SEEK_NEXT_SYNC, (err, result) => { 4299 if (err == null) { 4300 console.info('seek success!'); 4301 } else { 4302 console.info('seek fail!'); 4303 } 4304}); 4305``` 4306 4307### seek<sup>8+</sup> 4308 4309seek(timeMs: number, mode?:SeekMode): Promise\<number> 4310 4311Seeks to the specified playback position. If **mode** is not specified, the previous key frame at the specified position is played. This API uses a promise to return the result. 4312 4313**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4314 4315**Parameters** 4316 4317| Name| Type | Mandatory| Description | 4318| ------ | ---------------------- | ---- | ------------------------------------------------------------ | 4319| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, duration].| 4320| mode | [SeekMode](#seekmode8) | No | Seek mode. | 4321 4322**Return value** 4323 4324| Type | Description | 4325| ---------------- | ------------------------------------------- | 4326| Promise\<number>| Promise used to return the playback position, in ms.| 4327 4328**Example** 4329 4330```js 4331import media from '@ohos.multimedia.media' 4332let seekTime = 5000; 4333videoPlayer.seek(seekTime).then((seekDoneTime) => { // seekDoneTime indicates the position after the seek operation is complete. 4334 console.info('seek success'); 4335}).catch((error) => { 4336 console.info(`video catchCallback, error:${error}`); 4337}); 4338 4339videoPlayer.seek(seekTime, media.SeekMode.SEEK_NEXT_SYNC).then((seekDoneTime) => { 4340 console.info('seek success'); 4341}).catch((error) => { 4342 console.info(`video catchCallback, error:${error}`); 4343}); 4344``` 4345 4346### setVolume<sup>8+</sup> 4347 4348setVolume(vol: number, callback: AsyncCallback\<void>): void 4349 4350Sets the volume. This API uses an asynchronous callback to return the result. 4351 4352**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4353 4354**Parameters** 4355 4356| Name | Type | Mandatory| Description | 4357| -------- | -------------------- | ---- | ------------------------------------------------------------ | 4358| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1.00** indicates the maximum volume (100%).| 4359| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 4360 4361**Example** 4362 4363```js 4364let vol = 0.5; 4365videoPlayer.setVolume(vol, (err, result) => { 4366 if (err == null) { 4367 console.info('setVolume success!'); 4368 } else { 4369 console.info('setVolume fail!'); 4370 } 4371}); 4372``` 4373 4374### setVolume<sup>8+</sup> 4375 4376setVolume(vol: number): Promise\<void> 4377 4378Sets the volume. This API uses a promise to return the result. 4379 4380**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4381 4382**Parameters** 4383 4384| Name| Type | Mandatory| Description | 4385| ------ | ------ | ---- | ------------------------------------------------------------ | 4386| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1.00** indicates the maximum volume (100%).| 4387 4388**Return value** 4389 4390| Type | Description | 4391| -------------- | ------------------------- | 4392| Promise\<void> | Promise used to return the result.| 4393 4394**Example** 4395 4396```js 4397let vol = 0.5; 4398videoPlayer.setVolume(vol).then(() => { 4399 console.info('setVolume success'); 4400}).catch((error) => { 4401 console.info(`video catchCallback, error:${error}`); 4402}); 4403``` 4404 4405### release<sup>8+</sup> 4406 4407release(callback: AsyncCallback\<void>): void 4408 4409Releases the video playback resources. This API uses an asynchronous callback to return the result. 4410 4411**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4412 4413**Parameters** 4414 4415| Name | Type | Mandatory| Description | 4416| -------- | -------------------- | ---- | ------------------------ | 4417| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4418 4419**Example** 4420 4421```js 4422videoPlayer.release((err) => { 4423 if (err == null) { 4424 console.info('release success!'); 4425 } else { 4426 console.info('release fail!'); 4427 } 4428}); 4429``` 4430 4431### release<sup>8+</sup> 4432 4433release(): Promise\<void> 4434 4435Releases the video playback resources. This API uses a promise to return the result. 4436 4437**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4438 4439**Return value** 4440 4441| Type | Description | 4442| -------------- | ----------------------------- | 4443| Promise\<void> | Promise used to return the result.| 4444 4445**Example** 4446 4447```js 4448videoPlayer.release().then(() => { 4449 console.info('release success'); 4450}).catch((error) => { 4451 console.info(`video catchCallback, error:${error}`); 4452}); 4453``` 4454 4455### getTrackDescription<sup>8+</sup> 4456 4457getTrackDescription(callback: AsyncCallback\<Array\<MediaDescription>>): void 4458 4459Obtains the video track information. This API uses an asynchronous callback to return the result. 4460 4461**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4462 4463**Parameters** 4464 4465| Name | Type | Mandatory| Description | 4466| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------ | 4467| callback | AsyncCallback\<Array\<[MediaDescription](#mediadescription8)>> | Yes | Callback used to return a **MediaDescription** array, which records the video track information.| 4468 4469**Example** 4470 4471```js 4472function printfDescription(obj) { 4473 for (let item in obj) { 4474 let property = obj[item]; 4475 console.info('video key is ' + item); 4476 console.info('video value is ' + property); 4477 } 4478} 4479 4480videoPlayer.getTrackDescription((error, arrList) => { 4481 if ((arrList) != null) { 4482 for (let i = 0; i < arrList.length; i++) { 4483 printfDescription(arrList[i]); 4484 } 4485 } else { 4486 console.log(`video getTrackDescription fail, error:${error}`); 4487 } 4488}); 4489``` 4490 4491### getTrackDescription<sup>8+</sup> 4492 4493getTrackDescription(): Promise\<Array\<MediaDescription>> 4494 4495Obtains the video track information. This API uses a promise to return the result. 4496 4497**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4498 4499**Return value** 4500 4501| Type | Description | 4502| ------------------------------------------------------ | ----------------------------------------------- | 4503| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return a **MediaDescription** array, which records the video track information.| 4504 4505**Example** 4506 4507```js 4508function printfDescription(obj) { 4509 for (let item in obj) { 4510 let property = obj[item]; 4511 console.info('video key is ' + item); 4512 console.info('video value is ' + property); 4513 } 4514} 4515 4516let arrayDescription; 4517videoPlayer.getTrackDescription().then((arrList) => { 4518 if (arrList != null) { 4519 arrayDescription = arrList; 4520 } else { 4521 console.log('video getTrackDescription fail'); 4522 } 4523}).catch((error) => { 4524 console.info(`video catchCallback, error:${error}`); 4525}); 4526for (let i = 0; i < arrayDescription.length; i++) { 4527 printfDescription(arrayDescription[i]); 4528} 4529``` 4530 4531### setSpeed<sup>8+</sup> 4532 4533setSpeed(speed:number, callback: AsyncCallback\<number>): void 4534 4535Sets the video playback speed. This API uses an asynchronous callback to return the result. 4536 4537**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4538 4539**Parameters** 4540 4541| Name | Type | Mandatory| Description | 4542| -------- | ---------------------- | ---- | ---------------------------------------------------------- | 4543| speed | number | Yes | Video playback speed. For details, see [PlaybackSpeed](#playbackspeed8).| 4544| callback | AsyncCallback\<number> | Yes | Callback used to return the result. | 4545 4546**Example** 4547 4548```js 4549import media from '@ohos.multimedia.media' 4550let speed = media.PlaybackSpeed.SPEED_FORWARD_2_00_X; 4551 4552videoPlayer.setSpeed(speed, (err, result) => { 4553 if (err == null) { 4554 console.info('setSpeed success!'); 4555 } else { 4556 console.info('setSpeed fail!'); 4557 } 4558}); 4559``` 4560 4561### setSpeed<sup>8+</sup> 4562 4563setSpeed(speed:number): Promise\<number> 4564 4565Sets the video playback speed. This API uses a promise to return the result. 4566 4567**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4568 4569**Parameters** 4570 4571| Name| Type | Mandatory| Description | 4572| ------ | ------ | ---- | ---------------------------------------------------------- | 4573| speed | number | Yes | Video playback speed. For details, see [PlaybackSpeed](#playbackspeed8).| 4574 4575**Return value** 4576 4577| Type | Description | 4578| ---------------- | ------------------------------------------------------------ | 4579| Promise\<number>| Promise used to return playback speed. For details, see [PlaybackSpeed](#playbackspeed8).| 4580 4581**Example** 4582 4583```js 4584import media from '@ohos.multimedia.media' 4585let speed = media.PlaybackSpeed.SPEED_FORWARD_2_00_X; 4586 4587videoPlayer.setSpeed(speed).then(() => { 4588 console.info('setSpeed success'); 4589}).catch((error) => { 4590 console.info(`video catchCallback, error:${error}`); 4591}); 4592``` 4593 4594### on('playbackCompleted')<sup>8+</sup> 4595 4596on(type: 'playbackCompleted', callback: Callback\<void>): void 4597 4598Subscribes to the video playback completion event. 4599 4600**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4601 4602**Parameters** 4603 4604| Name | Type | Mandatory| Description | 4605| -------- | -------- | ---- | ----------------------------------------------------------- | 4606| type | string | Yes | Event type, which is **'playbackCompleted'** in this case.| 4607| callback | function | Yes | Callback invoked when the event is triggered. | 4608 4609**Example** 4610 4611```js 4612videoPlayer.on('playbackCompleted', () => { 4613 console.info('playbackCompleted success!'); 4614}); 4615``` 4616 4617### on('bufferingUpdate')<sup>8+</sup> 4618 4619on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void 4620 4621Subscribes to the video buffering update event. Only network playback supports this subscription. 4622 4623**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4624 4625**Parameters** 4626 4627| Name | Type | Mandatory| Description | 4628| -------- | -------- | ---- | ------------------------------------------------------------ | 4629| type | string | Yes | Event type, which is **'bufferingUpdate'** in this case. | 4630| callback | function | Yes | Callback invoked when the event is triggered.<br>When [BufferingInfoType](#bufferinginfotype8) is set to **BUFFERING_PERCENT** or **CACHED_DURATION**, **value** is valid. Otherwise, **value** is fixed at **0**.| 4631 4632**Example** 4633 4634```js 4635videoPlayer.on('bufferingUpdate', (infoType, value) => { 4636 console.log('video bufferingInfo type: ' + infoType); 4637 console.log('video bufferingInfo value: ' + value); 4638}); 4639``` 4640 4641### on('startRenderFrame')<sup>8+</sup> 4642 4643on(type: 'startRenderFrame', callback: Callback\<void>): void 4644 4645Subscribes to the frame rendering start event. 4646 4647**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4648 4649**Parameters** 4650 4651| Name | Type | Mandatory| Description | 4652| -------- | --------------- | ---- | ------------------------------------------------------------ | 4653| type | string | Yes | Event type, which is **'startRenderFrame'** in this case.| 4654| callback | Callback\<void> | Yes | Callback invoked when the event is triggered. | 4655 4656**Example** 4657 4658```js 4659videoPlayer.on('startRenderFrame', () => { 4660 console.info('startRenderFrame success!'); 4661}); 4662``` 4663 4664### on('videoSizeChanged')<sup>8+</sup> 4665 4666on(type: 'videoSizeChanged', callback: (width: number, height: number) => void): void 4667 4668Subscribes to the video width and height change event. 4669 4670**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4671 4672**Parameters** 4673 4674| Name | Type | Mandatory| Description | 4675| -------- | -------- | ---- | ------------------------------------------------------------ | 4676| type | string | Yes | Event type, which is **'videoSizeChanged'** in this case.| 4677| callback | function | Yes | Callback invoked when the event is triggered. **width** indicates the video width, and **height** indicates the video height. | 4678 4679**Example** 4680 4681```js 4682videoPlayer.on('videoSizeChanged', (width, height) => { 4683 console.log('video width is: ' + width); 4684 console.log('video height is: ' + height); 4685}); 4686``` 4687 4688### on('error')<sup>8+</sup> 4689 4690on(type: 'error', callback: ErrorCallback): void 4691 4692Subscribes to video playback error events. After an error event is reported, you must handle the event and exit the playback. 4693 4694**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4695 4696**Parameters** 4697 4698| Name | Type | Mandatory| Description | 4699| -------- | ------------- | ---- | ------------------------------------------------------------ | 4700| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during video playback.| 4701| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 4702 4703**Example** 4704 4705```js 4706videoPlayer.on('error', (error) => { // Set the 'error' event callback. 4707 console.info(`video error called, error: ${error}`); 4708}); 4709videoPlayer.url = 'fd://error'; // Set an incorrect URL to trigger the 'error' event. 4710``` 4711 4712## VideoPlayState<sup>(deprecated)</sup><a name=videoplayerstate></a> 4713 4714Enumerates the video playback states. You can obtain the state through the **state** attribute. 4715 4716> **NOTE** 4717> 4718> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayerState](#avplayerstate9) instead. 4719 4720**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4721 4722| Name | Type | Description | 4723| -------- | ------ | -------------- | 4724| idle | string | The video player is idle.| 4725| prepared | string | Video playback is being prepared.| 4726| playing | string | Video playback is in progress.| 4727| paused | string | Video playback is paused.| 4728| stopped | string | Video playback is stopped.| 4729| error | string | Video playback is in the error state. | 4730 4731## AudioRecorder<sup>(deprecated)</sup> 4732 4733> **NOTE** 4734> 4735> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder](#avrecorder9) instead. 4736 4737Implements audio recording. Before calling any API of **AudioRecorder**, you must use [createAudioRecorder()](#mediacreateaudiorecorder) to create an **AudioRecorder** instance. 4738 4739### prepare<a name=audiorecorder_prepare></a> 4740 4741prepare(config: AudioRecorderConfig): void 4742 4743Prepares for recording. 4744 4745**Required permissions:** ohos.permission.MICROPHONE 4746 4747**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4748 4749**Parameters** 4750 4751| Name| Type | Mandatory| Description | 4752| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ | 4753| config | [AudioRecorderConfig](#audiorecorderconfigdeprecated) | Yes | Audio recording parameters, including the audio output URI, encoding format, sampling rate, number of audio channels, and output format.| 4754 4755**Example** 4756 4757```js 4758let audioRecorderConfig = { 4759 audioEncoder : media.AudioEncoder.AAC_LC, 4760 audioEncodeBitRate : 22050, 4761 audioSampleRate : 22050, 4762 numberOfChannels : 2, 4763 format : media.AudioOutputFormat.AAC_ADTS, 4764 uri : 'fd://1', // The file must be created by the caller and granted with proper permissions. 4765 location : { latitude : 30, longitude : 130}, 4766} 4767audioRecorder.on('prepare', () => { // Set the 'prepare' event callback. 4768 console.log('prepare success'); 4769}); 4770audioRecorder.prepare(audioRecorderConfig); 4771``` 4772 4773 4774### start<a name=audiorecorder_start></a> 4775 4776start(): void 4777 4778Starts audio recording. This API can be called only after the [prepare](#audiorecorder_on) event is triggered. 4779 4780**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4781 4782**Example** 4783 4784```js 4785audioRecorder.on('start', () => { // Set the 'start' event callback. 4786 console.log('audio recorder start success'); 4787}); 4788audioRecorder.start(); 4789``` 4790 4791### pause<a name=audiorecorder_pause></a> 4792 4793pause():void 4794 4795Pauses audio recording. This API can be called only after the [start](#audiorecorder_on) event is triggered. 4796 4797**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4798 4799**Example** 4800 4801```js 4802audioRecorder.on('pause', () => { // Set the 'pause' event callback. 4803 console.log('audio recorder pause success'); 4804}); 4805audioRecorder.pause(); 4806``` 4807 4808### resume<a name=audiorecorder_resume></a> 4809 4810resume():void 4811 4812Resumes audio recording. This API can be called only after the [pause](#audiorecorder_on) event is triggered. 4813 4814**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4815 4816**Example** 4817 4818```js 4819audioRecorder.on('resume', () => { // Set the 'resume' event callback. 4820 console.log('audio recorder resume success'); 4821}); 4822audioRecorder.resume(); 4823``` 4824 4825### stop<a name=audiorecorder_stop></a> 4826 4827stop(): void 4828 4829Stops audio recording. 4830 4831**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4832 4833**Example** 4834 4835```js 4836audioRecorder.on('stop', () => { // Set the 'stop' event callback. 4837 console.log('audio recorder stop success'); 4838}); 4839audioRecorder.stop(); 4840``` 4841 4842### release<a name=audiorecorder_release></a> 4843 4844release(): void 4845 4846Releases the audio recording resources. 4847 4848**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4849 4850**Example** 4851 4852```js 4853audioRecorder.on('release', () => { // Set the 'release' event callback. 4854 console.log('audio recorder release success'); 4855}); 4856audioRecorder.release(); 4857audioRecorder = undefined; 4858``` 4859 4860### reset<a name=audiorecorder_reset></a> 4861 4862reset(): void 4863 4864Resets audio recording. 4865 4866Before resetting audio recording, you must call [stop()](#audiorecorder_stop) to stop recording. After audio recording is reset, you must call [prepare()](#audiorecorder_prepare) to set the recording parameters for another recording. 4867 4868**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4869 4870**Example** 4871 4872```js 4873audioRecorder.on('reset', () => { // Set the 'reset' event callback. 4874 console.log('audio recorder reset success'); 4875}); 4876audioRecorder.reset(); 4877``` 4878 4879### on('prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset')<a name=audiorecorder_on></a> 4880 4881on(type: 'prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset', callback: () => void): void 4882 4883Subscribes to the audio recording events. 4884 4885**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4886 4887**Parameters** 4888 4889| Name | Type | Mandatory| Description | 4890| -------- | -------- | ---- | ------------------------------------------------------------ | 4891| type | string | Yes | Event type. The following events are supported:<br>- 'prepare': triggered when the [prepare](#audiorecorder_prepare) API is called and the audio recording parameters are set.<br>- 'start': triggered when the [start](#audiorecorder_start) API is called and audio recording starts.<br>- 'pause': triggered when the [pause](#audiorecorder_pause) API is called and audio recording is paused.<br>- 'resume': triggered when the [resume](#audiorecorder_resume) API is called and audio recording is resumed.<br>- 'stop': triggered when the [stop](#audiorecorder_stop) API is called and audio recording stops.<br>- 'release': triggered when the [release](#audiorecorder_release) API is called and the recording resources are released.<br>- 'reset': triggered when the [reset](#audiorecorder_reset) API is called and audio recording is reset.| 4892| callback | ()=>void | Yes | Callback invoked when the event is triggered. | 4893 4894**Example** 4895 4896```js 4897let audioRecorder = media.createAudioRecorder(); // Create an AudioRecorder instance. 4898let audioRecorderConfig = { 4899 audioEncoder : media.AudioEncoder.AAC_LC, 4900 audioEncodeBitRate : 22050, 4901 audioSampleRate : 22050, 4902 numberOfChannels : 2, 4903 format : media.AudioOutputFormat.AAC_ADTS, 4904 uri : 'fd://xx', // The file must be created by the caller and granted with proper permissions. 4905 location : { latitude : 30, longitude : 130}, 4906} 4907audioRecorder.on('error', (error) => { // Set the 'error' event callback. 4908 console.info(`audio error called, error: ${error}`); 4909}); 4910audioRecorder.on('prepare', () => { // Set the 'prepare' event callback. 4911 console.log('prepare success'); 4912 audioRecorder.start(); // Start recording and trigger the 'start' event callback. 4913}); 4914audioRecorder.on('start', () => { // Set the 'start' event callback. 4915 console.log('audio recorder start success'); 4916}); 4917audioRecorder.on('pause', () => { // Set the 'pause' event callback. 4918 console.log('audio recorder pause success'); 4919}); 4920audioRecorder.on('resume', () => { // Set the 'resume' event callback. 4921 console.log('audio recorder resume success'); 4922}); 4923audioRecorder.on('stop', () => { // Set the 'stop' event callback. 4924 console.log('audio recorder stop success'); 4925}); 4926audioRecorder.on('release', () => { // Set the 'release' event callback. 4927 console.log('audio recorder release success'); 4928}); 4929audioRecorder.on('reset', () => { // Set the 'reset' event callback. 4930 console.log('audio recorder reset success'); 4931}); 4932audioRecorder.prepare(audioRecorderConfig) // Set recording parameters and trigger the 'prepare' event callback. 4933``` 4934 4935### on('error') 4936 4937on(type: 'error', callback: ErrorCallback): void 4938 4939Subscribes to audio recording error events. After an error event is reported, you must handle the event and exit the recording. 4940 4941**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4942 4943**Parameters** 4944 4945| Name | Type | Mandatory| Description | 4946| -------- | ------------- | ---- | ------------------------------------------------------------ | 4947| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during audio recording.| 4948| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 4949 4950**Example** 4951 4952```js 4953let audioRecorderConfig = { 4954 audioEncoder : media.AudioEncoder.AAC_LC, 4955 audioEncodeBitRate : 22050, 4956 audioSampleRate : 22050, 4957 numberOfChannels : 2, 4958 format : media.AudioOutputFormat.AAC_ADTS, 4959 uri : 'fd://xx', // The file must be created by the caller and granted with proper permissions. 4960 location : { latitude : 30, longitude : 130}, 4961} 4962audioRecorder.on('error', (error) => { // Set the 'error' event callback. 4963 console.info(`audio error called, error: ${error}`); 4964}); 4965audioRecorder.prepare(audioRecorderConfig); // Do no set any parameter in prepare and trigger the 'error' event callback. 4966``` 4967 4968## AudioRecorderConfig<sup>(deprecated)</sup> 4969 4970> **NOTE** 4971> 4972> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorderConfig](#avrecorderconfig9) instead. 4973 4974Describes audio recording configurations. 4975 4976**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4977 4978| Name | Type | Mandatory| Description | 4979| ----------------------------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 4980| audioEncoder | [AudioEncoder](#audioencoderdeprecated) | No | Audio encoding format. The default value is **AAC_LC**.<br>**Note**: This parameter is deprecated since API version 8. Use **audioEncoderMime** instead.| 4981| audioEncodeBitRate | number | No | Audio encoding bit rate. The default value is **48000**. | 4982| audioSampleRate | number | No | Audio sampling rate. The default value is **48000**. | 4983| numberOfChannels | number | No | Number of audio channels. The default value is **2**. | 4984| format | [AudioOutputFormat](#audiooutputformatdeprecated) | No | Audio output format. The default value is **MPEG_4**.<br>**Note**: This parameter is deprecated since API version 8. Use **fileFormat** instead.| 4985| location | [Location](#location) | No | Geographical location of the recorded audio. | 4986| uri | string | Yes | Audio output URI. Supported: fd://xx (fd number)<br>![](figures/en-us_image_url.png) <br>The file must be created by the caller and granted with proper permissions.| 4987| audioEncoderMime<sup>8+</sup> | [CodecMimeType](#codecmimetype8) | No | Audio encoding format. | 4988| fileFormat<sup>8+</sup> | [ContainerFormatType](#containerformattype8) | No | Audio encoding format. | 4989 4990## AudioEncoder<sup>(deprecated)</sup> 4991 4992> **NOTE** 4993> 4994> This API is supported since API version 6 and deprecated since API version 8. You are advised to use [CodecMimeType](#codecmimetype8) instead. 4995 4996Enumerates the audio encoding formats. 4997 4998**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4999 5000| Name | Value | Description | 5001| ------- | ---- | ------------------------------------------------------------ | 5002| DEFAULT | 0 | Default encoding format.<br>This API is defined but not implemented yet. | 5003| AMR_NB | 1 | AMR-NB.<br>This API is defined but not implemented yet.| 5004| AMR_WB | 2 | Adaptive Multi Rate-Wide Band Speech Codec (AMR-WB).<br>This API is defined but not implemented yet.| 5005| AAC_LC | 3 | Advanced Audio Coding Low Complexity (AAC-LC).| 5006| HE_AAC | 4 | High-Efficiency Advanced Audio Coding (HE_AAC).<br>This API is defined but not implemented yet.| 5007 5008## AudioOutputFormat<sup>(deprecated)</sup> 5009 5010> **NOTE** 5011> 5012> This API is supported since API version 6 and deprecated since API version 8. You are advised to use [ContainerFormatType](#containerformattype8) instead. 5013 5014Enumerates the audio output formats. 5015 5016**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 5017 5018| Name | Value | Description | 5019| -------- | ---- | ------------------------------------------------------------ | 5020| DEFAULT | 0 | Default encapsulation format.<br>This API is defined but not implemented yet. | 5021| MPEG_4 | 2 | MPEG-4. | 5022| AMR_NB | 3 | AMR_NB.<br>This API is defined but not implemented yet. | 5023| AMR_WB | 4 | AMR_WB.<br>This API is defined but not implemented yet. | 5024| AAC_ADTS | 6 | Audio Data Transport Stream (ADTS), which is a transport stream format of AAC-based audio.| 5025