1# 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 various media services covering audio and video, which provide the following capabilities: 10 11- Audio playback ([AudioPlayer](#audioplayer)) 12- Video playback ([VideoPlayer](#videoplayer8)) 13- Audio recording ([AudioRecorder](#audiorecorder)) 14 15The following capabilities will be provided in later versions: data source audio/video playback, audio/video encoding and decoding, container encapsulation and decapsulation, and media capability query. 16 17## Modules to Import 18 19```js 20import media from '@ohos.multimedia.media'; 21``` 22 23## media.createAudioPlayer 24 25createAudioPlayer(): [AudioPlayer](#audioplayer) 26 27Creates an **AudioPlayer** instance in synchronous mode. 28 29**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 30 31**Return value** 32 33| Type | Description | 34| --------------------------- | ------------------------------------------------------------ | 35| [AudioPlayer](#audioplayer) | Returns the **AudioPlayer** instance if the operation is successful; returns **null** otherwise. After the instance is created, you can start, pause, or stop audio playback.| 36 37**Example** 38 39```js 40let audioPlayer = media.createAudioPlayer(); 41``` 42 43## media.createVideoPlayer<sup>8+</sup> 44 45createVideoPlayer(callback: AsyncCallback\<[VideoPlayer](#videoplayer8)>): void 46 47Creates a **VideoPlayer** instance in asynchronous mode. This API uses a callback to return the result. 48 49**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 50 51**Parameters** 52 53| Name | Type | Mandatory| Description | 54| -------- | ------------------------------------------- | ---- | ------------------------------ | 55| callback | AsyncCallback<[VideoPlayer](#videoplayer8)> | Yes | Callback used to return the **VideoPlayer** instance created.| 56 57**Example** 58 59```js 60let videoPlayer 61 62media.createVideoPlayer((error, video) => { 63 if (video != null) { 64 videoPlayer = video; 65 console.info('video createVideoPlayer success'); 66 } else { 67 console.info(`video createVideoPlayer fail, error:${error.message}`); 68 } 69}); 70``` 71 72## media.createVideoPlayer<sup>8+</sup> 73 74createVideoPlayer(): Promise<[VideoPlayer](#videoplayer8)> 75 76Creates a **VideoPlayer** instance in asynchronous mode. This API uses a promise to return the result. 77 78**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 79 80**Return value** 81 82| Type | Description | 83| ------------------------------------- | ----------------------------------- | 84| Promise<[VideoPlayer](#videoplayer8)> | Promise used to return the **VideoPlayer** instance created.| 85 86**Example** 87 88```js 89let videoPlayer 90 91media.createVideoPlayer().then((video) => { 92 if (video != null) { 93 videoPlayer = video; 94 console.info('video createVideoPlayer success'); 95 } else { 96 console.info('video createVideoPlayer fail'); 97 } 98}).catch((error) => { 99 console.info(`video catchCallback, error:${error.message}`); 100}); 101``` 102 103## media.createAudioRecorder 104 105createAudioRecorder(): AudioRecorder 106 107Creates an **AudioRecorder** instance to control audio recording. 108Only one **AudioRecorder** instance can be created for a device. 109 110**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 111 112**Return value** 113 114| Type | Description | 115| ------------------------------- | ----------------------------------------- | 116| [AudioRecorder](#audiorecorder) | Returns the **AudioRecorder** instance if the operation is successful; returns **null** otherwise.| 117 118**Example** 119 120```js 121let audioRecorder = media.createAudioRecorder(); 122``` 123 124 125 126 127## MediaErrorCode<sup>8+</sup> 128 129Enumerates the media error codes. 130 131**System capability**: SystemCapability.Multimedia.Media.Core 132 133| Name | Value | Description | 134| -------------------------- | ---- | -------------------------------------- | 135| MSERR_OK | 0 | The operation is successful. | 136| MSERR_NO_MEMORY | 1 | Failed to allocate memory. The system may have no available memory.| 137| MSERR_OPERATION_NOT_PERMIT | 2 | No permission to perform this operation. | 138| MSERR_INVALID_VAL | 3 | Invalid input parameter. | 139| MSERR_IO | 4 | An I/O error occurs. | 140| MSERR_TIMEOUT | 5 | The operation times out. | 141| MSERR_UNKNOWN | 6 | An unknown error occurs. | 142| MSERR_SERVICE_DIED | 7 | Invalid server. | 143| MSERR_INVALID_STATE | 8 | The operation is not allowed in the current state. | 144| MSERR_UNSUPPORTED | 9 | The operation is not supported in the current version. | 145 146## MediaType<sup>8+</sup> 147 148Enumerates the media types. 149 150**System capability**: SystemCapability.Multimedia.Media.Core 151 152| Name | Value | Description | 153| -------------- | ---- | ---------- | 154| MEDIA_TYPE_AUD | 0 | Media.| 155| MEDIA_TYPE_VID | 1 | Video.| 156 157## CodecMimeType<sup>8+</sup> 158 159Enumerates the codec MIME types. 160 161**System capability**: SystemCapability.Multimedia.Media.Core 162 163| Name | Value | Description | 164| ------------ | --------------------- | ------------------------ | 165| VIDEO_H263 | 'video/h263' | Video in H.263 format. | 166| VIDEO_AVC | 'video/avc' | Video in AVC format. | 167| VIDEO_MPEG2 | 'video/mpeg2' | Video in MPEG-2 format. | 168| VIDEO_MPEG4 | 'video/mp4v-es' | Video in MPEG-4 format. | 169| VIDEO_VP8 | 'video/x-vnd.on2.vp8' | Video in VP8 format. | 170| AUDIO_AAC | "audio/mp4a-latm" | Audio in MP4A-LATM format.| 171| AUDIO_VORBIS | 'audio/vorbis' | Audio in Vorbis format. | 172| AUDIO_FLAC | 'audio/flac' | Audio in FLAC format. | 173 174## MediaDescriptionKey<sup>8+</sup> 175 176Enumerates the media description keys. 177 178**System capability**: SystemCapability.Multimedia.Media.Core 179 180| Name | Value | Description | 181| ------------------------ | --------------- | ------------------------------------------------------------ | 182| MD_KEY_TRACK_INDEX | "track_index" | Track index, which is a number. | 183| MD_KEY_TRACK_TYPE | "track_type" | Track type, which is a number. For details, see [MediaType](#mediatype8).| 184| MD_KEY_CODEC_MIME | "codec_mime" | Codec MIME type, which is a string. | 185| MD_KEY_DURATION | "duration" | Media duration, which is a number, in units of ms. | 186| MD_KEY_BITRATE | "bitrate" | Bit rate, which is a number, in units of bit/s. | 187| MD_KEY_WIDTH | "width" | Video width, which is a number, in units of pixel. | 188| MD_KEY_HEIGHT | "height" | Video height, which is a number, in units of pixel. | 189| MD_KEY_FRAME_RATE | "frame_rate" | Video frame rate, which is a number, in units of 100 fps.| 190| MD_KEY_AUD_CHANNEL_COUNT | "channel_count" | Number of audio channels, which is a number. | 191| MD_KEY_AUD_SAMPLE_RATE | "sample_rate" | Sampling rate, which is a number, in units of Hz. | 192 193## BufferingInfoType<sup>8+</sup> 194 195Enumerates the buffering event types. 196 197**System capability**: SystemCapability.Multimedia.Media.Core 198 199| Name | Value | Description | 200| ----------------- | ---- | -------------------------------- | 201| BUFFERING_START | 1 | Buffering starts. | 202| BUFFERING_END | 2 | Buffering ends. | 203| BUFFERING_PERCENT | 3 | Buffering progress, in percent. | 204| CACHED_DURATION | 4 | Cache duration, in milliseconds.| 205 206## AudioPlayer 207 208Provides APIs to manage and play audio. Before calling an API of **AudioPlayer**, you must use [createAudioPlayer()](#mediacreateaudioplayer) to create an **AudioPlayer** instance. 209 210For details about the audio playback demo, see [Audio Playback Development](../../media/audio-playback.md). 211 212### Attributes<a name=audioplayer_attributes></a> 213 214**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 215 216| Name | Type | Readable| Writable| Description | 217| ----------- | ------------------------- | ---- | ---- | ------------------------------------------------------------ | 218| src | string | Yes | Yes | Audio file URI. The mainstream audio formats (MPEG-4, MPEG-TS, WebM, and MKV) are supported.<br>**Example of supported URIs**:<br>1. FD playback: fd://xx<br><br>2. HTTP network playback: http://xx<br>3. HTTPS network playback: https://xx<br>4. HLS network playback: http://xx or https://xx<br>**NOTE**<br>To use media materials, you must declare the read permission. Otherwise, the media materials cannot be played properly.| 219| loop | boolean | Yes | Yes | Whether to loop audio playback. The value **true** means to loop audio playback, and **false** means the opposite. | 220| currentTime | number | Yes | No | Current audio playback position. | 221| duration | number | Yes | No | Audio duration. | 222| state | [AudioState](#audiostate) | Yes | No | Audio playback state. | 223 224### play<a name=audioplayer_play></a> 225 226play(): void 227 228Starts to play audio resources. This API can be called only after the [dataLoad](#audioplayer_on) event is triggered. 229 230**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 231 232**Example** 233 234```js 235audioPlayer.on('play', () => { // Set the 'play' event callback. 236 console.log('audio play success'); 237}); 238audioPlayer.play(); 239``` 240 241### pause<a name=audioplayer_pause></a> 242 243pause(): void 244 245Pauses audio playback. 246 247**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 248 249**Example** 250 251```js 252audioPlayer.on('pause', () => { // Set the 'pause' event callback. 253 console.log('audio pause success'); 254}); 255audioPlayer.pause(); 256``` 257 258### stop<a name=audioplayer_stop></a> 259 260stop(): void 261 262Stops audio playback. 263 264**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 265 266**Example** 267 268```js 269audioPlayer.on('stop', () => { // Set the 'stop' event callback. 270 console.log('audio stop success'); 271}); 272audioPlayer.stop(); 273``` 274 275### reset<sup>7+</sup><a name=audioplayer_reset></a> 276 277reset(): void 278 279Switches the audio resource to be played. 280 281**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 282 283**Example** 284 285```js 286audioPlayer.on('reset', () => { // Set the 'reset' event callback. 287 console.log('audio reset success'); 288}); 289audioPlayer.reset(); 290``` 291 292### seek<a name=audioplayer_seek></a> 293 294seek(timeMs: number): void 295 296Seeks to the specified playback position. 297 298**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 299 300**Parameters** 301 302| Name| Type | Mandatory| Description | 303| ------ | ------ | ---- | ------------------------------------ | 304| timeMs | number | Yes | Position to seek to, in milliseconds.| 305 306**Example** 307 308```js 309audioPlayer.on('timeUpdate', (seekDoneTime) => { // Set the 'timeUpdate' event callback. 310 if (seekDoneTime == null) { 311 console.info('audio seek fail'); 312 return; 313 } 314 console.log('audio seek success. seekDoneTime: ' + seekDoneTime); 315}); 316audioPlayer.seek(30000); // Seek to 30000 ms. 317``` 318 319### setVolume<a name=audioplayer_setvolume></a> 320 321setVolume(vol: number): void 322 323Sets the volume. 324 325**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 326 327**Parameters** 328 329| Name| Type | Mandatory| Description | 330| ------ | ------ | ---- | ------------------------------------------------------------ | 331| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1** indicates the maximum volume (100%).| 332 333**Example** 334 335```js 336audioPlayer.on('volumeChange', () => { // Set the 'volumeChange' event callback. 337 console.log('audio volumeChange success'); 338}); 339audioPlayer.setVolume(1); // Set the volume to 100%. 340``` 341 342### release<a name=audioplayer_release></a> 343 344release(): void 345 346Releases the audio playback resource. 347 348**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 349 350**Example** 351 352```js 353audioPlayer.release(); 354audioPlayer = undefined; 355``` 356 357### getTrackDescription<sup>8+</sup><a name=audioplayer_gettrackdescription1></a> 358 359getTrackDescription(callback: AsyncCallback<Array\<MediaDescription>>): void 360 361Obtains the audio track information. This API uses a callback to return the result. It can be called only after the [dataLoad](#audioplayer_on) event is triggered. 362 363**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 364 365**Parameters** 366 367| Name | Type | Mandatory| Description | 368| -------- | ------------------------------------------------------------ | ---- | -------------------------- | 369| callback | AsyncCallback<Array<[MediaDescription](#mediadescription8)>> | Yes | Callback used to return the audio track information obtained.| 370 371**Example** 372 373```js 374function printfDescription(obj) { 375 for (let item in obj) { 376 let property = obj[item]; 377 console.info('audio key is ' + item); 378 console.info('audio value is ' + property); 379 } 380} 381 382audioPlayer.getTrackDescription((error, arrlist) => { 383 if (arrlist != null) { 384 for (let i = 0; i < arrlist.length; i++) { 385 printfDescription(arrlist[i]); 386 } 387 } else { 388 console.log(`audio getTrackDescription fail, error:${error.message}`); 389 } 390}); 391``` 392 393### getTrackDescription<sup>8+</sup><a name=audioplayer_gettrackdescription2></a> 394 395getTrackDescription(): Promise<Array\<MediaDescription>> 396 397Obtains 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. 398 399**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 400 401**Return value** 402 403| Type | Description | 404| ------------------------------------------------------ | ------------------------------- | 405| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return the audio track information obtained.| 406 407**Example** 408 409```js 410function printfDescription(obj) { 411 for (let item in obj) { 412 let property = obj[item]; 413 console.info('audio key is ' + item); 414 console.info('audio value is ' + property); 415 } 416} 417 418audioPlayer.getTrackDescription().then((arrlist) => { 419 if (arrlist != null) { 420 arrayDescription = arrlist; 421 } else { 422 console.log('audio getTrackDescription fail'); 423 } 424}).catch((error) => { 425 console.info(`audio catchCallback, error:${error.message}`); 426}); 427 428for (let i = 0; i < arrayDescription.length; i++) { 429 printfDescription(arrayDescription[i]); 430} 431``` 432 433### on('bufferingUpdate')<sup>8+</sup> 434 435on(type: 'bufferingUpdate', callback: (infoType: [BufferingInfoType](#bufferinginfotype8), value: number) => void): void 436 437Subscribes to the audio buffering update event. 438 439**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 440 441**Parameters** 442 443| Name | Type | Mandatory| Description | 444| -------- | -------- | ---- | ------------------------------------------------------------ | 445| type | string | Yes | Event type, which is 'bufferingUpdate' in this case. | 446| 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**.| 447 448**Example** 449 450```js 451audioPlayer.on('bufferingUpdate', (infoType, value) => { 452 console.log('audio bufferingInfo type: ' + infoType); 453 console.log('audio bufferingInfo value: ' + value); 454}); 455``` 456 457 ### on('play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeChange')<a name = audioplayer_on></a> 458 459on(type: 'play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeChange', callback: () => void): void 460 461Subscribes to the audio playback events. 462 463**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 464 465**Parameters** 466 467| Name | Type | Mandatory| Description | 468| -------- | ---------- | ---- | ------------------------------------------------------------ | 469| 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. | 470| callback | () => void | Yes | Callback invoked when the event is triggered. | 471 472**Example** 473 474```js 475let audioPlayer = media.createAudioPlayer(); // Create an AudioPlayer instance. 476audioPlayer.on('dataLoad', () => { // Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully. 477 console.info('audio set source success'); 478 audioPlayer.play(); // Start the playback and trigger the 'play' event callback. 479}); 480audioPlayer.on('play', () => { // Set the 'play' event callback. 481 console.info('audio play success'); 482 audioPlayer.seek(30000); // Call the seek() API and trigger the 'timeUpdate' event callback. 483}); 484audioPlayer.on('pause', () => { // Set the 'pause' event callback. 485 console.info('audio pause success'); 486 audioPlayer.stop(); // Stop the playback and trigger the 'stop' event callback. 487}); 488audioPlayer.on('reset', () => { // Set the 'reset' event callback. 489 console.info('audio reset success'); 490 audioPlayer.release(); // Release the AudioPlayer instance. 491 audioPlayer = undefined; 492}); 493audioPlayer.on('timeUpdate', (seekDoneTime) => { // Set the 'timeUpdate' event callback. 494 if (seekDoneTime == null) { 495 console.info('audio seek fail'); 496 return; 497 } 498 console.info('audio seek success, and seek time is ' + seekDoneTime); 499 audioPlayer.setVolume(0.5); // Set the volume to 50% and trigger the 'volumeChange' event callback. 500}); 501audioPlayer.on('volumeChange', () => { // Set the 'volumeChange' event callback. 502 console.info('audio volumeChange success'); 503 audioPlayer.pause(); // Pause the playback and trigger the 'pause' event callback. 504}); 505audioPlayer.on('finish', () => { // Set the 'finish' event callback. 506 console.info('audio play finish'); 507 audioPlayer.stop(); // Stop the playback and trigger the 'stop' event callback. 508}); 509audioPlayer.on('error', (error) => { // Set the 'error' event callback. 510 console.info(`audio error called, errName is ${error.name}`); 511 console.info(`audio error called, errCode is ${error.code}`); 512 console.info(`audio error called, errMessage is ${error.message}`); 513}); 514 515// Set the FD (local playback) of the audio file selected by the user. 516let fdPath = 'fd://' 517// 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. 518let path = '/data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3'; 519fileIO.open(path).then(fdNumber) => { 520 fdPath = fdPath + '' + fdNumber; 521 console.info('open fd success fd is' + fdPath); 522}, (err) => { 523 console.info('open fd failed err is' + err); 524}).catch((err) => { 525 console.info('open fd failed err is' + err); 526}); 527audioPlayer.src = fdPath; // Set the src attribute and trigger the 'dataLoad' event callback. 528``` 529 530### on('timeUpdate') 531 532on(type: 'timeUpdate', callback: Callback\<number>): void 533 534Subscribes to the 'timeUpdate' event. 535 536**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 537 538**Parameters** 539 540| Name | Type | Mandatory| Description | 541| -------- | ----------------- | ---- | ------------------------------------------------------------ | 542| type | string | Yes | Event type, which is 'timeUpdate' in this case.<br>The 'timeUpdate' event is triggered when the [seek()](#audioplayer_seek) API is called.| 543| callback | Callback\<number> | Yes | Callback invoked when the event is triggered. The input parameter of the callback is the time when the seek operation is successful. | 544 545**Example** 546 547```js 548audioPlayer.on('timeUpdate', (seekDoneTime) => { // Set the 'timeUpdate' event callback. 549 if (seekDoneTime == null) { 550 console.info('audio seek fail'); 551 return; 552 } 553 console.log('audio seek success. seekDoneTime: ' + seekDoneTime); 554}); 555audioPlayer.seek(30000); // Seek to 30000 ms. 556``` 557 558### on('error') 559 560on(type: 'error', callback: ErrorCallback): void 561 562Subscribes to the audio playback error event. 563 564**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 565 566**Parameters** 567 568| Name | Type | Mandatory| Description | 569| -------- | ------------- | ---- | ------------------------------------------------------------ | 570| type | string | Yes | Event type, which is 'error' in this case.<br>The 'error' event is triggered when an error occurs during audio playback.| 571| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 572 573**Example** 574 575```js 576audioPlayer.on('error', (error) => { // Set the error event callback. 577 console.info(`audio error called, errName is ${error.name}`); // Print the error name. 578 console.info(`audio error called, errCode is ${error.code}`); // Print the error code. 579 console.info(`audio error called, errMessage is ${error.message}`);// Print the detailed description of the error type. 580}); 581audioPlayer.setVolume(3); // Set volume to an invalid value to trigger the 'error' event. 582``` 583 584## AudioState 585 586Enumerates the audio playback states. You can obtain the state through the **state** attribute. 587 588**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 589 590| Name | Type | Description | 591| ------------------ | ------ | -------------- | 592| idle | string | The audio player is idle.| 593| playing | string | Audio playback is in progress.| 594| paused | string | Audio playback is paused.| 595| stopped | string | Audio playback is stopped.| 596| error<sup>8+</sup> | string | Audio playback is in the error state. | 597 598## VideoPlayer<sup>8+</sup> 599 600Provides APIs to manage and play video. Before calling an API of **VideoPlayer**, you must call [createVideoPlayer()](#mediacreatevideoplayer8) to create a [VideoPlayer](#videoplayer8) instance. 601 602For details about the video playback demo, see [Video Playback Development](../../media/video-playback.md). 603 604### Attributes<a name=videoplayer_attributes></a> 605 606**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 607 608| Name | Type | Readable| Writable| Description | 609| ------------------------ | ---------------------------------- | ---- | ---- | ------------------------------------------------------------ | 610| url<sup>8+</sup> | string | Yes | Yes | Video media URL. The mainstream video formats (MPEG-4, MPEG-TS, WebM, and MKV) are supported.<br>**Example of supported URIs**:<br>1. FD playback: fd://xx<br><br>2. HTTP network playback: http://xx<br>3. HTTPS network playback: https://xx<br>4. HLS network playback: http://xx or https://xx<br>**Note**:<br>To use media materials, you must declare the read permission. Otherwise, the media materials cannot be played properly.| 611| 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. | 612| currentTime<sup>8+</sup> | number | Yes | No | Current video playback position. | 613| duration<sup>8+</sup> | number | Yes | No | Video duration. The value **-1** indicates the live mode. | 614| state<sup>8+</sup> | [VideoPlayState](#videoplaystate8) | Yes | No | Video playback state. | 615| width<sup>8+</sup> | number | Yes | No | Video width. | 616| height<sup>8+</sup> | number | Yes | No | Video height. | 617 618### setDisplaySurface<sup>8+</sup> 619 620setDisplaySurface(surfaceId: string, callback: AsyncCallback\<void>): void 621 622Sets **SurfaceId**. This API uses a callback to return the result. 623 624**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 625 626**Parameters** 627 628| Name | Type | Mandatory| Description | 629| --------- | -------- | ---- | ------------------------- | 630| surfaceId | string | Yes | Surface ID to set. | 631| callback | function | Yes | Callback used to return the result.| 632 633**Example** 634 635```js 636videoPlayer.setDisplaySurface(surfaceId, (err) => { 637 if (err == null) { 638 console.info('setDisplaySurface success!'); 639 } else { 640 console.info('setDisplaySurface fail!'); 641 } 642}); 643``` 644 645### setDisplaySurface<sup>8+</sup> 646 647setDisplaySurface(surfaceId: string): Promise\<void> 648 649Sets **SurfaceId**. This API uses a promise to return the result. 650 651**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 652 653**Parameters** 654 655| Name | Type | Mandatory| Description | 656| --------- | ------ | ---- | --------- | 657| surfaceId | string | Yes | Surface ID to set.| 658 659**Return value** 660 661| Type | Description | 662| -------------- | ------------------------------ | 663| Promise\<void> | Promise used to return the result.| 664 665**Example** 666 667```js 668videoPlayer.setDisplaySurface(surfaceId).then(() => { 669 console.info('setDisplaySurface success'); 670}).catch((error) => { 671 console.info(`video catchCallback, error:${error.message}`); 672}); 673``` 674 675### prepare<sup>8+</sup> 676 677prepare(callback: AsyncCallback\<void>): void 678 679Prepares for video playback. This API uses a callback to return the result. 680 681**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 682 683**Parameters** 684 685| Name | Type | Mandatory| Description | 686| -------- | -------- | ---- | ------------------------ | 687| callback | function | Yes | Callback used to return the result.| 688 689**Example** 690 691```js 692videoPlayer.prepare((err) => { 693 if (err == null) { 694 console.info('prepare success!'); 695 } else { 696 console.info('prepare fail!'); 697 } 698}); 699``` 700 701### prepare<sup>8+</sup> 702 703prepare(): Promise\<void> 704 705Prepares for video playback. This API uses a promise to return the result. 706 707**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 708 709**Return value** 710 711| Type | Description | 712| -------------- | ----------------------------- | 713| Promise\<void> | Promise used to return the result.| 714 715**Example** 716 717```js 718videoPlayer.prepare().then(() => { 719 console.info('prepare success'); 720}).catch((error) => { 721 console.info(`video catchCallback, error:${error.message}`); 722}); 723``` 724 725### play<sup>8+</sup> 726 727play(callback: AsyncCallback\<void>): void; 728 729Starts to play video resources. This API uses a callback to return the result. 730 731**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 732 733**Parameters** 734 735| Name | Type | Mandatory| Description | 736| -------- | -------- | ---- | ------------------------ | 737| callback | function | Yes | Callback used to return the result.| 738 739**Example** 740 741```js 742videoPlayer.play((err) => { 743 if (err == null) { 744 console.info('play success!'); 745 } else { 746 console.info('play fail!'); 747 } 748}); 749``` 750 751### play<sup>8+</sup> 752 753play(): Promise\<void>; 754 755Starts to play video resources. This API uses a promise to return the result. 756 757**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 758 759**Return value** 760 761| Type | Description | 762| -------------- | ----------------------------- | 763| Promise\<void> | Promise used to return the result.| 764 765**Example** 766 767```js 768videoPlayer.play().then(() => { 769 console.info('play success'); 770}).catch((error) => { 771 console.info(`video catchCallback, error:${error.message}`); 772}); 773``` 774 775### pause<sup>8+</sup> 776 777pause(callback: AsyncCallback\<void>): void 778 779Pauses video playback. This API uses a callback to return the result. 780 781**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 782 783**Parameters** 784 785| Name | Type | Mandatory| Description | 786| -------- | -------- | ---- | ------------------------ | 787| callback | function | Yes | Callback used to return the result.| 788 789**Example** 790 791```js 792videoPlayer.pause((err) => { 793 if (err == null) { 794 console.info('pause success!'); 795 } else { 796 console.info('pause fail!'); 797 } 798}); 799``` 800 801### pause<sup>8+</sup> 802 803pause(): Promise\<void> 804 805Pauses video playback. This API uses a promise to return the result. 806 807**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 808 809**Return value** 810 811| Type | Description | 812| -------------- | ----------------------------- | 813| Promise\<void> | Promise used to return the result.| 814 815**Example** 816 817```js 818videoPlayer.pause().then(() => { 819 console.info('pause success'); 820}).catch((error) => { 821 console.info(`video catchCallback, error:${error.message}`); 822}); 823``` 824 825### stop<sup>8+</sup> 826 827stop(callback: AsyncCallback\<void>): void 828 829Stops video playback. This API uses a callback to return the result. 830 831**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 832 833**Parameters** 834 835| Name | Type | Mandatory| Description | 836| -------- | -------- | ---- | ------------------------ | 837| callback | function | Yes | Callback used to return the result.| 838 839**Example** 840 841```js 842videoPlayer.stop((err) => { 843 if (err == null) { 844 console.info('stop success!'); 845 } else { 846 console.info('stop fail!'); 847 } 848}); 849``` 850 851### stop<sup>8+</sup> 852 853stop(): Promise\<void> 854 855Stops video playback. This API uses a promise to return the result. 856 857**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 858 859**Return value** 860 861| Type | Description | 862| -------------- | ----------------------------- | 863| Promise\<void> | Promise used to return the result.| 864 865**Example** 866 867```js 868videoPlayer.stop().then(() => { 869 console.info('stop success'); 870}).catch((error) => { 871 console.info(`video catchCallback, error:${error.message}`); 872}); 873``` 874 875### reset<sup>8+</sup> 876 877reset(callback: AsyncCallback\<void>): void 878 879Switches the video resource to be played. This API uses a callback to return the result. 880 881**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 882 883**Parameters** 884 885| Name | Type | Mandatory| Description | 886| -------- | -------- | ---- | ------------------------ | 887| callback | function | Yes | Callback used to return the result.| 888 889**Example** 890 891```js 892videoPlayer.reset((err) => { 893 if (err == null) { 894 console.info('reset success!'); 895 } else { 896 console.info('reset fail!'); 897 } 898}); 899``` 900 901### reset<sup>8+</sup> 902 903reset(): Promise\<void> 904 905Switches the video resource to be played. This API uses a promise to return the result. 906 907**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 908 909**Return value** 910 911| Type | Description | 912| -------------- | ----------------------------- | 913| Promise\<void> | Promise used to return the result.| 914 915**Example** 916 917```js 918videoPlayer.reset().then(() => { 919 console.info('reset success'); 920}).catch((error) => { 921 console.info(`video catchCallback, error:${error.message}`); 922}); 923``` 924 925### seek<sup>8+</sup> 926 927seek(timeMs: number, callback: AsyncCallback\<number>): void 928 929Seeks to the specified playback position. The next key frame at the specified position is played. This API uses a callback to return the result. 930 931**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 932 933**Parameters** 934 935| Name | Type | Mandatory| Description | 936| -------- | -------- | ---- | ------------------------------------ | 937| timeMs | number | Yes | Position to seek to, in milliseconds.| 938| callback | function | Yes | Callback used to return the result. | 939 940**Example** 941 942```js 943videoPlayer.seek((seekTime, err) => { 944 if (err == null) { 945 console.info('seek success!'); 946 } else { 947 console.info('seek fail!'); 948 } 949}); 950``` 951 952### seek<sup>8+</sup> 953 954seek(timeMs: number, mode:SeekMode, callback: AsyncCallback\<number>): void 955 956Seeks to the specified playback position. This API uses a callback to return the result. 957 958**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 959 960**Parameters** 961 962| Name | Type | Mandatory| Description | 963| -------- | ---------------------- | ---- | ------------------------------------ | 964| timeMs | number | Yes | Position to seek to, in milliseconds.| 965| mode | [SeekMode](#seekmode8) | Yes | Seek mode. | 966| callback | function | Yes | Callback used to return the result. | 967 968**Example** 969 970```js 971videoPlayer.seek((seekTime, seekMode, err) => { 972 if (err == null) { 973 console.info('seek success!'); 974 } else { 975 console.info('seek fail!'); 976 } 977}); 978``` 979 980### seek<sup>8+</sup> 981 982seek(timeMs: number, mode?:SeekMode): Promise\<number> 983 984Seeks to the specified playback position. If **mode** is not specified, the next key frame at the specified position is played. This API uses a promise to return the result. 985 986**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 987 988**Parameters** 989 990| Name| Type | Mandatory| Description | 991| ------ | ---------------------- | ---- | ------------------------------------ | 992| timeMs | number | Yes | Position to seek to, in milliseconds.| 993| mode | [SeekMode](#seekmode8) | No | Seek mode. | 994 995**Return value** 996 997| Type | Description | 998| -------------- | ----------------------------------- | 999| Promise\<void> | Promise used to return the result.| 1000 1001**Example** 1002 1003```js 1004videoPlayer.seek(seekTime).then((seekDoneTime) => { // seekDoneTime indicates the position after the seek operation is complete. 1005 console.info('seek success'); 1006}).catch((error) => { 1007 console.info(`video catchCallback, error:${error.message}`); 1008}); 1009 1010videoPlayer.seek(seekTime, seekMode).then((seekDoneTime) => { 1011 console.info('seek success'); 1012}).catch((error) => { 1013 console.info(`video catchCallback, error:${error.message}`); 1014}); 1015``` 1016 1017### setVolume<sup>8+</sup> 1018 1019setVolume(vol: number, callback: AsyncCallback\<void>): void 1020 1021Sets the volume. This API uses a callback to return the result. 1022 1023**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1024 1025**Parameters** 1026 1027| Name | Type | Mandatory| Description | 1028| -------- | -------- | ---- | ------------------------------------------------------------ | 1029| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1** indicates the maximum volume (100%).| 1030| callback | function | Yes | Callback used to return the result. | 1031 1032**Example** 1033 1034```js 1035videoPlayer.setVolume((vol, err) => { 1036 if (err == null) { 1037 console.info('setVolume success!'); 1038 } else { 1039 console.info('setVolume fail!'); 1040 } 1041}); 1042``` 1043 1044### setVolume<sup>8+</sup> 1045 1046setVolume(vol: number): Promise\<void> 1047 1048Sets the volume. This API uses a promise to return the result. 1049 1050**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1051 1052**Parameters** 1053 1054| Name| Type | Mandatory| Description | 1055| ------ | ------ | ---- | ------------------------------------------------------------ | 1056| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1** indicates the maximum volume (100%).| 1057 1058**Return value** 1059 1060| Type | Description | 1061| -------------- | ------------------------- | 1062| Promise\<void> | Promise used to return the result.| 1063 1064**Example** 1065 1066```js 1067videoPlayer.setVolume(vol).then() => { 1068 console.info('setVolume success'); 1069}).catch((error) => { 1070 console.info(`video catchCallback, error:${error.message}`); 1071}); 1072``` 1073 1074### release<sup>8+</sup> 1075 1076release(callback: AsyncCallback\<void>): void 1077 1078Releases the video playback resource. This API uses a callback to return the result. 1079 1080**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1081 1082**Parameters** 1083 1084| Name | Type | Mandatory| Description | 1085| -------- | -------- | ---- | ------------------------ | 1086| callback | function | Yes | Callback used to return the result.| 1087 1088**Example** 1089 1090```js 1091videoPlayer.release((err) => { 1092 if (err == null) { 1093 console.info('release success!'); 1094 } else { 1095 console.info('release fail!'); 1096 } 1097}); 1098``` 1099 1100### release<sup>8+</sup> 1101 1102release(): Promise\<void> 1103 1104Releases the video playback resource. This API uses a promise to return the result. 1105 1106**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1107 1108**Return value** 1109 1110| Type | Description | 1111| -------------- | ----------------------------- | 1112| Promise\<void> | Promise used to return the result.| 1113 1114**Example** 1115 1116```js 1117videoPlayer.release().then() => { 1118 console.info('release success'); 1119}).catch((error) => { 1120 console.info(`video catchCallback, error:${error.message}`); 1121}); 1122``` 1123 1124### getTrackDescription<sup>8+</sup> 1125 1126getTrackDescription(callback: AsyncCallback<Array\<MediaDescription>>): void 1127 1128Obtains the video track information. This API uses a callback to return the result. 1129 1130**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1131 1132**Parameters** 1133 1134| Name | Type | Mandatory| Description | 1135| -------- | ------------------------------------------------------------ | ---- | -------------------------- | 1136| callback | AsyncCallback<Array<[MediaDescription](#mediadescription8)>> | Yes | Callback used to return the video track information obtained.| 1137 1138**Example** 1139 1140```js 1141function printfDescription(obj) { 1142 for (let item in obj) { 1143 let property = obj[item]; 1144 console.info('video key is ' + item); 1145 console.info('video value is ' + property); 1146 } 1147} 1148 1149videoPlayer.getTrackDescription((error, arrlist) => { 1150 if (arrlist != null) { 1151 for (let i = 0; i < arrlist.length; i++) { 1152 printfDescription(arrlist[i]); 1153 } 1154 } else { 1155 console.log(`video getTrackDescription fail, error:${error.message}`); 1156 } 1157}); 1158``` 1159 1160### getTrackDescription<sup>8+</sup> 1161 1162getTrackDescription(): Promise<Array\<MediaDescription>> 1163 1164Obtains the video track information. This API uses a promise to return the result. 1165 1166**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1167 1168**Return value** 1169 1170| Type | Description | 1171| ------------------------------------------------------ | ------------------------------- | 1172| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return the video track information obtained.| 1173 1174**Example** 1175 1176```js 1177function printfDescription(obj) { 1178 for (let item in obj) { 1179 let property = obj[item]; 1180 console.info('video key is ' + item); 1181 console.info('video value is ' + property); 1182 } 1183} 1184 1185let arrayDescription; 1186videoPlayer.getTrackDescription().then((arrlist) => { 1187 if (arrlist != null) { 1188 arrayDescription = arrlist; 1189 } else { 1190 console.log('video getTrackDescription fail'); 1191 } 1192}).catch((error) => { 1193 console.info(`video catchCallback, error:${error.message}`); 1194}); 1195for (let i = 0; i < arrayDescription.length; i++) { 1196 printfDescription(arrayDescription[i]); 1197} 1198``` 1199 1200### setSpeed<sup>8+</sup> 1201 1202setSpeed(speed:number, callback: AsyncCallback\<number>): void 1203 1204Sets the video playback speed. This API uses a callback to return the result. 1205 1206**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1207 1208**Parameters** 1209 1210| Name | Type | Mandatory| Description | 1211| -------- | -------- | ---- | ---------------------------------------------------------- | 1212| speed | number | Yes | Video playback speed. For details, see [PlaybackSpeed](#playbackspeed8).| 1213| callback | function | Yes | Callback used to return the result. | 1214 1215**Example** 1216 1217```js 1218videoPlayer.setSpeed((speed:number, err) => { 1219 if (err == null) { 1220 console.info('setSpeed success!'); 1221 } else { 1222 console.info('setSpeed fail!'); 1223 } 1224}); 1225``` 1226 1227### setSpeed<sup>8+</sup> 1228 1229setSpeed(speed:number): Promise\<number> 1230 1231Sets the video playback speed. This API uses a promise to return the result. 1232 1233**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1234 1235**Parameters** 1236 1237| Name| Type | Mandatory| Description | 1238| ------ | ------ | ---- | ---------------------------------------------------------- | 1239| speed | number | Yes | Video playback speed. For details, see [PlaybackSpeed](#playbackspeed8).| 1240 1241**Return value** 1242 1243| Type | Description | 1244| ---------------- | ------------------------- | 1245| Promise\<number> | Promise used to return the result.| 1246 1247**Example** 1248 1249```js 1250videoPlayer.setSpeed(speed).then() => { 1251 console.info('setSpeed success'); 1252}).catch((error) => { 1253 console.info(`video catchCallback, error:${error.message}`); 1254}); 1255``` 1256 1257### on('playbackCompleted')<sup>8+</sup> 1258 1259on(type: 'playbackCompleted', callback: Callback\<void>): void 1260 1261Subscribes to the video playback completion event. 1262 1263**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1264 1265**Parameters** 1266 1267| Name | Type | Mandatory| Description | 1268| -------- | -------- | ---- | ----------------------------------------------------------- | 1269| type | string | Yes | Event type, which is 'playbackCompleted' in this case.| 1270| callback | function | Yes | Callback invoked when the event is triggered. | 1271 1272**Example** 1273 1274```js 1275videoPlayer.on('playbackCompleted', () => { 1276 console.info('playbackCompleted success!'); 1277}); 1278``` 1279 1280### on('bufferingUpdate')<sup>8+</sup> 1281 1282on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void 1283 1284Subscribes to the video buffering update event. 1285 1286**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1287 1288**Parameters** 1289 1290| Name | Type | Mandatory| Description | 1291| -------- | -------- | ---- | ------------------------------------------------------------ | 1292| type | string | Yes | Event type, which is 'bufferingUpdate' in this case. | 1293| 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**.| 1294 1295**Example** 1296 1297```js 1298videoPlayer.on('bufferingUpdate', (infoType, value) => { 1299 console.log('video bufferingInfo type: ' + infoType); 1300 console.log('video bufferingInfo value: ' + value); 1301}); 1302``` 1303 1304### on('startRenderFrame')<sup>8+</sup> 1305 1306on(type: 'startRenderFrame', callback: Callback\<void>): void 1307 1308Subscribes to the frame rendering start event. 1309 1310**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1311 1312**Parameters** 1313 1314| Name | Type | Mandatory| Description | 1315| -------- | --------------- | ---- | ------------------------------------------------------------ | 1316| type | string | Yes | Event type, which is 'startRenderFrame' in this case.| 1317| callback | Callback\<void> | Yes | Callback invoked when the event is triggered. | 1318 1319**Example** 1320 1321```js 1322videoPlayer.on('startRenderFrame', () => { 1323 console.info('startRenderFrame success!'); 1324}); 1325``` 1326 1327### on('videoSizeChanged')<sup>8+</sup> 1328 1329on(type: 'videoSizeChanged', callback: (width: number, height: number) => void): void 1330 1331Subscribes to the video width and height change event. 1332 1333**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1334 1335**Parameters** 1336 1337| Name | Type | Mandatory| Description | 1338| -------- | -------- | ---- | ------------------------------------------------------------ | 1339| type | string | Yes | Event type, which is 'videoSizeChanged' in this case.| 1340| callback | function | Yes | Callback invoked when the event is triggered. **width** indicates the video width, and **height** indicates the video height. | 1341 1342**Example** 1343 1344```js 1345videoPlayer.on('videoSizeChanged', (width, height) => { 1346 console.log('video width is: ' + width); 1347 console.log('video height is: ' + height); 1348}); 1349``` 1350 1351### on('error')<sup>8+</sup> 1352 1353on(type: 'error', callback: ErrorCallback): void 1354 1355Subscribes to the video playback error event. 1356 1357**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1358 1359**Parameters** 1360 1361| Name | Type | Mandatory| Description | 1362| -------- | ------------- | ---- | ------------------------------------------------------------ | 1363| type | string | Yes | Event type, which is 'error' in this case.<br>The 'error' event is triggered when an error occurs during video playback.| 1364| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 1365 1366**Example** 1367 1368```js 1369videoPlayer.on('error', (error) => { // Set the 'error' event callback. 1370 console.info(`video error called, errName is ${error.name}`); // Print the error name. 1371 console.info(`video error called, errCode is ${error.code}`); // Print the error code. 1372 console.info(`video error called, errMessage is ${error.message}`);// Print the detailed description of the error type. 1373}); 1374videoPlayer.setVolume(3); // Set volume to an invalid value to trigger the 'error' event. 1375``` 1376 1377## VideoPlayState<sup>8+</sup> 1378 1379Enumerates the video playback states. You can obtain the state through the **state** attribute. 1380 1381**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1382 1383| Name | Type | Description | 1384| -------- | ------ | -------------- | 1385| idle | string | The video player is idle.| 1386| prepared | string | Video playback is being prepared.| 1387| playing | string | Video playback is in progress.| 1388| paused | string | Video playback is paused.| 1389| stopped | string | Video playback is stopped.| 1390| error | string | Video playback is in the error state. | 1391 1392## SeekMode<sup>8+</sup> 1393 1394Enumerates the video playback seek modes, which can be passed in the **seek** API. 1395 1396**System capability**: SystemCapability.Multimedia.Media.Core 1397 1398| Name | Value | Description | 1399| -------------- | ---- | ------------------------------------------------------------ | 1400| 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.| 1401| 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.| 1402 1403## PlaybackSpeed<sup>8+</sup> 1404 1405Enumerates the video playback speeds, which can be passed in the **setSpeed** API. 1406 1407**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1408 1409| Name | Value | Description | 1410| -------------------- | ---- | ------------------------------ | 1411| SPEED_FORWARD_0_75_X | 0 | Plays the video at 0.75 times the normal speed.| 1412| SPEED_FORWARD_1_00_X | 1 | Plays the video at the normal speed. | 1413| SPEED_FORWARD_1_25_X | 2 | Plays the video at 1.25 times the normal speed.| 1414| SPEED_FORWARD_1_75_X | 3 | Plays the video at 1.75 times the normal speed.| 1415| SPEED_FORWARD_2_00_X | 4 | Plays the video at 2.00 times the normal speed.| 1416 1417## MediaDescription<sup>8+</sup> 1418 1419### [key : string] : Object 1420 1421Defines media information in key-value mode. 1422 1423**System capability**: SystemCapability.Multimedia.Media.Core 1424 1425| Name | Type | Description | 1426| ----- | ------ | ------------------------------------------------------------ | 1427| key | string | Key of the media information. For details about the keys, see [MediaDescriptionKey](#mediadescriptionkey8).| 1428| value | any | Value of the key. For details about the values, see [MediaDescriptionKey](#mediadescriptionkey8).| 1429 1430**Example** 1431 1432```js 1433function printfItemDescription(obj, key) { 1434 let property = obj[key]; 1435 console.info('audio key is ' + key); 1436 console.info('audio value is ' + property); 1437} 1438 1439audioPlayer.getTrackDescription((error, arrlist) => { 1440 if (arrlist != null) { 1441 for (let i = 0; i < arrlist.length; i++) { 1442 printfItemDescription(arrlist[i], MD_KEY_TRACK_TYPE); // Print the MD_KEY_TRACK_TYPE value of each track. 1443 } 1444 } else { 1445 console.log(`audio getTrackDescription fail, error:${error.message}`); 1446 } 1447}); 1448``` 1449 1450## AudioRecorder 1451 1452Implements audio recording. Before calling an API of **AudioRecorder**, you must call [createAudioRecorder()](#mediacreateaudiorecorder) to create an [AudioRecorder](#audiorecorder) instance. 1453 1454For details about the audio recording demo, see [Audio Recording Development](../../media/audio-recorder.md). 1455 1456### prepare<a name=audiorecorder_prepare></a> 1457 1458prepare(config: AudioRecorderConfig): void 1459 1460Prepares for recording. 1461 1462**Required permissions:** ohos.permission.MICROPHONE 1463 1464**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 1465 1466**Parameters** 1467 1468| Name| Type | Mandatory| Description | 1469| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ | 1470| config | [AudioRecorderConfig](#audiorecorderconfig) | Yes | Audio recording parameters, including the audio output URI, encoding format, sampling rate, number of audio channels, and output format.| 1471 1472**Example** 1473 1474```js 1475let audioRecorderConfig = { 1476 audioEncoder : media.AudioEncoder.AAC_LC, 1477 audioEncodeBitRate : 22050, 1478 audioSampleRate : 22050, 1479 numberOfChannels : 2, 1480 format : media.AudioOutputFormat.AAC_ADTS, 1481 uri : 'fd://1', // The file must be created by the caller and granted with proper permissions. 1482 location : { latitude : 30, longitude : 130}, 1483} 1484audioRecorder.on('prepare', () => { // Set the 'prepare' event callback. 1485 console.log('prepare success'); 1486}); 1487audioRecorder.prepare(audioRecorderConfig); 1488``` 1489 1490 1491### start<a name=audiorecorder_start></a> 1492 1493start(): void 1494 1495Starts audio recording. This API can be called only after the [prepare](#audiorecorder_on) event is triggered. 1496 1497**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 1498 1499**Example** 1500 1501```js 1502audioRecorder.on('start', () => { // Set the 'start' event callback. 1503 console.log('audio recorder start success'); 1504}); 1505audioRecorder.start(); 1506``` 1507 1508### pause<a name=audiorecorder_pause></a> 1509 1510pause():void 1511 1512Pauses audio recording. This API can be called only after the [start](#audiorecorder_on) event is triggered. 1513 1514**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 1515 1516**Example** 1517 1518```js 1519audioRecorder.on('pause', () => { // Set the 'pause' event callback. 1520 console.log('audio recorder pause success'); 1521}); 1522audioRecorder.pause(); 1523``` 1524 1525### resume<a name=audiorecorder_resume></a> 1526 1527resume():void 1528 1529Resumes audio recording. This API can be called only after the [pause](#audiorecorder_on) event is triggered. 1530 1531**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 1532 1533**Example** 1534 1535```js 1536audioRecorder.on('resume', () => { // Set the 'resume' event callback. 1537 console.log('audio recorder resume success'); 1538}); 1539audioRecorder.resume(); 1540``` 1541 1542### stop<a name=audiorecorder_stop></a> 1543 1544stop(): void 1545 1546Stops audio recording. 1547 1548**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 1549 1550**Example** 1551 1552```js 1553audioRecorder.on('stop', () => { // Set the 'stop' event callback. 1554 console.log('audio recorder stop success'); 1555}); 1556audioRecorder.stop(); 1557``` 1558 1559### release<a name=audiorecorder_release></a> 1560 1561release(): void 1562 1563Releases the audio recording resource. 1564 1565**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 1566 1567**Example** 1568 1569```js 1570audioRecorder.on('release', () => { // Set the 'release' event callback. 1571 console.log('audio recorder release success'); 1572}); 1573audioRecorder.release(); 1574audioRecorder = undefined; 1575``` 1576 1577### reset<a name=audiorecorder_reset></a> 1578 1579reset(): void 1580 1581Resets audio recording. 1582 1583Before 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. 1584 1585**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 1586 1587**Example** 1588 1589```js 1590audioRecorder.on('reset', () => { // Set the 'reset' event callback. 1591 console.log('audio recorder reset success'); 1592}); 1593audioRecorder.reset(); 1594``` 1595 1596### on('prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset')<a name=audiorecorder_on></a> 1597 1598on(type: 'prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset', callback: () => void): void 1599 1600Subscribes to the audio recording events. 1601 1602**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 1603 1604**Parameters** 1605 1606| Name | Type | Mandatory| Description | 1607| -------- | -------- | ---- | ------------------------------------------------------------ | 1608| 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 resource is released.<br>- 'reset': triggered when the [reset](#audiorecorder_reset) API is called and audio recording is reset. | 1609| callback | ()=>void | Yes | Callback invoked when the event is triggered. | 1610 1611**Example** 1612 1613```js 1614let audiorecorder = media.createAudioRecorder(); // Create an AudioRecorder instance. 1615let audioRecorderConfig = { 1616 audioEncoder : media.AudioEncoder.AAC_LC, , 1617 audioEncodeBitRate : 22050, 1618 audioSampleRate : 22050, 1619 numberOfChannels : 2, 1620 format : media.AudioOutputFormat.AAC_ADTS, 1621 uri : 'fd://xx', // The file must be created by the caller and granted with proper permissions. 1622 location : { latitude : 30, longitude : 130}, 1623} 1624audioRecorder.on('error', (error) => { // Set the 'error' event callback. 1625 console.info(`audio error called, errName is ${error.name}`); 1626 console.info(`audio error called, errCode is ${error.code}`); 1627 console.info(`audio error called, errMessage is ${error.message}`); 1628}); 1629audioRecorder.on('prepare', () => { // Set the 'prepare' event callback. 1630 console.log('prepare success'); 1631 audioRecorder.start(); // Start recording and trigger the 'start' event callback. 1632}); 1633audioRecorder.on('start', () => { // Set the 'start' event callback. 1634 console.log('audio recorder start success'); 1635}); 1636audioRecorder.on('pause', () => { // Set the 'pause' event callback. 1637 console.log('audio recorder pause success'); 1638}); 1639audioRecorder.on('resume', () => { // Set the 'resume' event callback. 1640 console.log('audio recorder resume success'); 1641}); 1642audioRecorder.on('stop', () => { // Set the 'stop' event callback. 1643 console.log('audio recorder stop success'); 1644}); 1645audioRecorder.on('release', () => { // Set the 'release' event callback. 1646 console.log('audio recorder release success'); 1647}); 1648audioRecorder.on('reset', () => { // Set the 'reset' event callback. 1649 console.log('audio recorder reset success'); 1650}); 1651audioRecorder.prepare(audioRecorderConfig) // Set recording parameters and trigger the 'prepare' event callback. 1652``` 1653 1654### on('error') 1655 1656on(type: 'error', callback: ErrorCallback): void 1657 1658Subscribes to the audio recording error event. 1659 1660**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 1661 1662**Parameters** 1663 1664| Name | Type | Mandatory| Description | 1665| -------- | ------------- | ---- | ------------------------------------------------------------ | 1666| type | string | Yes | Event type, which is 'error' in this case.<br>The 'error' event is triggered when an error occurs during audio recording.| 1667| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 1668 1669**Example** 1670 1671```js 1672let audioRecorderConfig = { 1673 audioEncoder : media.AudioEncoder.AAC_LC, 1674 audioEncodeBitRate : 22050, 1675 audioSampleRate : 22050, 1676 numberOfChannels : 2, 1677 format : media.AudioOutputFormat.AAC_ADTS, 1678 uri : 'fd://xx', // The file must be created by the caller and granted with proper permissions. 1679 location : { latitude : 30, longitude : 130}, 1680} 1681audioRecorder.on('error', (error) => { // Set the 'error' event callback. 1682 console.info(`audio error called, error: ${error}`); 1683}); 1684audioRecorder.prepare(audioRecorderConfig); // Set any parameter in prepare and trigger the 'error' event callback. 1685``` 1686 1687## AudioRecorderConfig 1688 1689Describes audio recording configurations. 1690 1691**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 1692 1693| Name | Type | Mandatory| Description | 1694| --------------------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 1695| audioEncoder<sup>(deprecated)</sup> | [AudioEncoder](#audioencoder) | No | Audio encoding format. The default value is **AAC_LC**.<br>**Note**: This parameter is deprecated since API version 8. Use **audioEncoderMime** instead. | 1696| audioEncodeBitRate | number | No | Audio encoding bit rate. The default value is **48000**. | 1697| audioSampleRate | number | No | Audio sampling rate. The default value is **48000**. | 1698| numberOfChannels | number | No | Number of audio channels. The default value is **2**. | 1699| format<sup>(deprecated)</sup> | [AudioOutputFormat](#audiooutputformat) | No | Audio output format. The default value is **MPEG_4**.<br>**Note**: This parameter is deprecated since API version 8. Use **fileFormat** instead. | 1700| location | [Location](#location) | No | Geographical location of the recorded audio. | 1701| 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.| 1702| audioEncoderMime<sup>8+</sup> | [CodecMimeType](#codecmimetype8) | No | Audio encoding format. | 1703| fileFormat<sup>8+</sup> | [ContainerFormatType](#containerformattype8) | No | Audio encoding format. | 1704 1705 1706## AudioEncoder<sup>(deprecated)</sup> 1707 1708> **NOTE** 1709> This API is deprecated since API version 8. You are advised to use [CodecMimeType](#codecmimetype8) instead. 1710 1711Enumerates the audio encoding formats. 1712 1713**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 1714 1715| Name | Default Value| Description | 1716| ------- | ------ | ------------------------------------------------------------ | 1717| DEFAULT | 0 | Default encoding format.<br>This API is defined but not implemented yet.| 1718| AMR_NB | 1 | AMR-NB.<br>This API is defined but not implemented yet.| 1719| AMR_WB | 2 | Adaptive Multi Rate-Wide Band Speech Codec (AMR-WB).<br>This API is defined but not implemented yet.| 1720| AAC_LC | 3 | Advanced Audio Coding Low Complexity (AAC-LC).| 1721| HE_AAC | 4 | High-Efficiency Advanced Audio Coding (HE_AAC).<br>This API is defined but not implemented yet.| 1722 1723 1724## AudioOutputFormat<sup>(deprecated)</sup> 1725 1726> **NOTE** 1727> This API is deprecated since API version 8. You are advised to use [ContainerFormatType](#containerformattype8) instead. 1728 1729Enumerates the audio output formats. 1730 1731**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 1732 1733| Name | Default Value| Description | 1734| -------- | ------ | ------------------------------------------------------------ | 1735| DEFAULT | 0 | Default encapsulation format.<br>This API is defined but not implemented yet.| 1736| MPEG_4 | 2 | MPEG-4. | 1737| AMR_NB | 3 | AMR_NB.<br>This API is defined but not implemented yet.| 1738| AMR_WB | 4 | AMR_WB.<br>This API is defined but not implemented yet.| 1739| AAC_ADTS | 6 | Audio Data Transport Stream (ADTS), which is a transport stream format of AAC-based audio.| 1740 1741 1742## ContainerFormatType<sup>8+</sup> 1743 1744Enumerates the container format types (CFTs). 1745 1746**System capability**: SystemCapability.Multimedia.Media.Core 1747 1748| Name | Value | Description | 1749| ----------- | ----- | --------------------- | 1750| CFT_MPEG_4 | "mp4" | Video container format MPEG-4.| 1751| CFT_MPEG_4A | "m4a" | Audio container format M4A.| 1752 1753## Location 1754 1755Describes the geographical location of the recorded video. 1756 1757**System capability**: SystemCapability.Multimedia.Media.Core 1758 1759| Name | Type| Mandatory| Description | 1760| --------- | -------- | ---- | ---------------- | 1761| latitude | number | Yes | Latitude of the geographical location.| 1762| longitude | number | Yes | Longitude of the geographical location.| 1763