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 camera APIs, 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 camera APIs, 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 AVPlayer demo, see [AVPlayer Development](../../media/avplayer-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><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: | Abnormal stream. | 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.| 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.| 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.| 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 [AVPlayer Development](../../media/avplayer-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 AVRecorder demo, see [AVRecorder Development](../../media/avrecorder.md). 1704 1705To use the camera to record videos, the camera module is required. For details about how to use the camera APIs, see [Camera Management](js-apis-camera.md). 1706 1707### Attributes 1708 1709**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1710 1711| Name | Type | Readable| Writable| Description | 1712| ------- | ------------------------------------ | ---- | ---- | ------------------ | 1713| state9+ | [AVRecorderState](#avrecorderstate9) | Yes | No | AVRecorder state.| 1714 1715### prepare<sup>9+</sup><a name=avrecorder_prepare></a> 1716 1717prepare(config: AVRecorderConfig, callback: AsyncCallback\<void>): void 1718 1719Sets audio and video recording parameters. This API uses an asynchronous callback to return the result. 1720 1721**Required permissions:** ohos.permission.MICROPHONE 1722 1723This permission is required only if audio recording is involved. 1724 1725**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1726 1727**Parameters** 1728 1729| Name | Type | Mandatory| Description | 1730| -------- | -------------------------------------- | ---- | ------------------------------------- | 1731| config | [AVRecorderConfig](#avrecorderconfig9) | Yes | Audio and video recording parameters to set. | 1732| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 1733 1734**Error codes** 1735 1736For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1737 1738| ID| Error Message | 1739| -------- | --------------------------------------- | 1740| 201 | Permission denied. Return by callback. | 1741| 401 | Parameter error. Return by callback. | 1742| 5400102 | Operate not permit. Return by callback. | 1743| 5400105 | Service died. Return by callback. | 1744 1745**Example** 1746 1747```js 1748// Configure the parameters based on those supported by the hardware device. 1749let AVRecorderProfile = { 1750 audioBitrate : 48000, 1751 audioChannels : 2, 1752 audioCodec : media.CodecMimeType.AUDIO_AAC, 1753 audioSampleRate : 48000, 1754 fileFormat : media.ContainerFormatType.CFT_MPEG_4, 1755 videoBitrate : 2000000, 1756 videoCodec : media.CodecMimeType.VIDEO_MPEG4, 1757 videoFrameWidth : 640, 1758 videoFrameHeight : 480, 1759 videoFrameRate : 30 1760} 1761let AVRecorderConfig = { 1762 audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, 1763 videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 1764 profile : AVRecorderProfile, 1765 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. 1766 rotation: 0, // The value can be 0, 90, 180, or 270. If any other value is used, prepare() reports an error. 1767 location : { latitude : 30, longitude : 130 } 1768} 1769 1770avRecorder.prepare(AVRecorderConfig, (err) => { 1771 if (err == null) { 1772 console.info('prepare success'); 1773 } else { 1774 console.info('prepare failed and error is ' + err.message); 1775 } 1776}) 1777``` 1778 1779### prepare<sup>9+</sup> 1780 1781prepare(config: AVRecorderConfig): Promise\<void> 1782 1783Sets audio and video recording parameters. This API uses a promise to return the result. 1784 1785**Required permissions:** ohos.permission.MICROPHONE 1786 1787This permission is required only if audio recording is involved. 1788 1789**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1790 1791**Parameters** 1792 1793| Name| Type | Mandatory| Description | 1794| ------ | -------------------------------------- | ---- | -------------------------- | 1795| config | [AVRecorderConfig](#avrecorderconfig9) | Yes | Audio and video recording parameters to set.| 1796 1797**Return value** 1798 1799| Type | Description | 1800| -------------- | ------------------------------------------ | 1801| Promise\<void> | Promise used to return the result.| 1802 1803**Error codes** 1804 1805For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1806 1807| ID| Error Message | 1808| -------- | -------------------------------------- | 1809| 201 | Permission denied. Return by promise. | 1810| 401 | Parameter error. Return by promise. | 1811| 5400102 | Operate not permit. Return by promise. | 1812| 5400105 | Service died. Return by promise. | 1813 1814**Example** 1815 1816```js 1817// Configure the parameters based on those supported by the hardware device. 1818let AVRecorderProfile = { 1819 audioBitrate : 48000, 1820 audioChannels : 2, 1821 audioCodec : media.CodecMimeType.AUDIO_AAC, 1822 audioSampleRate : 48000, 1823 fileFormat : media.ContainerFormatType.CFT_MPEG_4, 1824 videoBitrate : 2000000, 1825 videoCodec : media.CodecMimeType.VIDEO_MPEG4, 1826 videoFrameWidth : 640, 1827 videoFrameHeight : 480, 1828 videoFrameRate : 30 1829} 1830let AVRecorderConfig = { 1831 audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, 1832 videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 1833 profile : AVRecorderProfile, 1834 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. 1835 rotation: 0, // The value can be 0, 90, 180, or 270. If any other value is used, prepare() reports an error. 1836 location : { latitude : 30, longitude : 130 } 1837} 1838 1839avRecorder.prepare(AVRecorderConfig).then(() => { 1840 console.info('prepare success'); 1841}).catch((err) => { 1842 console.info('prepare failed and catch error is ' + err.message); 1843}); 1844 1845``` 1846 1847### getInputSurface<sup>9+</sup><a name=avrecorder_getinputsurface></a> 1848 1849getInputSurface(callback: AsyncCallback\<string>): void 1850 1851Obtains 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. 1852 1853Note 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. 1854 1855This API can be called only after the **prepare()** API is called. 1856 1857**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1858 1859**Parameters** 1860 1861| Name | Type | Mandatory| Description | 1862| -------- | ---------------------- | ---- | --------------------------- | 1863| callback | AsyncCallback\<string> | Yes | Callback used to obtain the result.| 1864 1865**Error codes** 1866 1867For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1868 1869| ID| Error Message | 1870| -------- | --------------------------------------- | 1871| 5400102 | Operate not permit. Return by callback. | 1872| 5400103 | IO error. Return by callback. | 1873| 5400105 | Service died. Return by callback. | 1874 1875**Example** 1876 1877```js 1878let surfaceID = null; // The surfaceID is transferred to the camera API to create a videoOutput instance. 1879 1880avRecorder.getInputSurface((err, surfaceId) => { 1881 if (err == null) { 1882 console.info('getInputSurface success'); 1883 surfaceID = surfaceId; 1884 } else { 1885 console.info('getInputSurface failed and error is ' + err.message); 1886 } 1887}); 1888 1889``` 1890 1891### getInputSurface<sup>9+</sup> 1892 1893getInputSurface(): Promise\<string> 1894 1895Obtains 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. 1896 1897Note 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. 1898 1899This API can be called only after the **prepare()** API is called. 1900 1901**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1902 1903**Return value** 1904 1905| Type | Description | 1906| ---------------- | -------------------------------- | 1907| Promise\<string> | Promise used to return the result.| 1908 1909**Error codes** 1910 1911For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1912 1913| ID| Error Message | 1914| -------- | -------------------------------------- | 1915| 5400102 | Operate not permit. Return by promise. | 1916| 5400103 | IO error. Return by promise. | 1917| 5400105 | Service died. Return by promise. | 1918 1919**Example** 1920 1921```js 1922let surfaceID = null; // The surfaceID is transferred to the camera API to create a videoOutput instance. 1923 1924avRecorder.getInputSurface().then((surfaceId) => { 1925 console.info('getInputSurface success'); 1926 surfaceID = surfaceId; 1927}).catch((err) => { 1928 console.info('getInputSurface failed and catch error is ' + err.message); 1929}); 1930``` 1931 1932### start<sup>9+</sup><a name=avrecorder_start></a> 1933 1934start(callback: AsyncCallback\<void>): void 1935 1936Starts recording. This API uses an asynchronous callback to return the result. 1937 1938For 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. 1939 1940**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1941 1942**Parameters** 1943 1944| Name | Type | Mandatory| Description | 1945| -------- | -------------------- | ---- | ---------------------------- | 1946| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 1947 1948**Error codes** 1949 1950For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1951 1952| ID| Error Message | 1953| -------- | --------------------------------------- | 1954| 5400102 | Operate not permit. Return by callback. | 1955| 5400103 | IO error. Return by callback. | 1956| 5400105 | Service died. Return by callback. | 1957 1958**Example** 1959 1960```js 1961avRecorder.start((err) => { 1962 if (err == null) { 1963 console.info('start AVRecorder success'); 1964 } else { 1965 console.info('start AVRecorder failed and error is ' + err.message); 1966 } 1967}); 1968``` 1969 1970### start<sup>9+</sup> 1971 1972start(): Promise\<void> 1973 1974Starts recording. This API uses a promise to return the result. 1975 1976For 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. 1977 1978**System capability**: SystemCapability.Multimedia.Media.AVRecorder 1979 1980**Return value** 1981 1982| Type | Description | 1983| -------------- | ------------------------------------- | 1984| Promise\<void> | Promise used to return the result.| 1985 1986**Error codes** 1987 1988For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1989 1990| ID| Error Message | 1991| -------- | -------------------------------------- | 1992| 5400102 | Operate not permit. Return by promise. | 1993| 5400103 | IO error. Return by promise. | 1994| 5400105 | Service died. Return by promise. | 1995 1996**Example** 1997 1998```js 1999avRecorder.start().then(() => { 2000 console.info('start AVRecorder success'); 2001}).catch((err) => { 2002 console.info('start AVRecorder failed and catch error is ' + err.message); 2003}); 2004``` 2005 2006### pause<sup>9+</sup><a name=avrecorder_pause></a> 2007 2008pause(callback: AsyncCallback\<void>): void 2009 2010Pauses recording. This API uses an asynchronous callback to return the result. 2011 2012This API can be called only after the **start()** API is called. You can call **resume()** to resume recording. 2013 2014**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2015 2016**Parameters** 2017 2018| Name | Type | Mandatory| Description | 2019| -------- | -------------------- | ---- | --------------------------- | 2020| callback | AsyncCallback\<void> | Yes | Callback used to obtain the result.| 2021 2022**Error codes** 2023 2024For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2025 2026| ID| Error Message | 2027| -------- | --------------------------------------- | 2028| 5400102 | Operate not permit. Return by callback. | 2029| 5400103 | IO error. Return by callback. | 2030| 5400105 | Service died. Return by callback. | 2031 2032**Example** 2033 2034```js 2035avRecorder.pause((err) => { 2036 if (err == null) { 2037 console.info('pause AVRecorder success'); 2038 } else { 2039 console.info('pause AVRecorder failed and error is ' + err.message); 2040 } 2041}); 2042``` 2043 2044### pause<sup>9+</sup> 2045 2046pause(): Promise\<void> 2047 2048Pauses recording. This API uses a promise to return the result. 2049 2050This API can be called only after the **start()** API is called. You can call **resume()** to resume recording. 2051 2052**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2053 2054**Return value** 2055 2056| Type | Description | 2057| -------------- | ------------------------------------- | 2058| Promise\<void> | Promise used to return the result.| 2059 2060**Error codes** 2061 2062For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2063 2064| ID| Error Message | 2065| -------- | -------------------------------------- | 2066| 5400102 | Operate not permit. Return by promise. | 2067| 5400103 | IO error. Return by promise. | 2068| 5400105 | Service died. Return by promise. | 2069 2070**Example** 2071 2072```js 2073avRecorder.pause().then(() => { 2074 console.info('pause AVRecorder success'); 2075}).catch((err) => { 2076 console.info('pause AVRecorder failed and catch error is ' + err.message); 2077}); 2078``` 2079 2080### resume<sup>9+</sup><a name=avrecorder_resume></a> 2081 2082resume(callback: AsyncCallback\<void>): void 2083 2084Resumes recording. This API uses an asynchronous callback to return the result. 2085 2086This API can be called only after the **pause()** API is called. 2087 2088**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2089 2090**Parameters** 2091 2092| Name | Type | Mandatory| Description | 2093| -------- | -------------------- | ---- | ---------------------------- | 2094| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2095 2096**Error codes** 2097 2098For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2099 2100| ID| Error Message | 2101| -------- | --------------------------------------- | 2102| 5400102 | Operate not permit. Return by callback. | 2103| 5400103 | IO error. Return by callback. | 2104| 5400105 | Service died. Return by callback. | 2105 2106**Example** 2107 2108```js 2109avRecorder.resume((err) => { 2110 if (err == null) { 2111 console.info('resume AVRecorder success'); 2112 } else { 2113 console.info('resume AVRecorder failed and error is ' + err.message); 2114 } 2115}); 2116``` 2117 2118### resume<sup>9+</sup> 2119 2120resume(): Promise\<void> 2121 2122Resumes recording. This API uses a promise to return the result. 2123 2124This API can be called only after the **pause()** API is called. 2125 2126**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2127 2128**Return value** 2129 2130| Type | Description | 2131| -------------- | ------------------------------------- | 2132| Promise\<void> | Promise used to return the result.| 2133 2134**Error codes** 2135 2136For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2137 2138| ID| Error Message | 2139| -------- | -------------------------------------- | 2140| 5400102 | Operate not permit. Return by promise. | 2141| 5400103 | IO error. Return by promise. | 2142| 5400105 | Service died. Return by promise. | 2143 2144**Example** 2145 2146```js 2147avRecorder.resume().then(() => { 2148 console.info('resume AVRecorder success'); 2149}).catch((err) => { 2150 console.info('resume AVRecorder failed and catch error is ' + err.message); 2151}); 2152``` 2153 2154### stop<sup>9+</sup><a name=avrecorder_stop></a> 2155 2156stop(callback: AsyncCallback\<void>): void 2157 2158Stops recording. This API uses an asynchronous callback to return the result. 2159 2160This API can be called only after the **start()** or **pause()** API is called. 2161 2162For 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. 2163 2164**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2165 2166**Parameters** 2167 2168| Name | Type | Mandatory| Description | 2169| -------- | -------------------- | ---- | ---------------------------- | 2170| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2171 2172**Error codes** 2173 2174For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2175 2176| ID| Error Message | 2177| -------- | --------------------------------------- | 2178| 5400102 | Operate not permit. Return by callback. | 2179| 5400103 | IO error. Return by callback. | 2180| 5400105 | Service died. Return by callback. | 2181 2182**Example** 2183 2184```js 2185avRecorder.stop((err) => { 2186 if (err == null) { 2187 console.info('stop AVRecorder success'); 2188 } else { 2189 console.info('stop AVRecorder failed and error is ' + err.message); 2190 } 2191}); 2192``` 2193 2194### stop<sup>9+</sup> 2195 2196stop(): Promise\<void> 2197 2198Stops recording. This API uses a promise to return the result. 2199 2200This API can be called only after the **start()** or **pause()** API is called. 2201 2202For 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. 2203 2204**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2205 2206**Return value** 2207 2208| Type | Description | 2209| -------------- | ------------------------------------- | 2210| Promise\<void> | Promise used to return the result.| 2211 2212**Error codes** 2213 2214For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2215 2216| ID| Error Message | 2217| -------- | -------------------------------------- | 2218| 5400102 | Operate not permit. Return by promise. | 2219| 5400103 | IO error. Return by promise. | 2220| 5400105 | Service died. Return by promise. | 2221 2222**Example** 2223 2224```js 2225avRecorder.stop().then(() => { 2226 console.info('stop AVRecorder success'); 2227}).catch((err) => { 2228 console.info('stop AVRecorder failed and catch error is ' + err.message); 2229}); 2230``` 2231 2232### reset<sup>9+</sup><a name=avrecorder_reset></a> 2233 2234reset(callback: AsyncCallback\<void>): void 2235 2236Resets audio and video recording. This API uses an asynchronous callback to return the result. 2237 2238For 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. 2239 2240**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2241 2242**Parameters** 2243 2244| Name | Type | Mandatory| Description | 2245| -------- | -------------------- | ---- | ------------------------------ | 2246| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2247 2248**Error codes** 2249 2250For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2251 2252| ID| Error Message | 2253| -------- | --------------------------------- | 2254| 5400103 | IO error. Return by callback. | 2255| 5400105 | Service died. Return by callback. | 2256 2257**Example** 2258 2259```js 2260avRecorder.reset((err) => { 2261 if (err == null) { 2262 console.info('reset AVRecorder success'); 2263 } else { 2264 console.info('reset AVRecorder failed and error is ' + err.message); 2265 } 2266}); 2267``` 2268 2269### reset<sup>9+</sup> 2270 2271reset(): Promise\<void> 2272 2273Resets audio and video recording. This API uses a promise to return the result. 2274 2275For 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. 2276 2277**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2278 2279**Return value** 2280 2281| Type | Description | 2282| -------------- | --------------------------------------- | 2283| Promise\<void> | Promise used to return the result.| 2284 2285**Error codes** 2286 2287For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2288 2289| ID| Error Message | 2290| -------- | -------------------------------- | 2291| 5400103 | IO error. Return by promise. | 2292| 5400105 | Service died. Return by promise. | 2293 2294**Example** 2295 2296```js 2297avRecorder.reset().then(() => { 2298 console.info('reset AVRecorder success'); 2299}).catch((err) => { 2300 console.info('reset AVRecorder failed and catch error is ' + err.message); 2301}); 2302``` 2303 2304### release<sup>9+</sup><a name=avrecorder_release></a> 2305 2306release(callback: AsyncCallback\<void>): void 2307 2308Releases the audio and video recording resources. This API uses an asynchronous callback to return the result. 2309 2310After the resources are released, you can no longer perform any operation on the **AVRecorder** instance. 2311 2312**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2313 2314**Parameters** 2315 2316| Name | Type | Mandatory| Description | 2317| -------- | -------------------- | ---- | ---------------------------------- | 2318| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2319 2320**Error codes** 2321 2322For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2323 2324| ID| Error Message | 2325| -------- | --------------------------------- | 2326| 5400105 | Service died. Return by callback. | 2327 2328**Example** 2329 2330```js 2331avRecorder.release((err) => { 2332 if (err == null) { 2333 console.info('release AVRecorder success'); 2334 } else { 2335 console.info('release AVRecorder failed and error is ' + err.message); 2336 } 2337}); 2338``` 2339 2340### release<sup>9+</sup> 2341 2342release(): Promise\<void> 2343 2344Releases the audio and video recording resources. This API uses a promise to return the result. 2345 2346After the resources are released, you can no longer perform any operation on the **AVRecorder** instance. 2347 2348**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2349 2350**Return value** 2351 2352| Type | Description | 2353| -------------- | ------------------------------------------- | 2354| Promise\<void> | Promise used to return the result.| 2355 2356**Error codes** 2357 2358For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2359 2360| ID| Error Message | 2361| -------- | --------------------------------- | 2362| 5400105 | Service died. Return by callback. | 2363 2364**Example** 2365 2366```js 2367avRecorder.release().then(() => { 2368 console.info('release AVRecorder success'); 2369}).catch((err) => { 2370 console.info('release AVRecorder failed and catch error is ' + err.message); 2371}); 2372``` 2373 2374### on('stateChange')<sup>9+</sup><a name=avrecorder_onstatechange></a> 2375 2376on(type: 'stateChange', callback: (state: AVRecorderState, reason: StateChangeReason) => void): void 2377 2378Subscribes 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. 2379 2380**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2381 2382**Parameters** 2383 2384| Name | Type | Mandatory| Description | 2385| -------- | -------- | ---- | ------------------------------------------------------------ | 2386| type | string | Yes | Event type, which is **'stateChange'** in this case. This event can be triggered by both user operations and the system.| 2387| 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.| 2388 2389**Example** 2390 2391```js 2392avRecorder.on('stateChange', async (state, reason) => { 2393 console.info('case state has changed, new state is :' + state + ',and new reason is : ' + reason); 2394}); 2395``` 2396 2397### off('stateChange')<sup>9+</sup> 2398 2399off(type: 'stateChange'): void 2400 2401Unsubscribes from AVRecorder state changes. 2402 2403**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2404 2405**Parameters** 2406 2407| Name| Type | Mandatory| Description | 2408| ------ | ------ | ---- | ------------------------------------------------------------ | 2409| type | string | Yes | Event type, which is **'stateChange'** in this case. This event can be triggered by both user operations and the system.| 2410 2411**Example** 2412 2413```js 2414avRecorder.off('stateChange'); 2415``` 2416 2417### on('error')<sup>9+</sup><a name=avrecorder_onerror></a> 2418 2419on(type: 'error', callback: ErrorCallback): void 2420 2421Subscribes 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. 2422 2423An application can subscribe to only one AVRecorder error event. When the application initiates multiple subscriptions to this event, the last subscription prevails. 2424 2425**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2426 2427**Parameters** 2428 2429| Name | Type | Mandatory| Description | 2430| -------- | ------------- | ---- | ------------------------------------------------------------ | 2431| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during recording.| 2432| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 2433 2434**Error codes** 2435 2436For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2437 2438| ID| Error Message | 2439| -------- | --------------------------------- | 2440| 5400103 | IO error. Return by callback. | 2441| 5400105 | Service died. Return by callback. | 2442 2443**Example** 2444 2445```js 2446avRecorder.on('error', (err) => { 2447 console.info('case avRecorder.on(error) called, errMessage is ' + err.message); 2448}); 2449``` 2450 2451### off('error')<sup>9+</sup> 2452 2453off(type: 'error'): void 2454 2455Unsubscribes from AVRecorder errors. After the unsubscription, your application can no longer receive AVRecorder errors. 2456 2457**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2458 2459**Parameters** 2460 2461| Name| Type | Mandatory| Description | 2462| ------ | ------ | ---- | ------------------------------------------------------------ | 2463| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during recording.| 2464 2465**Error codes** 2466 2467For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2468 2469| ID| Error Message | 2470| -------- | --------------------------------- | 2471| 5400103 | IO error. Return by callback. | 2472| 5400105 | Service died. Return by callback. | 2473 2474**Example** 2475 2476```js 2477avRecorder.off('error'); 2478``` 2479 2480## AVRecorderState<sup>9+</sup> 2481 2482Enumerates the AVRecorder states. You can obtain the state through the **state** attribute. 2483 2484**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2485 2486| Name | Type | Description | 2487| -------- | ------ | ------------------------------------------------------------ | 2488| 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. | 2489| prepared | string | The AVRecorder enters this state when the parameters are set. In this state, you can call [start()](#avrecorder_start) to start recording.| 2490| 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.| 2491| 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.| 2492| stopped | string | The AVRecorder enters this state when the recording stops. In this state, you can call [prepare()](#avrecorder_prepare) to set recording parameters.| 2493| 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.| 2494| 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.| 2495 2496## AVRecorderConfig<sup>9+</sup> 2497 2498Describes the audio and video recording parameters. 2499 2500**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2501 2502| Name | Type | Mandatory| Description | 2503| --------------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | 2504| audioSourceType | [AudioSourceType](#audiosourcetype9) | No | Type of the audio source to record. This parameter is mandatory for audio recording. | 2505| videoSourceType | [VideoSourceType](#videosourcetype9) | No | Type of the video source to record. This parameter is mandatory for video recording. | 2506| profile | [AVRecorderProfile](#avrecorderprofile9) | Yes | Recording profile. This parameter is mandatory. | 2507| url | string | Yes | Recording output URL: fd://xx (fd number).<br><br>This parameter is mandatory.| 2508| rotation | number | No | Rotation angle of the recorded video. The value can only be 0, 90, 180, or 270. | 2509| location | [Location](#location) | No | Geographical location of the recorded video. | 2510 2511The **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**. 2512 2513## AVRecorderProfile<sup>9+</sup> 2514 2515Describes the audio and video recording profile. 2516 2517**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2518 2519| Name | Type | Mandatory| Description | 2520| ---------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 2521| audioBitrate | number | No | Audio encoding bit rate. This parameter is mandatory for audio recording. | 2522| audioChannels | number | No | Number of audio channels. This parameter is mandatory for audio recording. | 2523| audioCodec | [CodecMimeType](#codecmimetype8) | No | Audio encoding format. This parameter is mandatory for audio recording. Only **AUDIO_AAC** is supported. | 2524| audioSampleRate | number | No | Audio sampling rate. This parameter is mandatory for audio recording. | 2525| fileFormat | [ContainerFormatType](#containerformattype8) | Yes | Container format of a file. This parameter is mandatory. | 2526| videoBitrate | number | No | Video encoding bit rate. This parameter is mandatory for video recording. | 2527| videoCodec | [CodecMimeType](#codecmimetype8) | No | Video encoding format. This parameter is mandatory for video recording. Only **VIDEO_AVC** and **VIDEO_MPEG4** are supported.| 2528| videoFrameWidth | number | No | Width of a video frame. This parameter is mandatory for video recording. | 2529| videoFrameHeight | number | No | Height of a video frame. This parameter is mandatory for video recording. | 2530| videoFrameRate | number | No | Video frame rate. This parameter is mandatory for video recording. | 2531 2532## AudioSourceType<sup>9+</sup> 2533 2534Enumerates the audio source types for video recording. 2535 2536**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2537 2538| Name | Value | Description | 2539| ------------------------- | ---- | ---------------------- | 2540| AUDIO_SOURCE_TYPE_DEFAULT | 0 | Default audio input source.| 2541| AUDIO_SOURCE_TYPE_MIC | 1 | Mic audio input source. | 2542 2543## VideoSourceType<sup>9+</sup> 2544 2545Enumerates the video source types for video recording. 2546 2547**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2548 2549| Name | Value | Description | 2550| ----------------------------- | ---- | ------------------------------- | 2551| VIDEO_SOURCE_TYPE_SURFACE_YUV | 0 | The input surface carries raw data.| 2552| VIDEO_SOURCE_TYPE_SURFACE_ES | 1 | The input surface carries ES data. | 2553 2554## ContainerFormatType<sup>8+</sup> 2555 2556Enumerates the container format types (CFTs). 2557 2558**System capability**: SystemCapability.Multimedia.Media.Core 2559 2560| Name | Value | Description | 2561| ----------- | ----- | --------------------- | 2562| CFT_MPEG_4 | 'mp4' | Video container format MP4.| 2563| CFT_MPEG_4A | 'm4a' | Audio container format M4A.| 2564 2565## Location 2566 2567Describes the geographical location of the recorded video. 2568 2569**System capability**: SystemCapability.Multimedia.Media.Core 2570 2571| Name | Type | Mandatory| Description | 2572| --------- | ------ | ---- | ---------------- | 2573| latitude | number | Yes | Latitude of the geographical location.| 2574| longitude | number | Yes | Longitude of the geographical location.| 2575 2576## VideoRecorder<sup>9+</sup> 2577 2578> **NOTE** 2579> 2580> This class is deprecated after AVRecorder<sup>9+</sup> is released. You are advised to use [AVRecorder](#avrecorder9) instead. 2581 2582Implements video recording. Before calling any API in the **VideoRecorder** class, you must use [createVideoRecorder()](#mediacreatevideorecorder9) to create a [VideoRecorder](#videorecorder9) instance. 2583 2584For details about the video recording demo, see [Video Recording Development](../../media/video-recorder.md). 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/mp4v-es', 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/mp4v-es', 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> | 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><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 fileio from '@ohos.fileio' 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'; 3830fileio.open(path).then((fdValue) => { 3831 fdPath = fdPath + '' + fdValue; 3832 console.info('open fd success fd is' + fdPath); 3833}, (err) => { 3834 console.info('open fd failed err is' + err); 3835}).catch((err) => { 3836 console.info('open fd failed err is' + err); 3837}); 3838audioPlayer.src = fdPath; // Set the src attribute and trigger the 'dataLoad' event callback. 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 3919For details about the video playback demo, see [Video Playback Development](../../media/video-playback.md). 3920 3921### Attributes<a name=videoplayer_attributes></a> 3922 3923**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 3924 3925| Name | Type | Readable| Writable| Description | 3926| ------------------------------- | ------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 3927| 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><br>2. HTTP: http://xx<br>3. HTTPS: https://xx<br>4. HLS: http://xx or https://xx<br>| 3928| 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>| 3929| 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. | 3930| videoScaleType<sup>9+</sup> | [VideoScaleType](#videoscaletype9) | Yes | Yes | Video scale type. | 3931| audioInterruptMode<sup>9+</sup> | [audio.InterruptMode](js-apis-audio.md#interruptmode9) | Yes | Yes | Audio interruption mode. | 3932| currentTime<sup>8+</sup> | number | Yes | No | Current video playback position, in ms. | 3933| duration<sup>8+</sup> | number | Yes | No | Video duration, in ms. The value **-1** indicates the live mode. | 3934| state<sup>8+</sup> | [VideoPlayState](#videoplayerstate) | Yes | No | Video playback state. | 3935| width<sup>8+</sup> | number | Yes | No | Video width, in pixels. | 3936| height<sup>8+</sup> | number | Yes | No | Video height, in pixels. | 3937 3938### setDisplaySurface<sup>8+</sup> 3939 3940setDisplaySurface(surfaceId: string, callback: AsyncCallback\<void>): void 3941 3942Sets **SurfaceId**. This API uses an asynchronous callback to return the result. 3943 3944**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. 3945 3946**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 3947 3948**Parameters** 3949 3950| Name | Type | Mandatory| Description | 3951| --------- | -------------------- | ---- | ------------------------- | 3952| surfaceId | string | Yes | Surface ID to set. | 3953| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3954 3955**Example** 3956 3957```js 3958let surfaceId = null; 3959videoPlayer.setDisplaySurface(surfaceId, (err) => { 3960 if (err == null) { 3961 console.info('setDisplaySurface success!'); 3962 } else { 3963 console.info('setDisplaySurface fail!'); 3964 } 3965}); 3966``` 3967 3968### setDisplaySurface<sup>8+</sup> 3969 3970setDisplaySurface(surfaceId: string): Promise\<void> 3971 3972Sets **SurfaceId**. This API uses a promise to return the result. 3973 3974**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. 3975 3976**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 3977 3978**Parameters** 3979 3980| Name | Type | Mandatory| Description | 3981| --------- | ------ | ---- | --------- | 3982| surfaceId | string | Yes | Surface ID to set.| 3983 3984**Return value** 3985 3986| Type | Description | 3987| -------------- | ------------------------------ | 3988| Promise\<void> | Promise used to return the result.| 3989 3990**Example** 3991 3992```js 3993let surfaceId = null; 3994videoPlayer.setDisplaySurface(surfaceId).then(() => { 3995 console.info('setDisplaySurface success'); 3996}).catch((error) => { 3997 console.info(`video catchCallback, error:${error}`); 3998}); 3999``` 4000 4001### prepare<sup>8+</sup> 4002 4003prepare(callback: AsyncCallback\<void>): void 4004 4005Prepares for video playback. This API uses an asynchronous callback to return the result. 4006 4007**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4008 4009**Parameters** 4010 4011| Name | Type | Mandatory| Description | 4012| -------- | -------------------- | ---- | ------------------------ | 4013| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4014 4015**Example** 4016 4017```js 4018videoPlayer.prepare((err) => { 4019 if (err == null) { 4020 console.info('prepare success!'); 4021 } else { 4022 console.info('prepare fail!'); 4023 } 4024}); 4025``` 4026 4027### prepare<sup>8+</sup> 4028 4029prepare(): Promise\<void> 4030 4031Prepares for video playback. This API uses a promise to return the result. 4032 4033**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4034 4035**Return value** 4036 4037| Type | Description | 4038| -------------- | ----------------------------- | 4039| Promise\<void> | Promise used to return the result.| 4040 4041**Example** 4042 4043```js 4044videoPlayer.prepare().then(() => { 4045 console.info('prepare success'); 4046}).catch((error) => { 4047 console.info(`video catchCallback, error:${error}`); 4048}); 4049``` 4050 4051### play<sup>8+</sup> 4052 4053play(callback: AsyncCallback\<void>): void; 4054 4055Starts to play video assets. This API uses an asynchronous callback to return the result. 4056 4057**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4058 4059**Parameters** 4060 4061| Name | Type | Mandatory| Description | 4062| -------- | -------------------- | ---- | ------------------------ | 4063| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4064 4065**Example** 4066 4067```js 4068videoPlayer.play((err) => { 4069 if (err == null) { 4070 console.info('play success!'); 4071 } else { 4072 console.info('play fail!'); 4073 } 4074}); 4075``` 4076 4077### play<sup>8+</sup> 4078 4079play(): Promise\<void>; 4080 4081Starts to play video assets. This API uses a promise to return the result. 4082 4083**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4084 4085**Return value** 4086 4087| Type | Description | 4088| -------------- | ----------------------------- | 4089| Promise\<void> | Promise used to return the result.| 4090 4091**Example** 4092 4093```js 4094videoPlayer.play().then(() => { 4095 console.info('play success'); 4096}).catch((error) => { 4097 console.info(`video catchCallback, error:${error}`); 4098}); 4099``` 4100 4101### pause<sup>8+</sup> 4102 4103pause(callback: AsyncCallback\<void>): void 4104 4105Pauses video playback. This API uses an asynchronous callback to return the result. 4106 4107**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4108 4109**Parameters** 4110 4111| Name | Type | Mandatory| Description | 4112| -------- | -------------------- | ---- | ------------------------ | 4113| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4114 4115**Example** 4116 4117```js 4118videoPlayer.pause((err) => { 4119 if (err == null) { 4120 console.info('pause success!'); 4121 } else { 4122 console.info('pause fail!'); 4123 } 4124}); 4125``` 4126 4127### pause<sup>8+</sup> 4128 4129pause(): Promise\<void> 4130 4131Pauses video playback. This API uses a promise to return the result. 4132 4133**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4134 4135**Return value** 4136 4137| Type | Description | 4138| -------------- | ----------------------------- | 4139| Promise\<void> | Promise used to return the result.| 4140 4141**Example** 4142 4143```js 4144videoPlayer.pause().then(() => { 4145 console.info('pause success'); 4146}).catch((error) => { 4147 console.info(`video catchCallback, error:${error}`); 4148}); 4149``` 4150 4151### stop<sup>8+</sup> 4152 4153stop(callback: AsyncCallback\<void>): void 4154 4155Stops video playback. This API uses an asynchronous callback to return the result. 4156 4157**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4158 4159**Parameters** 4160 4161| Name | Type | Mandatory| Description | 4162| -------- | -------------------- | ---- | ------------------------ | 4163| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4164 4165**Example** 4166 4167```js 4168videoPlayer.stop((err) => { 4169 if (err == null) { 4170 console.info('stop success!'); 4171 } else { 4172 console.info('stop fail!'); 4173 } 4174}); 4175``` 4176 4177### stop<sup>8+</sup> 4178 4179stop(): Promise\<void> 4180 4181Stops video playback. This API uses a promise to return the result. 4182 4183**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4184 4185**Return value** 4186 4187| Type | Description | 4188| -------------- | ----------------------------- | 4189| Promise\<void> | Promise used to return the result.| 4190 4191**Example** 4192 4193```js 4194videoPlayer.stop().then(() => { 4195 console.info('stop success'); 4196}).catch((error) => { 4197 console.info(`video catchCallback, error:${error}`); 4198}); 4199``` 4200 4201### reset<sup>8+</sup> 4202 4203reset(callback: AsyncCallback\<void>): void 4204 4205Resets the video asset to be played. This API uses an asynchronous callback to return the result. 4206 4207**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4208 4209**Parameters** 4210 4211| Name | Type | Mandatory| Description | 4212| -------- | -------------------- | ---- | ------------------------ | 4213| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4214 4215**Example** 4216 4217```js 4218videoPlayer.reset((err) => { 4219 if (err == null) { 4220 console.info('reset success!'); 4221 } else { 4222 console.info('reset fail!'); 4223 } 4224}); 4225``` 4226 4227### reset<sup>8+</sup> 4228 4229reset(): Promise\<void> 4230 4231Resets the video asset to be played. This API uses a promise to return the result. 4232 4233**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4234 4235**Return value** 4236 4237| Type | Description | 4238| -------------- | ----------------------------- | 4239| Promise\<void> | Promise used to return the result.| 4240 4241**Example** 4242 4243```js 4244videoPlayer.reset().then(() => { 4245 console.info('reset success'); 4246}).catch((error) => { 4247 console.info(`video catchCallback, error:${error}`); 4248}); 4249``` 4250 4251### seek<sup>8+</sup> 4252 4253seek(timeMs: number, callback: AsyncCallback\<number>): void 4254 4255Seeks 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. 4256 4257**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4258 4259**Parameters** 4260 4261| Name | Type | Mandatory| Description | 4262| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 4263| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, duration].| 4264| callback | AsyncCallback\<number> | Yes | Callback used to return the result. | 4265 4266**Example** 4267 4268```js 4269let seekTime = 5000; 4270videoPlayer.seek(seekTime, (err, result) => { 4271 if (err == null) { 4272 console.info('seek success!'); 4273 } else { 4274 console.info('seek fail!'); 4275 } 4276}); 4277``` 4278 4279### seek<sup>8+</sup> 4280 4281seek(timeMs: number, mode:SeekMode, callback: AsyncCallback\<number>): void 4282 4283Seeks to the specified playback position. This API uses an asynchronous callback to return the result. 4284 4285**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4286 4287**Parameters** 4288 4289| Name | Type | Mandatory| Description | 4290| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 4291| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, duration].| 4292| mode | [SeekMode](#seekmode8) | Yes | Seek mode. | 4293| callback | AsyncCallback\<number> | Yes | Callback used to return the result. | 4294 4295**Example** 4296 4297```js 4298import media from '@ohos.multimedia.media' 4299let seekTime = 5000; 4300videoPlayer.seek(seekTime, media.SeekMode.SEEK_NEXT_SYNC, (err, result) => { 4301 if (err == null) { 4302 console.info('seek success!'); 4303 } else { 4304 console.info('seek fail!'); 4305 } 4306}); 4307``` 4308 4309### seek<sup>8+</sup> 4310 4311seek(timeMs: number, mode?:SeekMode): Promise\<number> 4312 4313Seeks 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. 4314 4315**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4316 4317**Parameters** 4318 4319| Name| Type | Mandatory| Description | 4320| ------ | ---------------------- | ---- | ------------------------------------------------------------ | 4321| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, duration].| 4322| mode | [SeekMode](#seekmode8) | No | Seek mode. | 4323 4324**Return value** 4325 4326| Type | Description | 4327| ---------------- | ------------------------------------------- | 4328| Promise\<number>| Promise used to return the playback position, in ms.| 4329 4330**Example** 4331 4332```js 4333import media from '@ohos.multimedia.media' 4334let seekTime = 5000; 4335videoPlayer.seek(seekTime).then((seekDoneTime) => { // seekDoneTime indicates the position after the seek operation is complete. 4336 console.info('seek success'); 4337}).catch((error) => { 4338 console.info(`video catchCallback, error:${error}`); 4339}); 4340 4341videoPlayer.seek(seekTime, media.SeekMode.SEEK_NEXT_SYNC).then((seekDoneTime) => { 4342 console.info('seek success'); 4343}).catch((error) => { 4344 console.info(`video catchCallback, error:${error}`); 4345}); 4346``` 4347 4348### setVolume<sup>8+</sup> 4349 4350setVolume(vol: number, callback: AsyncCallback\<void>): void 4351 4352Sets the volume. This API uses an asynchronous callback to return the result. 4353 4354**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4355 4356**Parameters** 4357 4358| Name | Type | Mandatory| Description | 4359| -------- | -------------------- | ---- | ------------------------------------------------------------ | 4360| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1.00** indicates the maximum volume (100%).| 4361| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 4362 4363**Example** 4364 4365```js 4366let vol = 0.5; 4367videoPlayer.setVolume(vol, (err, result) => { 4368 if (err == null) { 4369 console.info('setVolume success!'); 4370 } else { 4371 console.info('setVolume fail!'); 4372 } 4373}); 4374``` 4375 4376### setVolume<sup>8+</sup> 4377 4378setVolume(vol: number): Promise\<void> 4379 4380Sets the volume. This API uses a promise to return the result. 4381 4382**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4383 4384**Parameters** 4385 4386| Name| Type | Mandatory| Description | 4387| ------ | ------ | ---- | ------------------------------------------------------------ | 4388| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1.00** indicates the maximum volume (100%).| 4389 4390**Return value** 4391 4392| Type | Description | 4393| -------------- | ------------------------- | 4394| Promise\<void> | Promise used to return the result.| 4395 4396**Example** 4397 4398```js 4399let vol = 0.5; 4400videoPlayer.setVolume(vol).then(() => { 4401 console.info('setVolume success'); 4402}).catch((error) => { 4403 console.info(`video catchCallback, error:${error}`); 4404}); 4405``` 4406 4407### release<sup>8+</sup> 4408 4409release(callback: AsyncCallback\<void>): void 4410 4411Releases the video playback resources. This API uses an asynchronous callback to return the result. 4412 4413**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4414 4415**Parameters** 4416 4417| Name | Type | Mandatory| Description | 4418| -------- | -------------------- | ---- | ------------------------ | 4419| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4420 4421**Example** 4422 4423```js 4424videoPlayer.release((err) => { 4425 if (err == null) { 4426 console.info('release success!'); 4427 } else { 4428 console.info('release fail!'); 4429 } 4430}); 4431``` 4432 4433### release<sup>8+</sup> 4434 4435release(): Promise\<void> 4436 4437Releases the video playback resources. This API uses a promise to return the result. 4438 4439**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4440 4441**Return value** 4442 4443| Type | Description | 4444| -------------- | ----------------------------- | 4445| Promise\<void> | Promise used to return the result.| 4446 4447**Example** 4448 4449```js 4450videoPlayer.release().then(() => { 4451 console.info('release success'); 4452}).catch((error) => { 4453 console.info(`video catchCallback, error:${error}`); 4454}); 4455``` 4456 4457### getTrackDescription<sup>8+</sup> 4458 4459getTrackDescription(callback: AsyncCallback\<Array\<MediaDescription>>): void 4460 4461Obtains the video track information. This API uses an asynchronous callback to return the result. 4462 4463**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4464 4465**Parameters** 4466 4467| Name | Type | Mandatory| Description | 4468| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------ | 4469| callback | AsyncCallback\<Array\<[MediaDescription](#mediadescription8)>> | Yes | Callback used to return a **MediaDescription** array, which records the video track information.| 4470 4471**Example** 4472 4473```js 4474function printfDescription(obj) { 4475 for (let item in obj) { 4476 let property = obj[item]; 4477 console.info('video key is ' + item); 4478 console.info('video value is ' + property); 4479 } 4480} 4481 4482videoPlayer.getTrackDescription((error, arrList) => { 4483 if ((arrList) != null) { 4484 for (let i = 0; i < arrList.length; i++) { 4485 printfDescription(arrList[i]); 4486 } 4487 } else { 4488 console.log(`video getTrackDescription fail, error:${error}`); 4489 } 4490}); 4491``` 4492 4493### getTrackDescription<sup>8+</sup> 4494 4495getTrackDescription(): Promise\<Array\<MediaDescription>> 4496 4497Obtains the video track information. This API uses a promise to return the result. 4498 4499**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4500 4501**Return value** 4502 4503| Type | Description | 4504| ------------------------------------------------------ | ----------------------------------------------- | 4505| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return a **MediaDescription** array, which records the video track information.| 4506 4507**Example** 4508 4509```js 4510function printfDescription(obj) { 4511 for (let item in obj) { 4512 let property = obj[item]; 4513 console.info('video key is ' + item); 4514 console.info('video value is ' + property); 4515 } 4516} 4517 4518let arrayDescription; 4519videoPlayer.getTrackDescription().then((arrList) => { 4520 if (arrList != null) { 4521 arrayDescription = arrList; 4522 } else { 4523 console.log('video getTrackDescription fail'); 4524 } 4525}).catch((error) => { 4526 console.info(`video catchCallback, error:${error}`); 4527}); 4528for (let i = 0; i < arrayDescription.length; i++) { 4529 printfDescription(arrayDescription[i]); 4530} 4531``` 4532 4533### setSpeed<sup>8+</sup> 4534 4535setSpeed(speed:number, callback: AsyncCallback\<number>): void 4536 4537Sets the video playback speed. This API uses an asynchronous callback to return the result. 4538 4539**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4540 4541**Parameters** 4542 4543| Name | Type | Mandatory| Description | 4544| -------- | ---------------------- | ---- | ---------------------------------------------------------- | 4545| speed | number | Yes | Video playback speed. For details, see [PlaybackSpeed](#playbackspeed8).| 4546| callback | AsyncCallback\<number> | Yes | Callback used to return the result. | 4547 4548**Example** 4549 4550```js 4551import media from '@ohos.multimedia.media' 4552let speed = media.PlaybackSpeed.SPEED_FORWARD_2_00_X; 4553 4554videoPlayer.setSpeed(speed, (err, result) => { 4555 if (err == null) { 4556 console.info('setSpeed success!'); 4557 } else { 4558 console.info('setSpeed fail!'); 4559 } 4560}); 4561``` 4562 4563### setSpeed<sup>8+</sup> 4564 4565setSpeed(speed:number): Promise\<number> 4566 4567Sets the video playback speed. This API uses a promise to return the result. 4568 4569**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4570 4571**Parameters** 4572 4573| Name| Type | Mandatory| Description | 4574| ------ | ------ | ---- | ---------------------------------------------------------- | 4575| speed | number | Yes | Video playback speed. For details, see [PlaybackSpeed](#playbackspeed8).| 4576 4577**Return value** 4578 4579| Type | Description | 4580| ---------------- | ------------------------------------------------------------ | 4581| Promise\<number>| Promise used to return playback speed. For details, see [PlaybackSpeed](#playbackspeed8).| 4582 4583**Example** 4584 4585```js 4586import media from '@ohos.multimedia.media' 4587let speed = media.PlaybackSpeed.SPEED_FORWARD_2_00_X; 4588 4589videoPlayer.setSpeed(speed).then(() => { 4590 console.info('setSpeed success'); 4591}).catch((error) => { 4592 console.info(`video catchCallback, error:${error}`); 4593}); 4594``` 4595 4596### on('playbackCompleted')<sup>8+</sup> 4597 4598on(type: 'playbackCompleted', callback: Callback\<void>): void 4599 4600Subscribes to the video playback completion event. 4601 4602**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4603 4604**Parameters** 4605 4606| Name | Type | Mandatory| Description | 4607| -------- | -------- | ---- | ----------------------------------------------------------- | 4608| type | string | Yes | Event type, which is **'playbackCompleted'** in this case.| 4609| callback | function | Yes | Callback invoked when the event is triggered. | 4610 4611**Example** 4612 4613```js 4614videoPlayer.on('playbackCompleted', () => { 4615 console.info('playbackCompleted success!'); 4616}); 4617``` 4618 4619### on('bufferingUpdate')<sup>8+</sup> 4620 4621on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void 4622 4623Subscribes to the video buffering update event. Only network playback supports this subscription. 4624 4625**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4626 4627**Parameters** 4628 4629| Name | Type | Mandatory| Description | 4630| -------- | -------- | ---- | ------------------------------------------------------------ | 4631| type | string | Yes | Event type, which is **'bufferingUpdate'** in this case. | 4632| 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**.| 4633 4634**Example** 4635 4636```js 4637videoPlayer.on('bufferingUpdate', (infoType, value) => { 4638 console.log('video bufferingInfo type: ' + infoType); 4639 console.log('video bufferingInfo value: ' + value); 4640}); 4641``` 4642 4643### on('startRenderFrame')<sup>8+</sup> 4644 4645on(type: 'startRenderFrame', callback: Callback\<void>): void 4646 4647Subscribes to the frame rendering start event. 4648 4649**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4650 4651**Parameters** 4652 4653| Name | Type | Mandatory| Description | 4654| -------- | --------------- | ---- | ------------------------------------------------------------ | 4655| type | string | Yes | Event type, which is **'startRenderFrame'** in this case.| 4656| callback | Callback\<void> | Yes | Callback invoked when the event is triggered. | 4657 4658**Example** 4659 4660```js 4661videoPlayer.on('startRenderFrame', () => { 4662 console.info('startRenderFrame success!'); 4663}); 4664``` 4665 4666### on('videoSizeChanged')<sup>8+</sup> 4667 4668on(type: 'videoSizeChanged', callback: (width: number, height: number) => void): void 4669 4670Subscribes to the video width and height change event. 4671 4672**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4673 4674**Parameters** 4675 4676| Name | Type | Mandatory| Description | 4677| -------- | -------- | ---- | ------------------------------------------------------------ | 4678| type | string | Yes | Event type, which is **'videoSizeChanged'** in this case.| 4679| callback | function | Yes | Callback invoked when the event is triggered. **width** indicates the video width, and **height** indicates the video height. | 4680 4681**Example** 4682 4683```js 4684videoPlayer.on('videoSizeChanged', (width, height) => { 4685 console.log('video width is: ' + width); 4686 console.log('video height is: ' + height); 4687}); 4688``` 4689 4690### on('error')<sup>8+</sup> 4691 4692on(type: 'error', callback: ErrorCallback): void 4693 4694Subscribes to video playback error events. After an error event is reported, you must handle the event and exit the playback. 4695 4696**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4697 4698**Parameters** 4699 4700| Name | Type | Mandatory| Description | 4701| -------- | ------------- | ---- | ------------------------------------------------------------ | 4702| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during video playback.| 4703| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 4704 4705**Example** 4706 4707```js 4708videoPlayer.on('error', (error) => { // Set the 'error' event callback. 4709 console.info(`video error called, error: ${error}`); 4710}); 4711videoPlayer.url = 'fd://error'; // Set an incorrect URL to trigger the 'error' event. 4712``` 4713 4714## VideoPlayState<sup>(deprecated)</sup><a name=videoplayerstate></a> 4715 4716Enumerates the video playback states. You can obtain the state through the **state** attribute. 4717 4718> **NOTE** 4719> 4720> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayerState](#avplayerstate9) instead. 4721 4722**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4723 4724| Name | Type | Description | 4725| -------- | ------ | -------------- | 4726| idle | string | The video player is idle.| 4727| prepared | string | Video playback is being prepared.| 4728| playing | string | Video playback is in progress.| 4729| paused | string | Video playback is paused.| 4730| stopped | string | Video playback is stopped.| 4731| error | string | Video playback is in the error state. | 4732 4733## AudioRecorder<sup>(deprecated)</sup> 4734 4735> **NOTE** 4736> 4737> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder](#avrecorder9) instead. 4738 4739Implements audio recording. Before calling any API of **AudioRecorder**, you must use [createAudioRecorder()](#mediacreateaudiorecorder) to create an **AudioRecorder** instance. 4740 4741For details about the audio recording demo, see [Audio Recording Development](../../media/audio-recorder.md). 4742 4743### prepare<a name=audiorecorder_prepare></a> 4744 4745prepare(config: AudioRecorderConfig): void 4746 4747Prepares for recording. 4748 4749**Required permissions:** ohos.permission.MICROPHONE 4750 4751**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4752 4753**Parameters** 4754 4755| Name| Type | Mandatory| Description | 4756| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ | 4757| config | [AudioRecorderConfig](#audiorecorderconfigdeprecated) | Yes | Audio recording parameters, including the audio output URI, encoding format, sampling rate, number of audio channels, and output format.| 4758 4759**Example** 4760 4761```js 4762let audioRecorderConfig = { 4763 audioEncoder : media.AudioEncoder.AAC_LC, 4764 audioEncodeBitRate : 22050, 4765 audioSampleRate : 22050, 4766 numberOfChannels : 2, 4767 format : media.AudioOutputFormat.AAC_ADTS, 4768 uri : 'fd://1', // The file must be created by the caller and granted with proper permissions. 4769 location : { latitude : 30, longitude : 130}, 4770} 4771audioRecorder.on('prepare', () => { // Set the 'prepare' event callback. 4772 console.log('prepare success'); 4773}); 4774audioRecorder.prepare(audioRecorderConfig); 4775``` 4776 4777 4778### start<a name=audiorecorder_start></a> 4779 4780start(): void 4781 4782Starts audio recording. This API can be called only after the [prepare](#audiorecorder_on) event is triggered. 4783 4784**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4785 4786**Example** 4787 4788```js 4789audioRecorder.on('start', () => { // Set the 'start' event callback. 4790 console.log('audio recorder start success'); 4791}); 4792audioRecorder.start(); 4793``` 4794 4795### pause<a name=audiorecorder_pause></a> 4796 4797pause():void 4798 4799Pauses audio recording. This API can be called only after the [start](#audiorecorder_on) event is triggered. 4800 4801**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4802 4803**Example** 4804 4805```js 4806audioRecorder.on('pause', () => { // Set the 'pause' event callback. 4807 console.log('audio recorder pause success'); 4808}); 4809audioRecorder.pause(); 4810``` 4811 4812### resume<a name=audiorecorder_resume></a> 4813 4814resume():void 4815 4816Resumes audio recording. This API can be called only after the [pause](#audiorecorder_on) event is triggered. 4817 4818**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4819 4820**Example** 4821 4822```js 4823audioRecorder.on('resume', () => { // Set the 'resume' event callback. 4824 console.log('audio recorder resume success'); 4825}); 4826audioRecorder.resume(); 4827``` 4828 4829### stop<a name=audiorecorder_stop></a> 4830 4831stop(): void 4832 4833Stops audio recording. 4834 4835**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4836 4837**Example** 4838 4839```js 4840audioRecorder.on('stop', () => { // Set the 'stop' event callback. 4841 console.log('audio recorder stop success'); 4842}); 4843audioRecorder.stop(); 4844``` 4845 4846### release<a name=audiorecorder_release></a> 4847 4848release(): void 4849 4850Releases the audio recording resources. 4851 4852**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4853 4854**Example** 4855 4856```js 4857audioRecorder.on('release', () => { // Set the 'release' event callback. 4858 console.log('audio recorder release success'); 4859}); 4860audioRecorder.release(); 4861audioRecorder = undefined; 4862``` 4863 4864### reset<a name=audiorecorder_reset></a> 4865 4866reset(): void 4867 4868Resets audio recording. 4869 4870Before 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. 4871 4872**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4873 4874**Example** 4875 4876```js 4877audioRecorder.on('reset', () => { // Set the 'reset' event callback. 4878 console.log('audio recorder reset success'); 4879}); 4880audioRecorder.reset(); 4881``` 4882 4883### on('prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset')<a name=audiorecorder_on></a> 4884 4885on(type: 'prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset', callback: () => void): void 4886 4887Subscribes to the audio recording events. 4888 4889**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4890 4891**Parameters** 4892 4893| Name | Type | Mandatory| Description | 4894| -------- | -------- | ---- | ------------------------------------------------------------ | 4895| 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.| 4896| callback | ()=>void | Yes | Callback invoked when the event is triggered. | 4897 4898**Example** 4899 4900```js 4901let audioRecorder = media.createAudioRecorder(); // Create an AudioRecorder instance. 4902let audioRecorderConfig = { 4903 audioEncoder : media.AudioEncoder.AAC_LC, 4904 audioEncodeBitRate : 22050, 4905 audioSampleRate : 22050, 4906 numberOfChannels : 2, 4907 format : media.AudioOutputFormat.AAC_ADTS, 4908 uri : 'fd://xx', // The file must be created by the caller and granted with proper permissions. 4909 location : { latitude : 30, longitude : 130}, 4910} 4911audioRecorder.on('error', (error) => { // Set the 'error' event callback. 4912 console.info(`audio error called, error: ${error}`); 4913}); 4914audioRecorder.on('prepare', () => { // Set the 'prepare' event callback. 4915 console.log('prepare success'); 4916 audioRecorder.start(); // Start recording and trigger the 'start' event callback. 4917}); 4918audioRecorder.on('start', () => { // Set the 'start' event callback. 4919 console.log('audio recorder start success'); 4920}); 4921audioRecorder.on('pause', () => { // Set the 'pause' event callback. 4922 console.log('audio recorder pause success'); 4923}); 4924audioRecorder.on('resume', () => { // Set the 'resume' event callback. 4925 console.log('audio recorder resume success'); 4926}); 4927audioRecorder.on('stop', () => { // Set the 'stop' event callback. 4928 console.log('audio recorder stop success'); 4929}); 4930audioRecorder.on('release', () => { // Set the 'release' event callback. 4931 console.log('audio recorder release success'); 4932}); 4933audioRecorder.on('reset', () => { // Set the 'reset' event callback. 4934 console.log('audio recorder reset success'); 4935}); 4936audioRecorder.prepare(audioRecorderConfig) // Set recording parameters and trigger the 'prepare' event callback. 4937``` 4938 4939### on('error') 4940 4941on(type: 'error', callback: ErrorCallback): void 4942 4943Subscribes to audio recording error events. After an error event is reported, you must handle the event and exit the recording. 4944 4945**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4946 4947**Parameters** 4948 4949| Name | Type | Mandatory| Description | 4950| -------- | ------------- | ---- | ------------------------------------------------------------ | 4951| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during audio recording.| 4952| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 4953 4954**Example** 4955 4956```js 4957let audioRecorderConfig = { 4958 audioEncoder : media.AudioEncoder.AAC_LC, 4959 audioEncodeBitRate : 22050, 4960 audioSampleRate : 22050, 4961 numberOfChannels : 2, 4962 format : media.AudioOutputFormat.AAC_ADTS, 4963 uri : 'fd://xx', // The file must be created by the caller and granted with proper permissions. 4964 location : { latitude : 30, longitude : 130}, 4965} 4966audioRecorder.on('error', (error) => { // Set the 'error' event callback. 4967 console.info(`audio error called, error: ${error}`); 4968}); 4969audioRecorder.prepare(audioRecorderConfig); // Do no set any parameter in prepare and trigger the 'error' event callback. 4970``` 4971 4972## AudioRecorderConfig<sup>(deprecated)</sup> 4973 4974> **NOTE** 4975> 4976> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorderConfig](#avrecorderconfig9) instead. 4977 4978Describes audio recording configurations. 4979 4980**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4981 4982| Name | Type | Mandatory| Description | 4983| ----------------------------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 4984| 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.| 4985| audioEncodeBitRate | number | No | Audio encoding bit rate. The default value is **48000**. | 4986| audioSampleRate | number | No | Audio sampling rate. The default value is **48000**. | 4987| numberOfChannels | number | No | Number of audio channels. The default value is **2**. | 4988| 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.| 4989| location | [Location](#location) | No | Geographical location of the recorded audio. | 4990| uri | string | Yes | Audio output URI. Supported: fd://xx (fd number)<br> <br>The file must be created by the caller and granted with proper permissions.| 4991| audioEncoderMime<sup>8+</sup> | [CodecMimeType](#codecmimetype8) | No | Audio encoding format. | 4992| fileFormat<sup>8+</sup> | [ContainerFormatType](#containerformattype8) | No | Audio encoding format. | 4993 4994## AudioEncoder<sup>(deprecated)</sup> 4995 4996> **NOTE** 4997> 4998> This API is supported since API version 6 and deprecated since API version 8. You are advised to use [CodecMimeType](#codecmimetype8) instead. 4999 5000Enumerates the audio encoding formats. 5001 5002**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 5003 5004| Name | Value | Description | 5005| ------- | ---- | ------------------------------------------------------------ | 5006| DEFAULT | 0 | Default encoding format.<br>This API is defined but not implemented yet. | 5007| AMR_NB | 1 | AMR-NB.<br>This API is defined but not implemented yet.| 5008| AMR_WB | 2 | Adaptive Multi Rate-Wide Band Speech Codec (AMR-WB).<br>This API is defined but not implemented yet.| 5009| AAC_LC | 3 | Advanced Audio Coding Low Complexity (AAC-LC).| 5010| HE_AAC | 4 | High-Efficiency Advanced Audio Coding (HE_AAC).<br>This API is defined but not implemented yet.| 5011 5012## AudioOutputFormat<sup>(deprecated)</sup> 5013 5014> **NOTE** 5015> 5016> This API is supported since API version 6 and deprecated since API version 8. You are advised to use [ContainerFormatType](#containerformattype8) instead. 5017 5018Enumerates the audio output formats. 5019 5020**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 5021 5022| Name | Value | Description | 5023| -------- | ---- | ------------------------------------------------------------ | 5024| DEFAULT | 0 | Default encapsulation format.<br>This API is defined but not implemented yet. | 5025| MPEG_4 | 2 | MPEG-4. | 5026| AMR_NB | 3 | AMR_NB.<br>This API is defined but not implemented yet. | 5027| AMR_WB | 4 | AMR_WB.<br>This API is defined but not implemented yet. | 5028| AAC_ADTS | 6 | Audio Data Transport Stream (ADTS), which is a transport stream format of AAC-based audio.| 5029