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 ([AVPlayer](#avplayer9)<sup>9+</sup>) 12 13 The **AVPlayer** class has integrated [AudioPlayer](#audioplayerdeprecated)<sup>6+</sup> and [VideoPlayer](#videoplayerdeprecated)<sup>8+</sup>, with the state machine and error codes upgraded. It is recommended. 14 15- Audio and video recording [AVRecorder](#avrecorder9)<sup>9+</sup>) 16 17 The **AVRecorder** class has integrated [AudioRecorder](#audiorecorderdeprecated)<sup>6+</sup> and [VideoRecorder](#videorecorder9)<sup>9+</sup>. It is recommended. 18 19- Obtaining audio and video metadata ([AVMetadataExtractor](#avmetadataextractor11)<sup>11+</sup>). 20 21- Obtaining video thumbnails ([AVImageGenerator](#avimagegenerator11)<sup>11+</sup>) 22 23## Modules to Import 24 25```ts 26import media from '@ohos.multimedia.media'; 27``` 28 29## media.createAVPlayer<sup>9+</sup> 30 31createAVPlayer(callback: AsyncCallback\<AVPlayer>): void 32 33Creates an **AVPlayer** instance. This API uses an asynchronous callback to return the result. 34 35> **NOTE** 36> 37> - A maximum of 13 instances can be created in video-only playback scenarios. 38> - A maximum of 16 instances can be created in both audio and video playback scenarios. 39> - The actual number of instances that can be created may be different. It depends on the specifications of the device chip in use. For example, in the case of RK3568, a maximum of 6 instances can be created in video-only playback scenarios. 40 41**System capability**: SystemCapability.Multimedia.Media.AVPlayer 42 43**Parameters** 44 45| Name | Type | Mandatory| Description | 46| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 47| 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.| 48 49**Error codes** 50 51For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 52 53| ID| Error Message | 54| -------- | ------------------------------ | 55| 5400101 | No memory. Return by callback. | 56 57**Example** 58 59```ts 60import { BusinessError } from '@ohos.base'; 61 62let avPlayer: media.AVPlayer; 63media.createAVPlayer((error: BusinessError, video: media.AVPlayer) => { 64 if (video != null) { 65 avPlayer = video; 66 console.info('createAVPlayer success'); 67 } else { 68 console.error(`createAVPlayer fail, error message:${error.message}`); 69 } 70}); 71``` 72 73## media.createAVPlayer<sup>9+</sup> 74 75createAVPlayer(): Promise\<AVPlayer> 76 77Creates an **AVPlayer** instance. This API uses a promise to return the result. 78 79> **NOTE** 80> 81> - A maximum of 13 instances can be created in video-only playback scenarios. 82> - A maximum of 16 instances can be created in both audio and video playback scenarios. 83> - The actual number of instances that can be created may be different. It depends on the specifications of the device chip in use. For example, in the case of RK3568, a maximum of 6 instances can be created in video-only playback scenarios. 84 85**System capability**: SystemCapability.Multimedia.Media.AVPlayer 86 87**Return value** 88 89| Type | Description | 90| ------------------------------- | ------------------------------------------------------------ | 91| 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.| 92 93**Error codes** 94 95For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 96 97| ID| Error Message | 98| -------- | ----------------------------- | 99| 5400101 | No memory. Return by promise. | 100 101**Example** 102 103```ts 104let avPlayer: media.AVPlayer; 105media.createAVPlayer().then((video: media.AVPlayer) => { 106 if (video != null) { 107 avPlayer = video; 108 console.info('createAVPlayer success'); 109 } else { 110 console.error('createAVPlayer fail'); 111 } 112}).catch((error: BusinessError) => { 113 console.error(`AVPlayer catchCallback, error message:${error.message}`); 114}); 115``` 116 117## media.createAVRecorder<sup>9+</sup> 118 119createAVRecorder(callback: AsyncCallback\<AVRecorder>): void 120 121Creates an **AVRecorder** instance. This API uses an asynchronous callback to return the result. 122 123> **NOTE** 124> 125> - A maximum of 2 instances can be created in audio and video recording scenarios. 126> - Only one instance can perform audio recording on a device at one time, since all the applications share the audio channel. Any attempt to create the second instance for audio recording fails due to audio channel conflicts. 127 128**System capability**: SystemCapability.Multimedia.Media.AVRecorder 129 130**Parameters** 131 132| Name | Type | Mandatory| Description | 133| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 134| 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.| 135 136**Error codes** 137 138For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 139 140| ID| Error Message | 141| -------- | ------------------------------ | 142| 5400101 | No memory. Return by callback. | 143 144**Example** 145 146```ts 147let avRecorder: media.AVRecorder; 148 149media.createAVRecorder((error: BusinessError, recorder: media.AVRecorder) => { 150 if (recorder != null) { 151 avRecorder = recorder; 152 console.info('createAVRecorder success'); 153 } else { 154 console.error(`createAVRecorder fail, error message:${error.message}`); 155 } 156}); 157``` 158 159## media.createAVRecorder<sup>9+</sup> 160 161createAVRecorder(): Promise\<AVRecorder> 162 163Creates an **AVRecorder** instance. This API uses a promise to return the result. 164 165> **NOTE** 166> 167> - A maximum of 2 instances can be created in audio and video recording scenarios. 168> - Only one instance can perform audio recording on a device at one time, since all the applications share the audio channel. Any attempt to create the second instance for audio recording fails due to audio channel conflicts. 169 170**System capability**: SystemCapability.Multimedia.Media.AVRecorder 171 172**Return value** 173 174| Type | Description | 175| ------------------------------------ | ------------------------------------------------------------ | 176| 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.| 177 178**Error codes** 179 180For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 181 182| ID| Error Message | 183| -------- | ----------------------------- | 184| 5400101 | No memory. Return by promise. | 185 186**Example** 187 188```ts 189let avRecorder: media.AVRecorder; 190 191media.createAVRecorder().then((recorder: media.AVRecorder) => { 192 if (recorder != null) { 193 avRecorder = recorder; 194 console.info('createAVRecorder success'); 195 } else { 196 console.error('createAVRecorder fail'); 197 } 198}).catch((error: BusinessError) => { 199 console.error(`createAVRecorder catchCallback, error message:${error.message}`); 200}); 201``` 202 203## media.createAVMetadataExtractor<sup>11+</sup> 204 205createAVMetadataExtractor(callback: AsyncCallback\<AVMetadataExtractor>): void 206 207Creates an **AVMetadataExtractor** instance. This API uses an asynchronous callback to return the result. 208 209**System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor 210 211**Parameters** 212 213| Name | Type | Mandatory| Description | 214| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 215| callback | AsyncCallback\<[AVMetadataExtractor](#avmetadataextractor11)> | Yes | Callback used to return the result. If the operation is successful, an **AVMetadataExtractor** instance is returned; otherwise, **null** is returned. The interface can be used to obtain audio and video metadata.| 216 217**Error codes** 218 219For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 220 221| ID| Error Message | 222| -------- | ------------------------------ | 223| 5400101 | No memory. Returned by callback. | 224 225**Example** 226 227```ts 228let avMetadataExtractor: media.AVMetadataExtractor; 229media.createAVMetadataExtractor((error: BusinessError, extractor: media.AVMetadataExtractor) => { 230 if (extractor != null) { 231 avMetadataExtractor = extractor; 232 console.info('createAVMetadataExtractor success'); 233 } else { 234 console.error(`createAVMetadataExtractor fail, error message:${error.message}`); 235 } 236}); 237``` 238 239## media.createAVMetadataExtractor<sup>11+</sup> 240 241createAVMetadataExtractor(): Promise\<AVMetadataExtractor> 242 243Creates an **AVMetadataExtractor** instance. This API uses a promise to return the result. 244 245**System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor 246 247**Return value** 248 249| Type | Description | 250| ------------------------------- | ------------------------------------------------------------ | 251| Promise\<[AVMetadataExtractor](#avmetadataextractor11)> | Promise used to return the result. If the operation is successful, an **AVMetadataExtractor** instance is returned; otherwise, **null** is returned. The interface can be used to obtain audio and video metadata.| 252 253**Error codes** 254 255For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 256 257| ID| Error Message | 258| -------- | ----------------------------- | 259| 5400101 | No memory. Returned by promise. | 260 261**Example** 262 263```ts 264let avMetadataExtractor: media.AVMetadataExtractor; 265media.createAVMetadataExtractor().then((extractor: media.AVMetadataExtractor) => { 266 if (extractor != null) { 267 avMetadataExtractor = extractor; 268 console.info('createAVMetadataExtractor success'); 269 } else { 270 console.error('createAVMetadataExtractor fail'); 271 } 272}).catch((error: BusinessError) => { 273 console.error(`AVMetadataExtractor catchCallback, error message:${error.message}`); 274}); 275``` 276 277## media.createAVImageGenerator<sup>11+</sup> 278 279createAVImageGenerator(callback: AsyncCallback\<AVImageGenerator>): void 280 281Creates an **AVImageGenerator** instance. This API uses an asynchronous callback to return the result. 282 283**System capability**: SystemCapability.Multimedia.Media.AVImageGenerator 284 285**System API**: This is a system API. 286 287**Parameters** 288 289| Name | Type | Mandatory| Description | 290| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 291| callback | AsyncCallback\<[AVImageGenerator](#avimagegenerator11)> | Yes | Callback used to return the result. If the operation is successful, an **AVImageGenerator** instance is returned; otherwise, **null** is returned. The interface can be used to obtain a video thumbnail.| 292 293**Error codes** 294 295For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 296 297| ID| Error Message | 298| -------- | ------------------------------ | 299| 5400101 | No memory. Returned by callback. | 300 301**Example** 302 303```ts 304let avImageGenerator: media.AVImageGenerator; 305media.createAVImageGenerator((error: BusinessError, generator: media.AVImageGenerator) => { 306 if (generator != null) { 307 avImageGenerator = generator; 308 console.info('createAVImageGenerator success'); 309 } else { 310 console.error(`createAVImageGenerator fail, error message:${error.message}`); 311 } 312}); 313``` 314 315## media.createAVImageGenerator<sup>11+</sup> 316 317createAVImageGenerator(): Promise\<AVImageGenerator> 318 319Creates an **AVImageGenerator** instance. This API uses a promise to return the result. 320 321**System capability**: SystemCapability.Multimedia.Media.AVImageGenerator 322 323**System API**: This is a system API. 324 325**Return value** 326 327| Type | Description | 328| ------------------------------- | ------------------------------------------------------------ | 329| Promise\<[AVImageGenerator](#avimagegenerator11)> | Promise used to return the result. If the operation is successful, an **AVImageGenerator** instance is returned; otherwise, **null** is returned. The interface can be used to obtain a video thumbnail.| 330 331**Error codes** 332 333For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 334 335| ID| Error Message | 336| -------- | ----------------------------- | 337| 5400101 | No memory. Returned by promise. | 338 339**Example** 340 341```ts 342let avImageGenerator: media.AVImageGenerator; 343media.createAVImageGenerator().then((generator: media.AVImageGenerator) => { 344 if (generator != null) { 345 avImageGenerator = generator; 346 console.info('createAVImageGenerator success'); 347 } else { 348 console.error('createAVImageGenerator fail'); 349 } 350}).catch((error: BusinessError) => { 351 console.error(`AVImageGenerator catchCallback, error message:${error.message}`); 352}); 353``` 354 355## media.createVideoRecorder<sup>9+</sup> 356 357createVideoRecorder(callback: AsyncCallback\<VideoRecorder>): void 358 359Creates a **VideoRecorder** instance. This API uses an asynchronous callback to return the result. 360 361Only one **VideoRecorder** instance can be created per device. 362 363**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 364 365**System API**: This is a system API. 366 367**Parameters** 368 369| Name | Type | Mandatory| Description | 370| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 371| 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.| 372 373**Error codes** 374 375For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 376 377| ID| Error Message | 378| -------- | ------------------------------ | 379| 5400101 | No memory. Return by callback. | 380 381**Example** 382 383```ts 384let videoRecorder: media.VideoRecorder; 385 386media.createVideoRecorder((error: BusinessError, video: media.VideoRecorder) => { 387 if (video != null) { 388 videoRecorder = video; 389 console.info('video createVideoRecorder success'); 390 } else { 391 console.error(`video createVideoRecorder fail, error message:${error.message}`); 392 } 393}); 394``` 395 396## media.createVideoRecorder<sup>9+</sup> 397 398createVideoRecorder(): Promise\<VideoRecorder> 399 400Creates a **VideoRecorder** instance. This API uses a promise to return the result. 401 402Only one **VideoRecorder** instance can be created per device. 403 404**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 405 406**System API**: This is a system API. 407 408**Return value** 409 410| Type | Description | 411| ----------------------------------------- | ------------------------------------------------------------ | 412| 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.| 413 414**Error codes** 415 416For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 417 418| ID| Error Message | 419| -------- | ----------------------------- | 420| 5400101 | No memory. Return by promise. | 421 422**Example** 423 424```ts 425let videoRecorder: media.VideoRecorder; 426 427media.createVideoRecorder().then((video: media.VideoRecorder) => { 428 if (video != null) { 429 videoRecorder = video; 430 console.info('video createVideoRecorder success'); 431 } else { 432 console.error('video createVideoRecorder fail'); 433 } 434}).catch((error: BusinessError) => { 435 console.error(`video catchCallback, error message:${error.message}`); 436}); 437``` 438 439## media.createSoundPool<sup>10+</sup> 440 441createSoundPool(maxStreams: number, audioRenderInfo: audio.AudioRendererInfo, callback: AsyncCallback\<SoundPool>): void 442 443Creates a **SoundPool** instance. This API uses an asynchronous callback to return the result. 444 445**System capability**: SystemCapability.Multimedia.Media.SoundPool 446 447**Parameters** 448 449| Name | Type | Mandatory| Description | 450| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 451| maxStreams | number | Yes | Maximum number of streams that can be played by the **SoundPool** instance.| 452| audioRenderInfo | [audio.AudioRendererInfo](js-apis-audio.md#audiorendererinfo8) | Yes | Audio renderer parameters.| 453| callback | AsyncCallback<[SoundPool](js-apis-inner-multimedia-soundPool.md)> | Yes | Callback used to return the result. If the operation is successful, a **SoundPool** instance is returned; otherwise, **null** is returned. The instance is used for loading and playback.| 454 455**Error codes** 456 457For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 458 459| ID| Error Message | 460| -------- | ------------------------------ | 461| 5400101 | No memory. Return by callback. | 462 463**Example** 464 465```js 466import audio from '@ohos.multimedia.audio' 467 468let soundPool: media.SoundPool; 469let audioRendererInfo: audio.AudioRendererInfo = { 470 usage : audio.StreamUsage.STREAM_USAGE_MUSIC, 471 rendererFlags : 0 472} 473 474media.createSoundPool(5, audioRendererInfo, (error, soundPool_: media.SoundPool) => { 475 if (error) { 476 console.error(`createSoundPool failed`) 477 return; 478 } else { 479 soundPool = soundPool_; 480 console.info(`createSoundPool success`) 481 } 482}); 483``` 484 485## media.createSoundPool<sup>10+</sup> 486 487createSoundPool(maxStreams: number, audioRenderInfo: audio.AudioRendererInfo): Promise\<SoundPool> 488 489Creates a **SoundPool** instance. This API uses a promise to return the result. 490 491**System capability**: SystemCapability.Multimedia.Media.SoundPool 492 493**Parameters** 494 495| Name | Type | Mandatory| Description | 496| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 497| maxStreams | number | Yes | Maximum number of streams that can be played by the **SoundPool** instance.| 498| audioRenderInfo | [audio.AudioRendererInfo](js-apis-audio.md#audiorendererinfo8) | Yes | Audio renderer parameters.| 499 500**Return value** 501 502| Type | Description | 503| ----------------------------------------- | ------------------------------------------------------------ | 504| Promise<[SoundPool](js-apis-inner-multimedia-soundPool.md)> | Promise used to return the result. If the operation is successful, a **SoundPool** instance is returned; otherwise, **null** is returned. The instance is used for loading and playback.| 505 506**Error codes** 507 508For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 509 510| ID| Error Message | 511| -------- | ----------------------------- | 512| 5400101 | No memory. Return by promise. | 513 514**Example** 515 516```js 517import audio from '@ohos.multimedia.audio' 518 519let soundPool: media.SoundPool; 520let audioRendererInfo: audio.AudioRendererInfo = { 521 usage : audio.StreamUsage.STREAM_USAGE_MUSIC, 522 rendererFlags : 0 523} 524 525media.createSoundPool(5, audioRendererInfo).then((soundpool_: media.SoundPool) => { 526 if (soundpool_ != null) { 527 soundPool = soundpool_; 528 console.info('create SoundPool success'); 529 } else { 530 console.error('create SoundPool fail'); 531 } 532}, (error: BusinessError) => { 533 console.error(`soundpool catchCallback, error message:${error.message}`); 534}); 535``` 536 537## AVErrorCode<sup>9+</sup> 538 539Enumerates the [media error codes](../errorcodes/errorcode-media.md). 540 541**System capability**: SystemCapability.Multimedia.Media.Core 542 543| Name | Value | Description | 544| :------------------------- | ------- | ------------------------------------ | 545| AVERR_OK | 0 | The operation is successful. | 546| AVERR_NO_PERMISSION | 201 | You do not have the permission to perform the operation. | 547| AVERR_INVALID_PARAMETER | 401 | Invalid input parameter. | 548| AVERR_UNSUPPORT_CAPABILITY | 801 | Unsupported API. | 549| AVERR_NO_MEMORY | 5400101 | The system memory is insufficient or the number of services reaches the upper limit.| 550| 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.| 551| AVERR_IO | 5400103 | The data stream is abnormal. | 552| AVERR_TIMEOUT | 5400104 | The system or network response times out. | 553| AVERR_SERVICE_DIED | 5400105 | The service process is dead. | 554| AVERR_UNSUPPORT_FORMAT | 5400106 | The format of the media asset is not supported. | 555 556## MediaType<sup>8+</sup> 557 558Enumerates the media types. 559 560**System capability**: SystemCapability.Multimedia.Media.Core 561 562| Name | Value | Description | 563| -------------- | ---- | ---------- | 564| MEDIA_TYPE_AUD | 0 | Media.| 565| MEDIA_TYPE_VID | 1 | Video.| 566 567## CodecMimeType<sup>8+</sup> 568 569Enumerates the codec MIME types. 570 571**System capability**: SystemCapability.Multimedia.Media.Core 572 573| Name | Value | Description | 574| ------------ | --------------------- | ------------------------ | 575| VIDEO_AVC | 'video/avc' | Video in AVC format. | 576| AUDIO_AAC | 'audio/mp4a-latm' | Audio in MP4A-LATM format.| 577| AUDIO_VORBIS | 'audio/vorbis' | Audio in Vorbis format. | 578| AUDIO_FLAC | 'audio/flac' | Audio in FLAC format. | 579 580## MediaDescriptionKey<sup>8+</sup> 581 582Enumerates the media description keys. 583 584**System capability**: SystemCapability.Multimedia.Media.Core 585 586| Name | Value | Description | 587| ------------------------ | --------------- | ------------------------------------------------------------ | 588| MD_KEY_TRACK_INDEX | 'track_index' | Track index, which is a number. | 589| MD_KEY_TRACK_TYPE | 'track_type' | Track type, which is a number. For details, see [MediaType](#mediatype8).| 590| MD_KEY_CODEC_MIME | 'codec_mime' | Codec MIME type, which is a string. | 591| MD_KEY_DURATION | 'duration' | Media duration, which is a number, in units of ms. | 592| MD_KEY_BITRATE | 'bitrate' | Bit rate, which is a number, in units of bit/s. | 593| MD_KEY_WIDTH | 'width' | Video width, which is a number, in units of pixel. | 594| MD_KEY_HEIGHT | 'height' | Video height, which is a number, in units of pixel. | 595| MD_KEY_FRAME_RATE | 'frame_rate' | Video frame rate, which is a number, in units of 100 fps.| 596| MD_KEY_AUD_CHANNEL_COUNT | 'channel_count' | Number of audio channels, which is a number. | 597| MD_KEY_AUD_SAMPLE_RATE | 'sample_rate' | Sampling rate, which is a number, in units of Hz. | 598 599## BufferingInfoType<sup>8+</sup> 600 601Enumerates the buffering event types. 602 603**System capability**: SystemCapability.Multimedia.Media.Core 604 605| Name | Value | Description | 606| ----------------- | ---- | -------------------------------- | 607| BUFFERING_START | 1 | Buffering starts. | 608| BUFFERING_END | 2 | Buffering ends. | 609| BUFFERING_PERCENT | 3 | Buffering progress, in percent. | 610| CACHED_DURATION | 4 | Cache duration, in ms.| 611 612## StateChangeReason<sup>9+</sup> 613 614Enumerates the reasons for the state transition of the **AVPlayer** or **AVRecorder** instance. The enum value is reported together with **state**. 615 616**System capability**: SystemCapability.Multimedia.Media.Core 617 618| Name | Value | Description | 619| ---------- | ---- | ------------------------------------------------------------ | 620| USER | 1 | State transition triggered by user behavior. It happens when a user or the client calls an API.| 621| 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.| 622 623## AVPlayer<sup>9+</sup> 624 625A 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. 626 627For details about the audio and video playback demo, see [Audio Playback](../../media/using-avplayer-for-playback.md) and [Video Playback](../../media/video-playback.md). 628 629### Attributes 630 631**System capability**: SystemCapability.Multimedia.Media.AVPlayer 632 633| Name | Type | Readable| Writable| Description | 634| --------------------------------------------------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 635| 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, and MKV are supported.<br>The audio formats M4A, AAC, MP3, OGG, WAV, and FLAC 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>**NOTE**<br>WebM is not supported since API version 11.| 636| 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>The video formats MP4, MPEG-TS, and MKV are supported.<br>The audio formats M4A, AAC, MP3, OGG, WAV, and FLAC are supported.<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**.<br>**NOTE**<br>WebM is not supported since API version 11.| 637| dataSrc<sup>10+</sup> | [AVDataSrcDescriptor](#avdatasrcdescriptor10) | Yes | Yes | Descriptor of a streaming media asset. It is a static attribute and can be set only when the AVPlayer is in the idle state.<br>Use scenario: An application starts playing a media file while the file is still being downloaded from the remote to the local host.<br>The video formats MP4, MPEG-TS, and MKV are supported.<br>The audio formats M4A, AAC, MP3, OGG, WAV, and FLAC are supported.<br>**Example:**<br>A user is obtaining an audio and video file from a remote server and wants to play the downloaded file content. To implement this scenario, do as follows:<br>1. Obtain the total file size, in bytes. If the total size cannot be obtained, set **fileSize** to **-1**.<br>2. Implement the **func** callback to fill in data. If **fileSize** is **-1**, the format of **func** is **func(buffer: ArrayBuffer, length: number)**, and the AVPlayer obtains data in sequence; otherwise, the format is **func(buffer: ArrayBuffer, length: number, pos: number)**, and the AVPlayer seeks and obtains data in the required positions.<br>3. Set **AVDataSrcDescriptor {fileSize = size, callback = func}**.<br>**Notes:**<br>If the media file to play is in MP4/M4A format, ensure that the **moov** field (specifying the media information) is before the **mdat** field (specifying the media data) or the fields before the **moov** field is less than 10 MB. Otherwise, the parsing fails and the media file cannot be played.<br>**NOTE**<br>WebM is not supported since API version 11.| 638| 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).| 639| 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.<br>This setting is not supported in live mode.| 640| videoScaleType<sup>9+</sup> | [VideoScaleType](#videoscaletype9) | Yes | Yes | Video scaling type. The default value is **VIDEO_SCALE_TYPE_FIT**. It is a dynamic attribute<br>and can be set only when the AVPlayer is in the prepared, playing, paused, or completed state.| 641| 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.| 642| audioRendererInfo<sup>10+</sup> | [audio.AudioRendererInfo](js-apis-audio.md#audiorendererinfo8) | Yes | Yes | Audio renderer information. The default values of **usage** and **rendererFlags** are **STREAM_USAGE_MUSIC** and **0**, respectively.<br>It can be set only when the AVPlayer is in the initialized state.| 643| audioEffectMode<sup>10+</sup> | [audio.AudioEffectMode](js-apis-audio.md#audioeffectmode10) | Yes | Yes | Audio effect mode. The audio effect mode is a dynamic attribute and is restored to the default value **EFFECT_DEFAULT** when **usage** of **audioRendererInfo** is changed. It can be set only when the AVPlayer is in the prepared, playing, paused, or completed state.| 644| 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. | 645| 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.<br>In live mode, **-1** is returned by default.| 646| 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 mode, **-1** is returned by default.| 647| 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.| 648| 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.| 649 650**NOTE** 651 652After 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. 653 654### on('stateChange')<sup>9+</sup> 655 656on(type: 'stateChange', callback: (state: AVPlayerState, reason: StateChangeReason) => void): void 657 658Subscribes to AVPlayer state changes. 659 660**System capability**: SystemCapability.Multimedia.Media.AVPlayer 661 662**Parameters** 663 664| Name | Type | Mandatory| Description | 665| -------- | -------- | ---- | ------------------------------------------------------------ | 666| type | string | Yes | Event type, which is **'stateChange'** in this case. This event can be triggered by both user operations and the system.| 667| 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.| 668 669**Example** 670 671```ts 672avPlayer.on('stateChange', async (state: string, reason: media.StateChangeReason) => { 673 switch (state) { 674 case 'idle': 675 console.info('state idle called'); 676 break; 677 case 'initialized': 678 console.info('initialized prepared called'); 679 break; 680 case 'prepared': 681 console.info('state prepared called'); 682 break; 683 case 'playing': 684 console.info('state playing called'); 685 break; 686 case 'paused': 687 console.info('state paused called'); 688 break; 689 case 'completed': 690 console.info('state completed called'); 691 break; 692 case 'stopped': 693 console.info('state stopped called'); 694 break; 695 case 'released': 696 console.info('state released called'); 697 break; 698 case 'error': 699 console.info('state error called'); 700 break; 701 default: 702 console.info('unknown state :' + state); 703 break; 704 } 705}) 706``` 707 708### off('stateChange')<sup>9+</sup> 709 710off(type: 'stateChange'): void 711 712Unsubscribes from AVPlayer state changes. 713 714**System capability**: SystemCapability.Multimedia.Media.AVPlayer 715 716**Parameters** 717 718| Name| Type | Mandatory| Description | 719| ------ | ------ | ---- | ----------------------------------------------------- | 720| type | string | Yes | Event type, which is **'stateChange'** in this case.| 721 722**Example** 723 724```ts 725avPlayer.off('stateChange') 726``` 727 728### on('error')<sup>9+</sup> 729 730on(type: 'error', callback: ErrorCallback): void 731 732Subscribes 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. 733 734**System capability**: SystemCapability.Multimedia.Media.AVPlayer 735 736**Parameters** 737 738| Name | Type | Mandatory| Description | 739| -------- | -------- | ---- | ------------------------------------------------------------ | 740| type | string | Yes | Event type, which is **'error'** in this case. This event can be triggered by both user operations and the system.| 741| callback | function | Yes | Callback used to return the error code ID and error message.| 742 743**Error codes** 744 745For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 746 747| ID| Error Message | 748| -------- | --------------------- | 749| 201 | Permission denied | 750| 401 | The parameter check failed. | 751| 801 | Capability not supported. | 752| 5400101 | No Memory. | 753| 5400102 | Operation not allowed.| 754| 5400103 | I/O error | 755| 5400104 | Time out | 756| 5400105 | Service Died. | 757| 5400106 | Unsupport Format. | 758 759**Example** 760 761```ts 762avPlayer.on('error', (error: BusinessError) => { 763 console.info('error happened,and error message is :' + error.message) 764 console.info('error happened,and error code is :' + error.code) 765}) 766``` 767 768### off('error')<sup>9+</sup> 769 770off(type: 'error'): void 771 772Unsubscribes from AVPlayer errors. 773 774**System capability**: SystemCapability.Multimedia.Media.AVPlayer 775 776**Parameters** 777 778| Name| Type | Mandatory| Description | 779| ------ | ------ | ---- | ----------------------------------------- | 780| type | string | Yes | Event type, which is **'error'** in this case.| 781 782**Example** 783 784```ts 785avPlayer.off('error') 786``` 787 788### prepare<sup>9+</sup> 789 790prepare(callback: AsyncCallback\<void>): void 791 792Prepares 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. The state changes can be detected by subscribing to the [stateChange](#onstatechange9) event. 793 794**System capability**: SystemCapability.Multimedia.Media.AVPlayer 795 796**Parameters** 797 798| Name | Type | Mandatory| Description | 799| -------- | -------- | ---- | -------------------- | 800| callback | function | Yes | Callback used to return the result.| 801 802**Error codes** 803 804For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 805 806| ID| Error Message | 807| -------- | ------------------------------------------ | 808| 5400102 | Operation not allowed. Return by callback. | 809| 5400106 | Unsupport format. Return by callback. | 810 811**Example** 812 813```ts 814avPlayer.prepare((err: BusinessError) => { 815 if (err == null) { 816 console.info('prepare success'); 817 } else { 818 console.error('prepare filed,error message is :' + err.message) 819 } 820}) 821``` 822 823### prepare<sup>9+</sup> 824 825prepare(): Promise\<void> 826 827Prepares 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. The state changes can be detected by subscribing to the [stateChange](#onstatechange9) event. 828 829**System capability**: SystemCapability.Multimedia.Media.AVPlayer 830 831**Return value** 832 833| Type | Description | 834| -------------- | ------------------------- | 835| Promise\<void> | Promise used to return the result.| 836 837**Error codes** 838 839For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 840 841| ID| Error Message | 842| -------- | ----------------------------------------- | 843| 5400102 | Operation not allowed. Return by promise. | 844| 5400106 | Unsupport format. Return by promise. | 845 846**Example** 847 848```ts 849avPlayer.prepare().then(() => { 850 console.info('prepare success'); 851}, (err: BusinessError) => { 852 console.error('prepare filed,error message is :' + err.message) 853}) 854``` 855 856### play<sup>9+</sup> 857 858play(callback: AsyncCallback\<void>): void 859 860Starts 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. 861 862**System capability**: SystemCapability.Multimedia.Media.AVPlayer 863 864**Parameters** 865 866| Name | Type | Mandatory| Description | 867| -------- | -------- | ---- | -------------------- | 868| callback | function | Yes | Callback used to return the result.| 869 870**Error codes** 871 872For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 873 874| ID| Error Message | 875| -------- | ------------------------------------------ | 876| 5400102 | Operation not allowed. Return by callback. | 877 878**Example** 879 880```ts 881avPlayer.play((err: BusinessError) => { 882 if (err == null) { 883 console.info('play success'); 884 } else { 885 console.error('play filed,error message is :' + err.message) 886 } 887}) 888``` 889 890### play<sup>9+</sup> 891 892play(): Promise\<void> 893 894Starts 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. 895 896**System capability**: SystemCapability.Multimedia.Media.AVPlayer 897 898**Return value** 899 900| Type | Description | 901| -------------- | ------------------------- | 902| Promise\<void> | Promise used to return the result.| 903 904**Error codes** 905 906For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 907 908| ID| Error Message | 909| -------- | ----------------------------------------- | 910| 5400102 | Operation not allowed. Return by promise. | 911 912**Example** 913 914```ts 915avPlayer.play().then(() => { 916 console.info('play success'); 917}, (err: BusinessError) => { 918 console.error('play filed,error message is :' + err.message) 919}) 920``` 921 922### pause<sup>9+</sup> 923 924pause(callback: AsyncCallback\<void>): void 925 926Pauses 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. 927 928**System capability**: SystemCapability.Multimedia.Media.AVPlayer 929 930**Parameters** 931 932| Name | Type | Mandatory| Description | 933| -------- | -------- | ---- | -------------------- | 934| callback | function | Yes | Callback used to return the result.| 935 936**Error codes** 937 938For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 939 940| ID| Error Message | 941| -------- | ------------------------------------------ | 942| 5400102 | Operation not allowed. Return by callback. | 943 944**Example** 945 946```ts 947avPlayer.pause((err: BusinessError) => { 948 if (err == null) { 949 console.info('pause success'); 950 } else { 951 console.error('pause filed,error message is :' + err.message) 952 } 953}) 954``` 955 956### pause<sup>9+</sup> 957 958pause(): Promise\<void> 959 960Pauses 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. 961 962**System capability**: SystemCapability.Multimedia.Media.AVPlayer 963 964**Return value** 965 966| Type | Description | 967| -------------- | ------------------------- | 968| Promise\<void> | Promise used to return the result.| 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```ts 981avPlayer.pause().then(() => { 982 console.info('pause success'); 983}, (err: BusinessError) => { 984 console.error('pause filed,error message is :' + err.message) 985}) 986``` 987 988### stop<sup>9+</sup> 989 990stop(callback: AsyncCallback\<void>): void 991 992Stops 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. 993 994**System capability**: SystemCapability.Multimedia.Media.AVPlayer 995 996**Parameters** 997 998| Name | Type | Mandatory| Description | 999| -------- | -------- | ---- | -------------------- | 1000| callback | function | Yes | Callback used to return the result.| 1001 1002**Error codes** 1003 1004For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1005 1006| ID| Error Message | 1007| -------- | ------------------------------------------ | 1008| 5400102 | Operation not allowed. Return by callback. | 1009 1010**Example** 1011 1012```ts 1013avPlayer.stop((err: BusinessError) => { 1014 if (err == null) { 1015 console.info('stop success'); 1016 } else { 1017 console.error('stop filed,error message is :' + err.message) 1018 } 1019}) 1020``` 1021 1022### stop<sup>9+</sup> 1023 1024stop(): Promise\<void> 1025 1026Stops 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. 1027 1028**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1029 1030**Return value** 1031 1032| Type | Description | 1033| -------------- | ------------------------- | 1034| Promise\<void> | Promise used to return the result.| 1035 1036**Error codes** 1037 1038For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1039 1040| ID| Error Message | 1041| -------- | ----------------------------------------- | 1042| 5400102 | Operation not allowed. Return by promise. | 1043 1044**Example** 1045 1046```ts 1047avPlayer.stop().then(() => { 1048 console.info('stop success'); 1049}, (err: BusinessError) => { 1050 console.error('stop filed,error message is :' + err.message) 1051}) 1052``` 1053 1054### reset<sup>9+</sup> 1055 1056reset(callback: AsyncCallback\<void>): void 1057 1058Resets 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. 1059 1060**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1061 1062**Parameters** 1063 1064| Name | Type | Mandatory| Description | 1065| -------- | -------- | ---- | -------------------- | 1066| callback | function | Yes | Callback used to return the result.| 1067 1068**Error codes** 1069 1070For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1071 1072| ID| Error Message | 1073| -------- | ------------------------------------------ | 1074| 5400102 | Operation not allowed. Return by callback. | 1075 1076**Example** 1077 1078```ts 1079avPlayer.reset((err: BusinessError) => { 1080 if (err == null) { 1081 console.info('reset success'); 1082 } else { 1083 console.error('reset filed,error message is :' + err.message) 1084 } 1085}) 1086``` 1087 1088### reset<sup>9+</sup> 1089 1090reset(): Promise\<void> 1091 1092Resets 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. 1093 1094**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1095 1096**Return value** 1097 1098| Type | Description | 1099| -------------- | ------------------------- | 1100| Promise\<void> | Promise used to return the result.| 1101 1102**Error codes** 1103 1104For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1105 1106| ID| Error Message | 1107| -------- | ----------------------------------------- | 1108| 5400102 | Operation not allowed. Return by promise. | 1109 1110**Example** 1111 1112```ts 1113avPlayer.reset().then(() => { 1114 console.info('reset success'); 1115}, (err: BusinessError) => { 1116 console.error('reset filed,error message is :' + err.message) 1117}) 1118``` 1119 1120### release<sup>9+</sup> 1121 1122release(callback: AsyncCallback\<void>): void 1123 1124Releases 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. 1125 1126**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1127 1128**Parameters** 1129 1130| Name | Type | Mandatory| Description | 1131| -------- | -------- | ---- | -------------------- | 1132| callback | function | Yes | Callback used to return the result.| 1133 1134**Error codes** 1135 1136For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1137 1138| ID| Error Message | 1139| -------- | ------------------------------------------ | 1140| 5400102 | Operation not allowed. Return by callback. | 1141 1142**Example** 1143 1144```ts 1145avPlayer.release((err: BusinessError) => { 1146 if (err == null) { 1147 console.info('release success'); 1148 } else { 1149 console.error('release filed,error message is :' + err.message) 1150 } 1151}) 1152``` 1153 1154### release<sup>9+</sup> 1155 1156release(): Promise\<void> 1157 1158Releases 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. 1159 1160**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1161 1162**Return value** 1163 1164| Type | Description | 1165| -------------- | ------------------------- | 1166| Promise\<void> | Promise used to return the result.| 1167 1168**Error codes** 1169 1170For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1171 1172| ID| Error Message | 1173| -------- | ----------------------------------------- | 1174| 5400102 | Operation not allowed. Return by promise. | 1175 1176**Example** 1177 1178```ts 1179avPlayer.release().then(() => { 1180 console.info('release success'); 1181}, (err: BusinessError) => { 1182 console.error('release filed,error message is :' + err.message) 1183}) 1184``` 1185 1186### getTrackDescription<sup>9+</sup> 1187 1188getTrackDescription(callback: AsyncCallback\<Array\<MediaDescription>>): void 1189 1190Obtains 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. 1191 1192**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1193 1194**Parameters** 1195 1196| Name | Type | Mandatory| Description | 1197| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------- | 1198| callback | AsyncCallback<Array<[MediaDescription](#mediadescription8)>> | Yes | Callback used to return a **MediaDescription** array, which records the audio and video track information.| 1199 1200**Error codes** 1201 1202For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1203 1204| ID| Error Message | 1205| -------- | ------------------------------------------ | 1206| 5400102 | Operation not allowed. Return by callback. | 1207 1208**Example** 1209 1210```ts 1211avPlayer.getTrackDescription((error: BusinessError, arrList: Array<media.MediaDescription>) => { 1212 if ((arrList) != null) { 1213 console.info('getTrackDescription success'); 1214 } else { 1215 console.error(`video getTrackDescription fail, error:${error}`); 1216 } 1217}); 1218``` 1219 1220### getTrackDescription<sup>9+</sup> 1221 1222getTrackDescription(): Promise\<Array\<MediaDescription>> 1223 1224Obtains 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. 1225 1226**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1227 1228**Return value** 1229 1230| Type | Description | 1231| ------------------------------------------------------ | ------------------------------------------------- | 1232| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return a **MediaDescription** array, which records the audio and video track information.| 1233 1234**Error codes** 1235 1236For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1237 1238| ID| Error Message | 1239| -------- | ----------------------------------------- | 1240| 5400102 | Operation not allowed. Return by promise. | 1241 1242**Example** 1243 1244```ts 1245avPlayer.getTrackDescription().then((arrList: Array<media.MediaDescription>) => { 1246 console.info('getTrackDescription success'); 1247}).catch((error: BusinessError) => { 1248 console.error(`video catchCallback, error:${error}`); 1249}); 1250``` 1251 1252### seek<sup>9+</sup> 1253 1254seek(timeMs: number, mode?:SeekMode): void 1255 1256Seeks 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](#onseekdone9) event. 1257This API is not supported in live mode. 1258 1259**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1260 1261**Parameters** 1262 1263| Name| Type | Mandatory| Description | 1264| ------ | ---------------------- | ---- | ------------------------------------------------------------ | 1265| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, [duration](#avplayer_duration)].| 1266| mode | [SeekMode](#seekmode8) | No | Seek mode based on the video I frame. The default value is **SEEK_PREV_SYNC**. **Set this parameter only for video playback.**| 1267 1268**Example** 1269 1270```ts 1271let seekTime: number = 1000 1272avPlayer.seek(seekTime, media.SeekMode.SEEK_PREV_SYNC) 1273``` 1274 1275### on('seekDone')<sup>9+</sup> 1276 1277on(type: 'seekDone', callback: Callback\<number>): void 1278 1279Subscribes to the event to check whether the seek operation takes effect. 1280 1281**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1282 1283**Parameters** 1284 1285| Name | Type | Mandatory| Description | 1286| -------- | -------- | ---- | ------------------------------------------------------------ | 1287| type | string | Yes | Event type, which is **'seekDone'** in this case. This event is triggered each time **seek()** is called.| 1288| 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.| 1289 1290**Example** 1291 1292```ts 1293avPlayer.on('seekDone', (seekDoneTime:number) => { 1294 console.info('seekDone success,and seek time is:' + seekDoneTime) 1295}) 1296``` 1297 1298### off('seekDone')<sup>9+</sup> 1299 1300off(type: 'seekDone'): void 1301 1302Unsubscribes from the event that checks whether the seek operation takes effect. 1303 1304**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1305 1306**Parameters** 1307 1308| Name| Type | Mandatory| Description | 1309| ------ | ------ | ---- | ---------------------------------------------------- | 1310| type | string | Yes | Event type, which is **'seekDone'** in this case.| 1311 1312**Example** 1313 1314```ts 1315avPlayer.off('seekDone') 1316``` 1317 1318### setSpeed<sup>9+</sup> 1319 1320setSpeed(speed: PlaybackSpeed): void 1321 1322Sets 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](#onspeeddone9) event. 1323This API is not supported in live mode. 1324 1325**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1326 1327**Parameters** 1328 1329| Name| Type | Mandatory| Description | 1330| ------ | -------------------------------- | ---- | ------------------ | 1331| speed | [PlaybackSpeed](#playbackspeed8) | Yes | Playback speed to set.| 1332 1333**Example** 1334 1335```ts 1336avPlayer.setSpeed(media.PlaybackSpeed.SPEED_FORWARD_2_00_X) 1337``` 1338 1339### on('speedDone')<sup>9+</sup> 1340 1341on(type: 'speedDone', callback: Callback\<number>): void 1342 1343Subscribes to the event to check whether the playback speed is successfully set. 1344 1345**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1346 1347**Parameters** 1348 1349| Name | Type | Mandatory| Description | 1350| -------- | -------- | ---- | ------------------------------------------------------------ | 1351| type | string | Yes | Event type, which is **'speedDone'** in this case. This event is triggered each time **setSpeed()** is called.| 1352| callback | Callback\<number> | Yes | Callback invoked when the event is triggered. It reports the speed set. For details, see [PlaybackSpeed](#playbackspeed8).| 1353 1354**Example** 1355 1356```ts 1357avPlayer.on('speedDone', (speed:number) => { 1358 console.info('speedDone success,and speed value is:' + speed) 1359}) 1360``` 1361 1362### off('speedDone')<sup>9+</sup> 1363 1364off(type: 'speedDone'): void 1365 1366Unsubscribes from the event that checks whether the playback speed is successfully set. 1367 1368**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1369 1370**Parameters** 1371 1372| Name| Type | Mandatory| Description | 1373| ------ | ------ | ---- | --------------------------------------------------------- | 1374| type | string | Yes | Event type, which is **'speedDone'** in this case.| 1375 1376**Example** 1377 1378```ts 1379avPlayer.off('speedDone') 1380``` 1381 1382### setBitrate<sup>9+</sup> 1383 1384setBitrate(bitrate: number): void 1385 1386Sets 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](#onbitratedone9) event. 1387 1388**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1389 1390**Parameters** 1391 1392| Name | Type | Mandatory| Description | 1393| ------- | ------ | ---- | ------------------------------------------------------------ | 1394| bitrate | number | Yes | Bit rate to set. You can obtain the available bit rates of the current HLS stream by subscribing to the [availableBitrates](#onavailablebitrates9) event. If the bit rate to set is not in the list of the available bit rates, the AVPlayer selects from the list the minimum bit rate that is closed to the bit rate to set. If the length of the available bit rate list obtained through the event is 0, no bit rate can be set and the **bitrateDone** callback will not be triggered.| 1395 1396**Example** 1397 1398```ts 1399let bitrate: number = 96000 1400avPlayer.setBitrate(bitrate) 1401``` 1402 1403### on('bitrateDone')<sup>9+</sup> 1404 1405on(type: 'bitrateDone', callback: Callback\<number>): void 1406 1407Subscribes to the event to check whether the bit rate is successfully set. 1408 1409**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1410 1411**Parameters** 1412 1413| Name | Type | Mandatory| Description | 1414| -------- | -------- | ---- | ------------------------------------------------------------ | 1415| type | string | Yes | Event type, which is **'bitrateDone'** in this case. This event is triggered each time **setBitrate()** is called.| 1416| callback | function | Yes | Callback invoked when the event is triggered. It reports the effective bit rate. | 1417 1418**Example** 1419 1420```ts 1421avPlayer.on('bitrateDone', (bitrate:number) => { 1422 console.info('bitrateDone success,and bitrate value is:' + bitrate) 1423}) 1424``` 1425 1426### off('bitrateDone')<sup>9+</sup> 1427 1428off(type: 'bitrateDone'): void 1429 1430Unsubscribes from the event that checks whether the bit rate is successfully set. 1431 1432**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1433 1434**Parameters** 1435 1436| Name| Type | Mandatory| Description | 1437| ------ | ------ | ---- | ------------------------------------------------------------ | 1438| type | string | Yes | Event type, which is **'bitrateDone'** in this case.| 1439 1440**Example** 1441 1442```ts 1443avPlayer.off('bitrateDone') 1444``` 1445 1446### on('availableBitrates')<sup>9+</sup> 1447 1448on(type: 'availableBitrates', callback: (bitrates: Array\<number>) => void): void 1449 1450Subscribes to available bit rates of HLS streams. This event is reported only after the AVPlayer switches to the prepared state. 1451 1452**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1453 1454**Parameters** 1455 1456| Name | Type | Mandatory| Description | 1457| -------- | -------- | ---- | ------------------------------------------------------------ | 1458| type | string | Yes | Event type, which is **'availableBitrates'** in this case. This event is triggered once after the AVPlayer switches to the prepared state.| 1459| callback | function | Yes | Callback invoked when the event is triggered. It returns an array that holds the available bit rates. If the array length is 0, no bit rate can be set.| 1460 1461**Example** 1462 1463```ts 1464avPlayer.on('availableBitrates', (bitrates: Array<number>) => { 1465 console.info('availableBitrates success,and availableBitrates length is:' + bitrates.length) 1466}) 1467``` 1468 1469### off('availableBitrates')<sup>9+</sup> 1470 1471off(type: 'availableBitrates'): void 1472 1473Unsubscribes from available bit rates of HLS streams. 1474 1475**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1476 1477**Parameters** 1478 1479| Name| Type | Mandatory| Description | 1480| ------ | ------ | ---- | ------------------------------------------------------------ | 1481| type | string | Yes | Event type, which is **'availableBitrates'** in this case.| 1482 1483**Example** 1484 1485```ts 1486avPlayer.off('availableBitrates') 1487``` 1488 1489### setVolume<sup>9+</sup> 1490 1491setVolume(volume: number): void 1492 1493Sets 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](#onvolumechange9) event. 1494 1495**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1496 1497**Parameters** 1498 1499| Name| Type | Mandatory| Description | 1500| ------ | ------ | ---- | ------------------------------------------------------------ | 1501| volume | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1.00** indicates the maximum volume (100%).| 1502 1503**Example** 1504 1505```ts 1506let volume: number = 1.0 1507avPlayer.setVolume(volume) 1508``` 1509 1510### on('volumeChange')<sup>9+</sup> 1511 1512on(type: 'volumeChange', callback: Callback\<number>): void 1513 1514Subscribes to the event to check whether the volume is successfully set. 1515 1516**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1517 1518**Parameters** 1519 1520| Name | Type | Mandatory| Description | 1521| -------- | -------- | ---- | ------------------------------------------------------------ | 1522| type | string | Yes | Event type, which is **'volumeChange'** in this case. This event is triggered each time **setVolume()** is called.| 1523| callback | function | Yes | Callback invoked when the event is triggered. It reports the effective volume. | 1524 1525**Example** 1526 1527```ts 1528avPlayer.on('volumeChange', (vol: number) => { 1529 console.info('volumeChange success,and new volume is :' + vol) 1530}) 1531``` 1532 1533### off('volumeChange')<sup>9+</sup> 1534 1535off(type: 'volumeChange'): void 1536 1537Unsubscribes from the event that checks whether the volume is successfully set. 1538 1539**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1540 1541**Parameters** 1542 1543| Name| Type | Mandatory| Description | 1544| ------ | ------ | ---- | ------------------------------------------------------------ | 1545| type | string | Yes | Event type, which is **'volumeChange'** in this case.| 1546 1547**Example** 1548 1549```ts 1550avPlayer.off('volumeChange') 1551``` 1552 1553### on('endOfStream')<sup>9+</sup> 1554 1555on(type: 'endOfStream', callback: Callback\<void>): void 1556 1557Subscribes 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](#onstatechange9) event. 1558 1559**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1560 1561**Parameters** 1562 1563| Name | Type | Mandatory| Description | 1564| -------- | -------- | ---- | ------------------------------------------------------------ | 1565| type | string | Yes | Event type, which is **'endOfStream'** in this case. This event is triggered when the AVPlayer finishes playing the media asset.| 1566| callback | Callback\<void> | Yes | Callback invoked when the event is triggered. | 1567 1568**Example** 1569 1570```ts 1571avPlayer.on('endOfStream', () => { 1572 console.info('endOfStream success') 1573}) 1574``` 1575 1576### off('endOfStream')<sup>9+</sup> 1577 1578off(type: 'endOfStream'): void 1579 1580Unsubscribes from the event that indicates the end of the stream being played. 1581 1582**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1583 1584**Parameters** 1585 1586| Name| Type | Mandatory| Description | 1587| ------ | ------ | ---- | ------------------------------------------------------------ | 1588| type | string | Yes | Event type, which is **'endOfStream'** in this case.| 1589 1590**Example** 1591 1592```ts 1593avPlayer.off('endOfStream') 1594``` 1595 1596### on('timeUpdate')<sup>9+</sup> 1597 1598on(type: 'timeUpdate', callback: Callback\<number>): void 1599 1600Subscribes 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. 1601The **'timeUpdate'** event is not supported in live mode. 1602 1603**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1604 1605**Parameters** 1606 1607| Name | Type | Mandatory| Description | 1608| -------- | -------- | ---- | ---------------------------------------------- | 1609| type | string | Yes | Event type, which is **'timeUpdate'** in this case.| 1610| callback | function | Yes | Callback invoked when the event is triggered. It reports the current playback position, in ms. | 1611 1612**Example** 1613 1614```ts 1615avPlayer.on('timeUpdate', (time:number) => { 1616 console.info('timeUpdate success,and new time is :' + time) 1617}) 1618``` 1619 1620### off('timeUpdate')<sup>9+</sup> 1621 1622off(type: 'timeUpdate'): void 1623 1624Unsubscribes from playback position changes. 1625 1626**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1627 1628**Parameters** 1629 1630| Name| Type | Mandatory| Description | 1631| ------ | ------ | ---- | -------------------------------------------------- | 1632| type | string | Yes | Event type, which is **'timeUpdate'** in this case.| 1633 1634**Example** 1635 1636```ts 1637avPlayer.off('timeUpdate') 1638``` 1639 1640### on('durationUpdate')<sup>9+</sup> 1641 1642 1643on(type: 'durationUpdate', callback: Callback\<number>): void 1644 1645Subscribes 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. 1646The **'durationUpdate'** event is not supported in live mode. 1647 1648**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1649 1650**Parameters** 1651 1652| Name | Type | Mandatory| Description | 1653| -------- | -------- | ---- | -------------------------------------------------- | 1654| type | string | Yes | Event type, which is **'durationUpdate'** in this case.| 1655| callback | function | Yes | Callback invoked when the event is triggered. It reports the media asset duration, in ms. | 1656 1657**Example** 1658 1659```ts 1660avPlayer.on('durationUpdate', (duration: number) => { 1661 console.info('durationUpdate success,new duration is :' + duration) 1662}) 1663``` 1664 1665### off('durationUpdate')<sup>9+</sup> 1666 1667off(type: 'durationUpdate'): void 1668 1669Unsubscribes from media asset duration changes. 1670 1671**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1672 1673**Parameters** 1674 1675| Name| Type | Mandatory| Description | 1676| ------ | ------ | ---- | ------------------------------------------------------ | 1677| type | string | Yes | Event type, which is **'durationUpdate'** in this case.| 1678 1679**Example** 1680 1681```ts 1682avPlayer.off('durationUpdate') 1683``` 1684 1685### on('bufferingUpdate')<sup>9+</sup> 1686 1687on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void 1688 1689Subscribes to audio and video buffer changes. This subscription is supported only in network playback scenarios. 1690 1691**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1692 1693**Parameters** 1694 1695| Name | Type | Mandatory| Description | 1696| -------- | -------- | ---- | ------------------------------------------------------------ | 1697| type | string | Yes | Event type, which is **'bufferingUpdate'** in this case. | 1698| 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**.| 1699 1700**Example** 1701 1702```ts 1703avPlayer.on('bufferingUpdate', (infoType: media.BufferingInfoType, value: number) => { 1704 console.info('bufferingUpdate success,and infoType value is:' + infoType + ', value is :' + value) 1705}) 1706``` 1707 1708### off('bufferingUpdate')<sup>9+</sup> 1709 1710off(type: 'bufferingUpdate'): void 1711 1712Unsubscribes from audio and video buffer changes. 1713 1714**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1715 1716**Parameters** 1717 1718| Name| Type | Mandatory| Description | 1719| ------ | ------ | ---- | --------------------------------------------------------- | 1720| type | string | Yes | Event type, which is **'bufferingUpdate'** in this case.| 1721 1722**Example** 1723 1724```ts 1725avPlayer.off('bufferingUpdate') 1726``` 1727 1728### on('startRenderFrame')<sup>9+</sup> 1729 1730on(type: 'startRenderFrame', callback: Callback\<void>): void 1731 1732Subscribes 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. 1733 1734**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1735 1736**Parameters** 1737 1738| Name | Type | Mandatory| Description | 1739| -------- | -------- | ---- | ------------------------------------------------------------ | 1740| type | string | Yes | Event type, which is **'startRenderFrame'** in this case.| 1741| callback | Callback\<void> | Yes | Callback invoked when the event is triggered. | 1742 1743**Example** 1744 1745```ts 1746avPlayer.on('startRenderFrame', () => { 1747 console.info('startRenderFrame success') 1748}) 1749``` 1750 1751### off('startRenderFrame')<sup>9+</sup> 1752 1753off(type: 'startRenderFrame'): void 1754 1755Unsubscribes from the event that indicates rendering starts for the first frame. 1756 1757**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1758 1759**Parameters** 1760 1761| Name| Type | Mandatory| Description | 1762| ------ | ------ | ---- | ------------------------------------------------------------ | 1763| type | string | Yes | Event type, which is **'startRenderFrame'** in this case.| 1764 1765**Example** 1766 1767```ts 1768avPlayer.off('startRenderFrame') 1769``` 1770 1771### on('videoSizeChange')<sup>9+</sup> 1772 1773on(type: 'videoSizeChange', callback: (width: number, height: number) => void): void 1774 1775Subscribes 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. 1776 1777**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1778 1779**Parameters** 1780 1781| Name | Type | Mandatory| Description | 1782| -------- | -------- | ---- | ------------------------------------------------------------ | 1783| type | string | Yes | Event type, which is **'videoSizeChange'** in this case.| 1784| callback | function | Yes | Callback invoked when the event is triggered. **width** indicates the video width, and **height** indicates the video height. | 1785 1786**Example** 1787 1788```ts 1789avPlayer.on('videoSizeChange', (width: number, height: number) => { 1790 console.info('videoSizeChange success,and width is:' + width + ', height is :' + height) 1791}) 1792``` 1793 1794### off('videoSizeChange')<sup>9+</sup> 1795 1796off(type: 'videoSizeChange'): void 1797 1798Unsubscribes from video size changes. 1799 1800**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1801 1802**Parameters** 1803 1804| Name| Type | Mandatory| Description | 1805| ------ | ------ | ---- | ------------------------------------------------------------ | 1806| type | string | Yes | Event type, which is **'videoSizeChange'** in this case.| 1807 1808**Example** 1809 1810```ts 1811avPlayer.off('videoSizeChange') 1812``` 1813 1814### on('audioInterrupt')<sup>9+</sup> 1815 1816on(type: 'audioInterrupt', callback: (info: audio.InterruptEvent) => void): void 1817 1818Subscribes 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). 1819 1820**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1821 1822**Parameters** 1823 1824| Name | Type | Mandatory| Description | 1825| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------------------- | 1826| type | string | Yes | Event type, which is **'audioInterrupt'** in this case.| 1827| callback | [audio.InterruptEvent<sup>9+</sup>](js-apis-audio.md#interruptevent9) | Yes | Callback invoked when the event is triggered. | 1828 1829**Example** 1830 1831```ts 1832import audio from '@ohos.multimedia.audio'; 1833 1834avPlayer.on('audioInterrupt', (info: audio.InterruptEvent) => { 1835 console.info('audioInterrupt success,and InterruptEvent info is:' + info) 1836}) 1837``` 1838 1839### off('audioInterrupt')<sup>9+</sup> 1840 1841off(type: 'audioInterrupt'): void 1842 1843Unsubscribes from the audio interruption event. 1844 1845**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1846 1847**Parameters** 1848 1849| Name| Type | Mandatory| Description | 1850| ------ | ------ | ---- | ------------------------------------------------------------ | 1851| type | string | Yes | Event type, which is **'audioInterrupt'** in this case.| 1852 1853**Example** 1854 1855```ts 1856avPlayer.off('audioInterrupt') 1857``` 1858 1859### on('audioOutputDeviceChangeWithInfo') <sup>11+</sup> 1860 1861on(type: 'audioOutputDeviceChangeWithInfo', callback: Callback\<audio.AudioStreamDeviceChangeInfo>): void 1862 1863Subscribes to audio stream output device changes and reasons. This API uses an asynchronous callback to return the result. 1864 1865**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1866 1867**Parameters** 1868 1869| Name | Type | Mandatory| Description | 1870| :------- | :------------------------- | :--- | :------------------------------------------ | 1871| type | string | Yes | Event type. The event **'outputDeviceChangeWithInfo'** is triggered when the output device is changed.| 1872| callback | Callback\<[AudioStreamDeviceChangeInfo](js-apis-audio.md#audiostreamdevicechangeinfo11)> | Yes | Callback used to return the output device descriptor of the current audio stream and the change reason.| 1873 1874 1875**Example** 1876 1877```ts 1878import audio from '@ohos.multimedia.audio'; 1879 1880avPlayer.on('audioOutputDeviceChangeWithInfo', (data: audio.AudioStreamDeviceChangeInfo) => { 1881 console.info(`${JSON.stringify(data)}`); 1882}); 1883``` 1884 1885### off('audioOutputDeviceChangeWithInfo') <sup>11+</sup> 1886 1887off(type: 'audioOutputDeviceChangeWithInfo', callback?: Callback\<audio.AudioStreamDeviceChangeInfo>): void 1888 1889Unsubscribes from audio stream output device changes and reasons. This API uses an asynchronous callback to return the result. 1890 1891**System capability**: SystemCapability.Multimedia.Audio.Device 1892 1893**Parameters** 1894 1895| Name | Type | Mandatory| Description | 1896| :------- | :------------------------- | :--- | :------------------------------------------ | 1897| type | string | Yes | Event type. The event **'outputDeviceChange'** is triggered when the audio output device is changed.| 1898| callback | Callback\<[AudioStreamDeviceChangeInfo](js-apis-audio.md#audiostreamdevicechangeinfo11)> | No | Callback used to return the output device descriptor of the current audio stream and the change reason.| 1899 1900**Example** 1901 1902```ts 1903avPlayer.off('audioOutputDeviceChangeWithInfo'); 1904``` 1905 1906## AVPlayerState<sup>9+</sup> 1907 1908Enumerates 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](##onstatechange9) event. For details about the rules for state transition, see [Audio Playback](../../media/using-avplayer-for-playback.md). 1909 1910**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1911 1912| Name | Type | Description | 1913| :-----------------------------: | :----: | :----------------------------------------------------------- | 1914| 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>**, **fdSrc<sup>9+</sup>**, or **dataSrc<sup>10+</sup>** attribute and the **loop** attribute are reset, and other attributes are retained.| 1915| 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.| 1916| 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.| 1917| playing | string | The AVPlayer enters this state when **play()** is called in the prepared, paused, or completed state.| 1918| paused | string | The AVPlayer enters this state when **pause()** is called in the playing state.| 1919| 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.| 1920| 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.| 1921| 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.| 1922| error | 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 [Media Error Codes](../errorcodes/errorcode-media.md).<br>**NOTE** Relationship between the error state and the [on('error')](#onerror9) event<br>1. When the AVPlayer enters the error state, the **on('error')** 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')** 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.| 1923 1924## AVFileDescriptor<sup>9+</sup> 1925 1926Describes 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. 1927 1928**System capability**: SystemCapability.Multimedia.Media.Core 1929 1930| Name | Type | Mandatory| Description | 1931| ------ | ------ | ---- | ------------------------------------------------------------ | 1932| fd | number | Yes | Resource handle, which is obtained by calling [resourceManager.getRawFd](js-apis-resource-manager.md#getrawfd9). | 1933| offset | number | No | 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.| 1934| length | number | No | 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.| 1935 1936## AVDataSrcDescriptor<sup>10+</sup> 1937 1938Defines the descriptor of an audio and video file, which is used in DataSource playback mode.<br>Use scenario: An application can create a playback instance and start playback before it finishes downloading the audio and video resources. 1939 1940**System capability**: SystemCapability.Multimedia.Media.AVPlayer 1941 1942| Name | Type | Mandatory| Description | 1943| ------ | ------ | ---- | ------------------------------------------------------------ | 1944| fileSize | number | Yes | Size of the file to play, in bytes. The value **-1** indicates that the size is unknown. If **fileSize** is set to **-1**, the playback mode is similar to the live mode. In this mode, the **seek** and **setSpeed** operations cannot be performed, and the **loop** attribute cannot be set, indicating that loop playback is unavailable.| 1945| callback | function | Yes | Callback used to fill in data.<br>- **buffer**: memory to be filled. The value is of the ArrayBuffer type. This parameter is mandatory.<br>- **length**: maximum length of the memory to be filled. The value is of the number type. This parameter is mandatory.<br>- **pos**: position of the data to be filled in the file. The value is of the number type. This parameter is optional. When **fileSize** is set to **-1**, this parameter cannot be used.| 1946 1947 1948## SeekMode<sup>8+</sup> 1949 1950Enumerates the video playback seek modes, which can be passed in the **seek** API. 1951 1952**System capability**: SystemCapability.Multimedia.Media.Core 1953 1954| Name | Value | Description | 1955| -------------- | ---- | ------------------------------------------------------------ | 1956| 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.| 1957| 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.| 1958 1959## PlaybackSpeed<sup>8+</sup> 1960 1961Enumerates the video playback speeds, which can be passed in the **setSpeed** API. 1962 1963**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1964 1965| Name | Value | Description | 1966| -------------------- | ---- | ------------------------------ | 1967| SPEED_FORWARD_0_75_X | 0 | Plays the video at 0.75 times the normal speed.| 1968| SPEED_FORWARD_1_00_X | 1 | Plays the video at the normal speed. | 1969| SPEED_FORWARD_1_25_X | 2 | Plays the video at 1.25 times the normal speed.| 1970| SPEED_FORWARD_1_75_X | 3 | Plays the video at 1.75 times the normal speed.| 1971| SPEED_FORWARD_2_00_X | 4 | Plays the video at 2.00 times the normal speed.| 1972 1973## VideoScaleType<sup>9+</sup> 1974 1975Enumerates the video scale modes. 1976 1977**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 1978 1979| Name | Value | Description | 1980| ------------------------- | ---- | ------------------------------------------------ | 1981| VIDEO_SCALE_TYPE_FIT | 0 | The video will be stretched to fit the window. | 1982| 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.| 1983 1984## MediaDescription<sup>8+</sup> 1985 1986Defines media information in key-value mode. 1987 1988**System capability**: SystemCapability.Multimedia.Media.Core 1989 1990| Name | Type | Mandatory| Description | 1991| ------------- | ------ | ---- | ------------------------------------------------------------ | 1992| [key: string] | Object | Yes | For details about the key range supported and the object type and range of each key, see [MediaDescriptionKey](#mediadescriptionkey8).| 1993 1994**Example** 1995 1996```ts 1997function printfItemDescription(obj: media.MediaDescription, key: string) { 1998 let property: Object = obj[key]; 1999 console.info('audio key is ' + key); // Specify a key. For details about the keys, see [MediaDescriptionKey]. 2000 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]. 2001} 2002 2003avPlayer.getTrackDescription((error: BusinessError, arrList: Array<media.MediaDescription>) => { 2004 if (arrList != null) { 2005 for (let i = 0; i < arrList.length; i++) { 2006 printfItemDescription(arrList[i], media.MediaDescriptionKey.MD_KEY_TRACK_TYPE); // Print the MD_KEY_TRACK_TYPE value of each track. 2007 } 2008 } else { 2009 console.error(`audio getTrackDescription fail, error:${error}`); 2010 } 2011}); 2012``` 2013 2014## AVRecorder<sup>9+</sup> 2015 2016A 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. 2017 2018For details about the audio and video recording demo, see [Audio Recording](../../media/using-avrecorder-for-recording.md) and [Video Recording](../../media/video-recording.md). 2019 2020> **NOTE** 2021> 2022> To use the camera to record videos, the camera module is required. For details about how to use the APIs provided by the camera module, see [Camera Management](js-apis-camera.md). 2023 2024### Attributes 2025 2026**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2027 2028| Name | Type | Readable| Writable| Description | 2029| ------- | ------------------------------------ | ---- | ---- | ------------------ | 2030| state9+ | [AVRecorderState](#avrecorderstate9) | Yes | No | AVRecorder state.| 2031 2032### prepare<sup>9+</sup> 2033 2034prepare(config: AVRecorderConfig, callback: AsyncCallback\<void>): void 2035 2036Sets audio and video recording parameters. This API uses an asynchronous callback to return the result. 2037 2038**Required permissions:** ohos.permission.MICROPHONE 2039 2040This permission is required only if audio recording is involved. 2041 2042To use the camera to record videos, the camera module is required. For details about how to use the APIs provided by the camera module, see [Camera Management](js-apis-camera.md). 2043 2044**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2045 2046**Parameters** 2047 2048| Name | Type | Mandatory| Description | 2049| -------- | -------------------------------------- | ---- | ------------------------------------- | 2050| config | [AVRecorderConfig](#avrecorderconfig9) | Yes | Audio and video recording parameters to set. | 2051| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2052 2053**Error codes** 2054 2055For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2056 2057| ID| Error Message | 2058| -------- | --------------------------------------- | 2059| 201 | Permission denied. Return by callback. | 2060| 401 | Parameter error. Return by callback. | 2061| 5400102 | Operate not permit. Return by callback. | 2062| 5400105 | Service died. Return by callback. | 2063 2064**Example** 2065 2066```ts 2067// Configure the parameters based on those supported by the hardware device. 2068let avRecorderProfile: media.AVRecorderProfile = { 2069 audioBitrate : 48000, 2070 audioChannels : 2, 2071 audioCodec : media.CodecMimeType.AUDIO_AAC, 2072 audioSampleRate : 48000, 2073 fileFormat : media.ContainerFormatType.CFT_MPEG_4, 2074 videoBitrate : 2000000, 2075 videoCodec : media.CodecMimeType.VIDEO_AVC, 2076 videoFrameWidth : 640, 2077 videoFrameHeight : 480, 2078 videoFrameRate : 30 2079} 2080let avRecorderConfig: media.AVRecorderConfig = { 2081 audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, 2082 videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 2083 profile : avRecorderProfile, 2084 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: fd://45. 2085 rotation: 0, // The value can be 0, 90, 180, or 270. If any other value is used, prepare() reports an error. 2086 location : { latitude : 30, longitude : 130 } 2087} 2088 2089avRecorder.prepare(avRecorderConfig, (err: BusinessError) => { 2090 if (err == null) { 2091 console.info('prepare success'); 2092 } else { 2093 console.error('prepare failed and error is ' + err.message); 2094 } 2095}) 2096``` 2097 2098### prepare<sup>9+</sup> 2099 2100prepare(config: AVRecorderConfig): Promise\<void> 2101 2102Sets audio and video recording parameters. This API uses a promise to return the result. 2103 2104**Required permissions:** ohos.permission.MICROPHONE 2105 2106This permission is required only if audio recording is involved. 2107 2108To use the camera to record videos, the camera module is required. For details about how to use the APIs provided by the camera module, see [Camera Management](js-apis-camera.md). 2109 2110**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2111 2112**Parameters** 2113 2114| Name| Type | Mandatory| Description | 2115| ------ | -------------------------------------- | ---- | -------------------------- | 2116| config | [AVRecorderConfig](#avrecorderconfig9) | Yes | Audio and video recording parameters to set.| 2117 2118**Return value** 2119 2120| Type | Description | 2121| -------------- | ------------------------------------------ | 2122| Promise\<void> | Promise used to return the result.| 2123 2124**Error codes** 2125 2126For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2127 2128| ID| Error Message | 2129| -------- | -------------------------------------- | 2130| 201 | Permission denied. Return by promise. | 2131| 401 | Parameter error. Return by promise. | 2132| 5400102 | Operate not permit. Return by promise. | 2133| 5400105 | Service died. Return by promise. | 2134 2135**Example** 2136 2137```ts 2138// Configure the parameters based on those supported by the hardware device. 2139let avRecorderProfile: media.AVRecorderProfile = { 2140 audioBitrate : 48000, 2141 audioChannels : 2, 2142 audioCodec : media.CodecMimeType.AUDIO_AAC, 2143 audioSampleRate : 48000, 2144 fileFormat : media.ContainerFormatType.CFT_MPEG_4, 2145 videoBitrate : 2000000, 2146 videoCodec : media.CodecMimeType.VIDEO_AVC, 2147 videoFrameWidth : 640, 2148 videoFrameHeight : 480, 2149 videoFrameRate : 30 2150} 2151let avRecorderConfig: media.AVRecorderConfig = { 2152 audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, 2153 videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 2154 profile : avRecorderProfile, 2155 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: fd://45. 2156 rotation: 0, // The value can be 0, 90, 180, or 270. If any other value is used, prepare() reports an error. 2157 location : { latitude : 30, longitude : 130 } 2158} 2159 2160avRecorder.prepare(avRecorderConfig).then(() => { 2161 console.info('prepare success'); 2162}).catch((err: BusinessError) => { 2163 console.error('prepare failed and catch error is ' + err.message); 2164}); 2165``` 2166 2167### getInputSurface<sup>9+</sup> 2168 2169getInputSurface(callback: AsyncCallback\<string>): void 2170 2171Obtains 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. 2172 2173Note 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. 2174 2175This API can be called only after the **prepare()** API is called. 2176 2177**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2178 2179**Parameters** 2180 2181| Name | Type | Mandatory| Description | 2182| -------- | ---------------------- | ---- | --------------------------- | 2183| callback | AsyncCallback\<string> | Yes | Callback used to obtain the result.| 2184 2185**Error codes** 2186 2187For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2188 2189| ID| Error Message | 2190| -------- | --------------------------------------- | 2191| 5400102 | Operate not permit. Return by callback. | 2192| 5400103 | IO error. Return by callback. | 2193| 5400105 | Service died. Return by callback. | 2194 2195**Example** 2196 2197```ts 2198let surfaceID: string; // The surfaceID is transferred to the camera API to create a videoOutput instance. 2199 2200avRecorder.getInputSurface((err: BusinessError, surfaceId: string) => { 2201 if (err == null) { 2202 console.info('getInputSurface success'); 2203 surfaceID = surfaceId; 2204 } else { 2205 console.error('getInputSurface failed and error is ' + err.message); 2206 } 2207}); 2208 2209``` 2210 2211### getInputSurface<sup>9+</sup> 2212 2213getInputSurface(): Promise\<string> 2214 2215Obtains 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. 2216 2217Note 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. 2218 2219This API can be called only after the **prepare()** API is called. 2220 2221**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2222 2223**Return value** 2224 2225| Type | Description | 2226| ---------------- | -------------------------------- | 2227| Promise\<string> | Promise used to return the result.| 2228 2229**Error codes** 2230 2231For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2232 2233| ID| Error Message | 2234| -------- | -------------------------------------- | 2235| 5400102 | Operate not permit. Return by promise. | 2236| 5400103 | IO error. Return by promise. | 2237| 5400105 | Service died. Return by promise. | 2238 2239**Example** 2240 2241```ts 2242let surfaceID: string; // The surfaceID is transferred to the camera API to create a videoOutput instance. 2243 2244avRecorder.getInputSurface().then((surfaceId: string) => { 2245 console.info('getInputSurface success'); 2246 surfaceID = surfaceId; 2247}).catch((err: BusinessError) => { 2248 console.error('getInputSurface failed and catch error is ' + err.message); 2249}); 2250``` 2251 2252### start<sup>9+</sup> 2253 2254start(callback: AsyncCallback\<void>): void 2255 2256Starts recording. This API uses an asynchronous callback to return the result. 2257 2258For 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. 2259 2260**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2261 2262**Parameters** 2263 2264| Name | Type | Mandatory| Description | 2265| -------- | -------------------- | ---- | ---------------------------- | 2266| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2267 2268**Error codes** 2269 2270For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2271 2272| ID| Error Message | 2273| -------- | --------------------------------------- | 2274| 5400102 | Operate not permit. Return by callback. | 2275| 5400103 | IO error. Return by callback. | 2276| 5400105 | Service died. Return by callback. | 2277 2278**Example** 2279 2280```ts 2281avRecorder.start((err: BusinessError) => { 2282 if (err == null) { 2283 console.info('start AVRecorder success'); 2284 } else { 2285 console.error('start AVRecorder failed and error is ' + err.message); 2286 } 2287}); 2288``` 2289 2290### start<sup>9+</sup> 2291 2292start(): Promise\<void> 2293 2294Starts recording. This API uses a promise to return the result. 2295 2296For 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. 2297 2298**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2299 2300**Return value** 2301 2302| Type | Description | 2303| -------------- | ------------------------------------- | 2304| Promise\<void> | Promise used to return the result.| 2305 2306**Error codes** 2307 2308For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2309 2310| ID| Error Message | 2311| -------- | -------------------------------------- | 2312| 5400102 | Operate not permit. Return by promise. | 2313| 5400103 | IO error. Return by promise. | 2314| 5400105 | Service died. Return by promise. | 2315 2316**Example** 2317 2318```ts 2319avRecorder.start().then(() => { 2320 console.info('start AVRecorder success'); 2321}).catch((err: BusinessError) => { 2322 console.error('start AVRecorder failed and catch error is ' + err.message); 2323}); 2324``` 2325 2326### pause<sup>9+</sup> 2327 2328pause(callback: AsyncCallback\<void>): void 2329 2330Pauses recording. This API uses an asynchronous callback to return the result. 2331 2332This API can be called only after the **start()** API is called. You can call **resume()** to resume recording. 2333 2334**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2335 2336**Parameters** 2337 2338| Name | Type | Mandatory| Description | 2339| -------- | -------------------- | ---- | --------------------------- | 2340| callback | AsyncCallback\<void> | Yes | Callback used to obtain the result.| 2341 2342**Error codes** 2343 2344For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2345 2346| ID| Error Message | 2347| -------- | --------------------------------------- | 2348| 5400102 | Operate not permit. Return by callback. | 2349| 5400103 | IO error. Return by callback. | 2350| 5400105 | Service died. Return by callback. | 2351 2352**Example** 2353 2354```ts 2355avRecorder.pause((err: BusinessError) => { 2356 if (err == null) { 2357 console.info('pause AVRecorder success'); 2358 } else { 2359 console.error('pause AVRecorder failed and error is ' + err.message); 2360 } 2361}); 2362``` 2363 2364### pause<sup>9+</sup> 2365 2366pause(): Promise\<void> 2367 2368Pauses recording. This API uses a promise to return the result. 2369 2370This API can be called only after the **start()** API is called. You can call **resume()** to resume recording. 2371 2372**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2373 2374**Return value** 2375 2376| Type | Description | 2377| -------------- | ------------------------------------- | 2378| Promise\<void> | Promise used to return the result.| 2379 2380**Error codes** 2381 2382For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2383 2384| ID| Error Message | 2385| -------- | -------------------------------------- | 2386| 5400102 | Operate not permit. Return by promise. | 2387| 5400103 | IO error. Return by promise. | 2388| 5400105 | Service died. Return by promise. | 2389 2390**Example** 2391 2392```ts 2393avRecorder.pause().then(() => { 2394 console.info('pause AVRecorder success'); 2395}).catch((err: BusinessError) => { 2396 console.error('pause AVRecorder failed and catch error is ' + err.message); 2397}); 2398``` 2399 2400### resume<sup>9+</sup> 2401 2402resume(callback: AsyncCallback\<void>): void 2403 2404Resumes recording. This API uses an asynchronous callback to return the result. 2405 2406This API can be called only after the **pause()** API is called. 2407 2408**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2409 2410**Parameters** 2411 2412| Name | Type | Mandatory| Description | 2413| -------- | -------------------- | ---- | ---------------------------- | 2414| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2415 2416**Error codes** 2417 2418For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2419 2420| ID| Error Message | 2421| -------- | --------------------------------------- | 2422| 5400102 | Operate not permit. Return by callback. | 2423| 5400103 | IO error. Return by callback. | 2424| 5400105 | Service died. Return by callback. | 2425 2426**Example** 2427 2428```ts 2429avRecorder.resume((err: BusinessError) => { 2430 if (err == null) { 2431 console.info('resume AVRecorder success'); 2432 } else { 2433 console.error('resume AVRecorder failed and error is ' + err.message); 2434 } 2435}); 2436``` 2437 2438### resume<sup>9+</sup> 2439 2440resume(): Promise\<void> 2441 2442Resumes recording. This API uses a promise to return the result. 2443 2444This API can be called only after the **pause()** API is called. 2445 2446**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2447 2448**Return value** 2449 2450| Type | Description | 2451| -------------- | ------------------------------------- | 2452| Promise\<void> | Promise used to return the result.| 2453 2454**Error codes** 2455 2456For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2457 2458| ID| Error Message | 2459| -------- | -------------------------------------- | 2460| 5400102 | Operate not permit. Return by promise. | 2461| 5400103 | IO error. Return by promise. | 2462| 5400105 | Service died. Return by promise. | 2463 2464**Example** 2465 2466```ts 2467avRecorder.resume().then(() => { 2468 console.info('resume AVRecorder success'); 2469}).catch((err: BusinessError) => { 2470 console.error('resume AVRecorder failed and catch error is ' + err.message); 2471}); 2472``` 2473 2474### stop<sup>9+</sup> 2475 2476stop(callback: AsyncCallback\<void>): void 2477 2478Stops recording. This API uses an asynchronous callback to return the result. 2479 2480This API can be called only after the **start()** or **pause()** API is called. 2481 2482For 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. 2483 2484**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2485 2486**Parameters** 2487 2488| Name | Type | Mandatory| Description | 2489| -------- | -------------------- | ---- | ---------------------------- | 2490| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2491 2492**Error codes** 2493 2494For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2495 2496| ID| Error Message | 2497| -------- | --------------------------------------- | 2498| 5400102 | Operate not permit. Return by callback. | 2499| 5400103 | IO error. Return by callback. | 2500| 5400105 | Service died. Return by callback. | 2501 2502**Example** 2503 2504```ts 2505avRecorder.stop((err: BusinessError) => { 2506 if (err == null) { 2507 console.info('stop AVRecorder success'); 2508 } else { 2509 console.error('stop AVRecorder failed and error is ' + err.message); 2510 } 2511}); 2512``` 2513 2514### stop<sup>9+</sup> 2515 2516stop(): Promise\<void> 2517 2518Stops recording. This API uses a promise to return the result. 2519 2520This API can be called only after the **start()** or **pause()** API is called. 2521 2522For 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. 2523 2524**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2525 2526**Return value** 2527 2528| Type | Description | 2529| -------------- | ------------------------------------- | 2530| Promise\<void> | Promise used to return the result.| 2531 2532**Error codes** 2533 2534For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2535 2536| ID| Error Message | 2537| -------- | -------------------------------------- | 2538| 5400102 | Operate not permit. Return by promise. | 2539| 5400103 | IO error. Return by promise. | 2540| 5400105 | Service died. Return by promise. | 2541 2542**Example** 2543 2544```ts 2545avRecorder.stop().then(() => { 2546 console.info('stop AVRecorder success'); 2547}).catch((err: BusinessError) => { 2548 console.error('stop AVRecorder failed and catch error is ' + err.message); 2549}); 2550``` 2551 2552### reset<sup>9+</sup> 2553 2554reset(callback: AsyncCallback\<void>): void 2555 2556Resets audio and video recording. This API uses an asynchronous callback to return the result. 2557 2558For 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. 2559 2560**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2561 2562**Parameters** 2563 2564| Name | Type | Mandatory| Description | 2565| -------- | -------------------- | ---- | ------------------------------ | 2566| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2567 2568**Error codes** 2569 2570For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2571 2572| ID| Error Message | 2573| -------- | --------------------------------- | 2574| 5400103 | IO error. Return by callback. | 2575| 5400105 | Service died. Return by callback. | 2576 2577**Example** 2578 2579```ts 2580avRecorder.reset((err: BusinessError) => { 2581 if (err == null) { 2582 console.info('reset AVRecorder success'); 2583 } else { 2584 console.error('reset AVRecorder failed and error is ' + err.message); 2585 } 2586}); 2587``` 2588 2589### reset<sup>9+</sup> 2590 2591reset(): Promise\<void> 2592 2593Resets audio and video recording. This API uses a promise to return the result. 2594 2595For 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. 2596 2597**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2598 2599**Return value** 2600 2601| Type | Description | 2602| -------------- | --------------------------------------- | 2603| Promise\<void> | Promise used to return the result.| 2604 2605**Error codes** 2606 2607For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2608 2609| ID| Error Message | 2610| -------- | -------------------------------- | 2611| 5400103 | IO error. Return by promise. | 2612| 5400105 | Service died. Return by promise. | 2613 2614**Example** 2615 2616```ts 2617avRecorder.reset().then(() => { 2618 console.info('reset AVRecorder success'); 2619}).catch((err: BusinessError) => { 2620 console.error('reset AVRecorder failed and catch error is ' + err.message); 2621}); 2622``` 2623 2624### release<sup>9+</sup> 2625 2626release(callback: AsyncCallback\<void>): void 2627 2628Releases the audio and video recording resources. This API uses an asynchronous callback to return the result. 2629 2630After the resources are released, you can no longer perform any operation on the **AVRecorder** instance. 2631 2632**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2633 2634**Parameters** 2635 2636| Name | Type | Mandatory| Description | 2637| -------- | -------------------- | ---- | ---------------------------------- | 2638| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 2639 2640**Error codes** 2641 2642For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2643 2644| ID| Error Message | 2645| -------- | --------------------------------- | 2646| 5400105 | Service died. Return by callback. | 2647 2648**Example** 2649 2650```ts 2651avRecorder.release((err: BusinessError) => { 2652 if (err == null) { 2653 console.info('release AVRecorder success'); 2654 } else { 2655 console.error('release AVRecorder failed and error is ' + err.message); 2656 } 2657}); 2658``` 2659 2660### release<sup>9+</sup> 2661 2662release(): Promise\<void> 2663 2664Releases the audio and video recording resources. This API uses a promise to return the result. 2665 2666After the resources are released, you can no longer perform any operation on the **AVRecorder** instance. 2667 2668**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2669 2670**Return value** 2671 2672| Type | Description | 2673| -------------- | ------------------------------------------- | 2674| Promise\<void> | Promise used to return the result.| 2675 2676**Error codes** 2677 2678For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2679 2680| ID| Error Message | 2681| -------- | --------------------------------- | 2682| 5400105 | Service died. Return by callback. | 2683 2684**Example** 2685 2686```ts 2687avRecorder.release().then(() => { 2688 console.info('release AVRecorder success'); 2689}).catch((err: BusinessError) => { 2690 console.error('release AVRecorder failed and catch error is ' + err.message); 2691}); 2692``` 2693 2694### on('stateChange')<sup>9+</sup> 2695 2696on(type: 'stateChange', callback: (state: AVRecorderState, reason: StateChangeReason) => void): void 2697 2698Subscribes 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. 2699 2700**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2701 2702**Parameters** 2703 2704| Name | Type | Mandatory| Description | 2705| -------- | -------- | ---- | ------------------------------------------------------------ | 2706| type | string | Yes | Event type, which is **'stateChange'** in this case. This event can be triggered by both user operations and the system.| 2707| 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.| 2708 2709**Error codes** 2710 2711For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2712 2713| ID| Error Message | 2714| -------- | --------------------------------- | 2715| 5400103 | IO error. Return by callback. | 2716| 5400105 | Service died. Return by callback. | 2717 2718**Example** 2719 2720```ts 2721avRecorder.on('stateChange', async (state: media.AVRecorderState, reason: media.StateChangeReason) => { 2722 console.info('case state has changed, new state is :' + state + ',and new reason is : ' + reason); 2723}); 2724``` 2725 2726### off('stateChange')<sup>9+</sup> 2727 2728off(type: 'stateChange'): void 2729 2730Unsubscribes from AVRecorder state changes. 2731 2732**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2733 2734**Parameters** 2735 2736| Name| Type | Mandatory| Description | 2737| ------ | ------ | ---- | ------------------------------------------------------------ | 2738| type | string | Yes | Event type, which is **'stateChange'** in this case. This event can be triggered by both user operations and the system.| 2739 2740**Example** 2741 2742```ts 2743avRecorder.off('stateChange'); 2744``` 2745 2746### on('error')<sup>9+</sup> 2747 2748on(type: 'error', callback: ErrorCallback): void 2749 2750Subscribes 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. 2751 2752An application can subscribe to only one AVRecorder error event. When the application initiates multiple subscriptions to this event, the last subscription prevails. 2753 2754**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2755 2756**Parameters** 2757 2758| Name | Type | Mandatory| Description | 2759| -------- | ------------- | ---- | ------------------------------------------------------------ | 2760| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during recording.| 2761| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 2762 2763**Error codes** 2764 2765For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2766 2767| ID| Error Message | 2768| -------- | ------------------------------------------------ | 2769| 5400101 | No memory. Return by callback. | 2770| 5400102 | Operation not allowed. Return by callback. | 2771| 5400103 | I/O error. Return by callback. | 2772| 5400104 | Time out. Return by callback. | 2773| 5400105 | Service died. Return by callback. | 2774| 5400106 | Unsupport format. Return by callback. | 2775 2776**Example** 2777 2778```ts 2779avRecorder.on('error', (err: BusinessError) => { 2780 console.info('case avRecorder.on(error) called, errMessage is ' + err.message); 2781}); 2782``` 2783 2784### off('error')<sup>9+</sup> 2785 2786off(type: 'error'): void 2787 2788Unsubscribes from AVRecorder errors. After the unsubscription, your application can no longer receive AVRecorder errors. 2789 2790**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2791 2792**Parameters** 2793 2794| Name| Type | Mandatory| Description | 2795| ------ | ------ | ---- | ------------------------------------------------------------ | 2796| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during recording.| 2797 2798**Example** 2799 2800```ts 2801avRecorder.off('error'); 2802``` 2803 2804## AVRecorderState<sup>9+</sup> 2805 2806Enumerates the AVRecorder states. You can obtain the state through the **state** attribute. 2807 2808**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2809 2810| Name | Type | Description | 2811| -------- | ------ | ------------------------------------------------------------ | 2812| idle | string | The AVRecorder enters this state after it is just created or the [AVRecorder.reset()](#reset9-2) API is called when the AVRecorder is in any state except released. In this state, you can call [AVRecorder.prepare()](#prepare9-2) to set recording parameters. | 2813| prepared | string | The AVRecorder enters this state when the parameters are set. In this state, you can call [AVRecorder.start()](#start9) to start recording.| 2814| started | string | The AVRecorder enters this state when the recording starts. In this state, you can call [AVRecorder.pause()](#pause9-2) to pause recording or call [AVRecorder.stop()](#stop9-2) to stop recording.| 2815| paused | string | The AVRecorder enters this state when the recording is paused. In this state, you can call [AVRecorder.resume()](#resume9) to continue recording or call [AVRecorder.stop()](#stop9-2) to stop recording.| 2816| stopped | string | The AVRecorder enters this state when the recording stops. In this state, you can call [AVRecorder.prepare()](#prepare9-2) to set recording parameters so that the AVRecorder enters the prepared state again.| 2817| 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 [AVRecorder.release()](#release9-2) to enter the released state.| 2818| error | string | The AVRecorder enters this state when an irreversible error occurs in the **AVRecorder** instance. In this state, the [AVRecorder.on('error') event](#onerror9-1) is reported, with the detailed error cause. In the error state, you must call [AVRecorder.reset()](#reset9-2) to reset the **AVRecorder** instance or call [AVRecorder.release()](#release9-2) to release the resources.| 2819 2820## AVRecorderConfig<sup>9+</sup> 2821 2822Describes the audio and video recording parameters. 2823 2824The **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**. 2825 2826**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2827 2828| Name | Type | Mandatory| Description | 2829| --------------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | 2830| audioSourceType | [AudioSourceType](#audiosourcetype9) | No | Type of the audio source to record. This parameter is mandatory for audio recording. | 2831| videoSourceType | [VideoSourceType](#videosourcetype9) | No | Type of the video source to record. This parameter is mandatory for video recording. | 2832| profile | [AVRecorderProfile](#avrecorderprofile9) | Yes | Recording profile. This parameter is mandatory. | 2833| url | string | Yes | Recording output URL: fd://xx (fd number).<br><br>This parameter is mandatory.| 2834| rotation | number | No | Rotation angle of the recorded video. The value can only be 0 (default), 90, 180, or 270. | 2835| location | [Location](#location) | No | Geographical location of the recorded video. By default, the geographical location information is not recorded. | 2836 2837## AVRecorderProfile<sup>9+</sup> 2838 2839Describes the audio and video recording profile. 2840 2841**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2842 2843| Name | Type | Mandatory| Description | 2844| ---------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 2845| audioBitrate | number | No | Audio encoding bit rate. This parameter is mandatory for audio recording. The value range is [8000 - 384000].| 2846| audioChannels | number | No | Number of audio channels. This parameter is mandatory for audio recording. The value range is [1 - 2]. | 2847| audioCodec | [CodecMimeType](#codecmimetype8) | No | Audio encoding format. This parameter is mandatory for audio recording. Only **AUDIO_AAC** is supported. | 2848| audioSampleRate | number | No | Audio sampling rate. This parameter is mandatory for audio recording. The value range is [8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 96000].| 2849| fileFormat | [ContainerFormatType](#containerformattype8) | Yes | Container format of a file. This parameter is mandatory. | 2850| videoBitrate | number | No | Video encoding bit rate. This parameter is mandatory for video recording. The value range is [1 - 3000000]. | 2851| videoCodec | [CodecMimeType](#codecmimetype8) | No | Video encoding format. This parameter is mandatory for video recording. Currently, VIDEO_AVC is supported. | 2852| videoFrameWidth | number | No | Width of a video frame. This parameter is mandatory for video recording. The value range is [2 - 1920]. | 2853| videoFrameHeight | number | No | Height of a video frame. This parameter is mandatory for video recording. The value range is [2 - 1080]. | 2854| videoFrameRate | number | No | Video frame rate. This parameter is mandatory for video recording. The value range is [1 - 30]. | 2855 2856## AudioSourceType<sup>9+</sup> 2857 2858Enumerates the audio source types for video recording. 2859 2860**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2861 2862| Name | Value | Description | 2863| ------------------------- | ---- | ---------------------- | 2864| AUDIO_SOURCE_TYPE_DEFAULT | 0 | Default audio input source.| 2865| AUDIO_SOURCE_TYPE_MIC | 1 | Mic audio input source. | 2866 2867## VideoSourceType<sup>9+</sup> 2868 2869Enumerates the video source types for video recording. 2870 2871**System capability**: SystemCapability.Multimedia.Media.AVRecorder 2872 2873| Name | Value | Description | 2874| ----------------------------- | ---- | ------------------------------- | 2875| VIDEO_SOURCE_TYPE_SURFACE_YUV | 0 | The input surface carries raw data.| 2876| VIDEO_SOURCE_TYPE_SURFACE_ES | 1 | The input surface carries ES data. | 2877 2878## ContainerFormatType<sup>8+</sup> 2879 2880Enumerates the container format types (CFTs). 2881 2882**System capability**: SystemCapability.Multimedia.Media.Core 2883 2884| Name | Value | Description | 2885| ----------- | ----- | --------------------- | 2886| CFT_MPEG_4 | 'mp4' | Video container format MP4.| 2887| CFT_MPEG_4A | 'm4a' | Audio container format M4A.| 2888 2889## Location 2890 2891Describes the geographical location of the recorded video. 2892 2893**System capability**: SystemCapability.Multimedia.Media.Core 2894 2895| Name | Type | Mandatory| Description | 2896| --------- | ------ | ---- | ---------------- | 2897| latitude | number | Yes | Latitude of the geographical location.| 2898| longitude | number | Yes | Longitude of the geographical location.| 2899 2900## AVMetadataExtractor<sup>11+</sup> 2901 2902Provides APIs to obtain metadata from media resources. Before calling any API of **AVMetadataExtractor**, you must use [createAVMetadataExtractor()](#mediacreateavmetadataextractor11) to create an **AVMetadataExtractor** instance. 2903 2904For details about the demo for obtaining audio or video metadata, see [Obtaining Audio/Video Metadata](../../media/avmetadataextractor.md). 2905 2906### Attributes 2907 2908**System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor 2909 2910| Name | Type | Readable| Writable| Description | 2911| --------------------------------------------------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 2912| fdSrc<sup>11+</sup> | [AVFileDescriptor](#avfiledescriptor9) | Yes | Yes | Media file descriptor, which specifies the data source. Before obtaining metadata, you must set the data source through either **fdSrc** or **dataSrc**.<br> **Example:**<br>There is a media file that stores continuous assets, the address offset is 0, and the byte length is 100. Its file descriptor is **AVFileDescriptor {fd = resourceHandle; offset = 0; length = 100; }**.| 2913| dataSrc<sup>11+</sup> | [AVDataSrcDescriptor](#avdatasrcdescriptor10) | Yes | Yes | Streaming media resource descriptor, which specifies the data source. Before obtaining metadata, you must set the data source through either **fdSrc** or **dataSrc**.<br> When an application obtains a media file from the remote, you can set **dataSrc** to obtain the metadata before the application finishes the downloading.| 2914 2915### fetchMetadata<sup>11+</sup> 2916 2917fetchMetadata(callback: AsyncCallback\<AVMetadata>): void 2918 2919Obtains media metadata. This API uses an asynchronous callback to return the result. 2920 2921**System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor 2922 2923**Parameters** 2924 2925| Name | Type | Mandatory| Description | 2926| -------- | -------------------------------------------- | ---- | ----------------------------------- | 2927| callback | AsyncCallback\<[AVMetadata](#avmetadata11)> | Yes | Callback used to return the result, which is an **AVMetadata** instance.| 2928 2929**Error codes** 2930 2931For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2932 2933| ID| Error Message | 2934| -------- | ------------------------------------------ | 2935| 5400102 | Operation not allowed. Returned by callback. | 2936| 5400106 | Unsupported format. Returned by callback. | 2937 2938**Example** 2939 2940```ts 2941// Obtain the metadata. 2942avMetadataExtractor.fetchMetadata((error: BusinessError, metadata) => { 2943 if (error) { 2944 console.error(`fetchMetadata callback failed, err = ${JSON.stringify(error)}`); 2945 return; 2946 } 2947 console.info(`fetchMetadata callback success, genre: ${metadata.genre}`); 2948}) 2949``` 2950 2951### fetchMetadata<sup>11+</sup> 2952 2953fetchMetadata(): Promise\<AVMetadata> 2954 2955Obtains media metadata. This API uses a promise to return the result. 2956 2957**System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor 2958 2959**Return value** 2960 2961| Type | Description | 2962| -------------- | ---------------------------------------- | 2963| Promise\<[AVMetadata](#avmetadata11)> | Promise used to return the result, which is an **AVMetadata** instance.| 2964 2965**Error codes** 2966 2967For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 2968 2969| ID| Error Message | 2970| -------- | ----------------------------------------- | 2971| 5400102 | Operation not allowed. Returned by promise. | 2972| 5400106 | Unsupported format. Returned by promise. | 2973 2974**Example** 2975 2976```ts 2977// Obtain the metadata. 2978avMetadataExtractor.fetchMetadata().then((metadata: media.AVMetadata) => { 2979 console.info(`fetchMetadata callback success, genre: ${metadata.genre}`) 2980}).catch((error: BusinessError) => { 2981 console.error(`fetchMetadata catchCallback, error message:${error.message}`); 2982}); 2983``` 2984 2985### fetchAlbumCover<sup>11+</sup> 2986 2987fetchAlbumCover(callback: AsyncCallback\<image.PixelMap>): void 2988 2989Obtains the cover of the audio album. This API uses an asynchronous callback to return the result. 2990 2991**System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor 2992 2993**Parameters** 2994 2995| Name | Type | Mandatory| Description | 2996| -------- | -------------------------------------------- | ---- | ----------------------------------- | 2997| callback | AsyncCallback\<[image.PixelMap](js-apis-image.md#pixelmap7)> | Yes | Callback used to return the album cover.| 2998 2999**Error codes** 3000 3001For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3002 3003| ID| Error Message | 3004| -------- | ------------------------------------------ | 3005| 5400102 | Operation not allowed. Return by callback. | 3006| 5400106 | Unsupported format. Returned by callback. | 3007 3008**Example** 3009 3010```ts 3011import image from '@ohos.multimedia.image'; 3012let pixel_map : image.PixelMap | undefined = undefined; 3013 3014// Obtain the album cover. 3015avMetadataExtractor.fetchAlbumCover((error: BusinessError, pixelMap) => { 3016 if (error) { 3017 console.error(`fetchAlbumCover callback failed, error = ${JSON.stringify(error)}`); 3018 return; 3019 } 3020 pixel_map = pixelMap; 3021}); 3022``` 3023 3024### fetchAlbumCover<sup>11+</sup> 3025 3026fetchAlbumCover(): Promise\<image.PixelMap> 3027 3028Obtains the cover of the audio album. This API uses a promise to return the result. 3029 3030**System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor 3031 3032**Return value** 3033 3034| Type | Description | 3035| -------------- | ---------------------------------------- | 3036| Promise\<[image.PixelMap](js-apis-image.md#pixelmap7)> | Promise used to return the album cover.| 3037 3038**Error codes** 3039 3040For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3041 3042| ID| Error Message | 3043| -------- | ----------------------------------------- | 3044| 5400102 | Operation not allowed. Returned by promise. | 3045| 5400106 | Unsupported format. Returned by promise. | 3046 3047**Example** 3048 3049```ts 3050import image from '@ohos.multimedia.image'; 3051let pixel_map : image.PixelMap | undefined = undefined; 3052 3053// Obtain the album cover. 3054avMetadataExtractor.fetchAlbumCover().then((pixelMap: image.PixelMap) => { 3055 pixel_map = pixelMap; 3056}).catch((error: BusinessError) => { 3057 console.error(`fetchAlbumCover catchCallback, error message:${error.message}`); 3058}); 3059``` 3060 3061### release<sup>11+</sup> 3062 3063release(callback: AsyncCallback\<void>): void 3064 3065Releases this **AVMetadataExtractor** instance. This API uses an asynchronous callback to return the result. 3066 3067**System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor 3068 3069**Parameters** 3070 3071| Name | Type | Mandatory| Description | 3072| -------- | -------------------------------------------- | ---- | ----------------------------------- | 3073| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3074 3075**Error codes** 3076 3077For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3078 3079| ID| Error Message | 3080| -------- | ------------------------------------------ | 3081| 5400102 | Operation not allowed. Returned by callback. | 3082 3083**Example** 3084 3085```ts 3086// Release the instance. 3087avMetadataExtractor.release((error: BusinessError) => { 3088 if (error) { 3089 console.error(`release failed, err = ${JSON.stringify(error)}`); 3090 return; 3091 } 3092 console.info(`release success.`); 3093}) 3094``` 3095 3096### release<sup>11+</sup> 3097 3098release(): Promise\<void> 3099 3100Releases this **AVMetadataExtractor** instance. This API uses a promise to return the result. 3101 3102**System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor 3103 3104**Return value** 3105 3106| Type | Description | 3107| -------------- | ---------------------------------------- | 3108| Promise\<void> | Promise used to return the result.| 3109 3110**Error codes** 3111 3112For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3113 3114| ID| Error Message | 3115| -------- | ----------------------------------------- | 3116| 5400102 | Operation not allowed. Returned by promise. | 3117 3118**Example** 3119 3120```ts 3121// Release the instance. 3122avMetadataExtractor.release().then(() => { 3123 console.info(`release success.`); 3124}).catch((error: BusinessError) => { 3125 console.error(`release catchCallback, error message:${error.message}`); 3126}); 3127``` 3128 3129## AVMetadata<sup>11+</sup> 3130 3131Defines the audio and video metadata. 3132 3133**System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor 3134 3135| Name | Type | Mandatory| Description | 3136| ------ | ------ | ---- | ------------------------------------------------------------ | 3137| album | string | No | Title of the album. | 3138| albumArtist | string | No | Artist of the album.| 3139| artist | string | No | Artist of the media asset.| 3140| author | string | No | Author of the media asset.| 3141| dateTime | string | No | Time when the media asset is created.| 3142| dateTimeFormat | string | No | Time when the media asset is created. The value is in the YYYY-MM-DD HH:mm:ss format.| 3143| composer | string | No | Composer of the media asset.| 3144| duration | string | No | Duration of the media asset.| 3145| genre | string | No | Type or genre of the media asset.| 3146| hasAudio | string | No | Whether the media asset contains audio.| 3147| hasVideo | string | No | Whether the media asset contains a video.| 3148| mimeType | string | No | MIME type of the media asset.| 3149| trackCount | string | No | Number of tracks of the media asset.| 3150| sampleRate | string | No | Audio sampling rate, in Hz.| 3151| title | string | No | Title of the media asset.| 3152| videoHeight | string | No | Video height, in pixels.| 3153| videoWidth | string | No | Video width, in pixels.| 3154| videoOrientation | string | No | Video rotation direction, in degrees.| 3155 3156## AVImageGenerator<sup>11+</sup> 3157 3158Provides APIs to obtain a thumbnail from a video. Before calling any API of **AVImageGenerator**, you must use [createAVImageGenerator()](#mediacreateavimagegenerator11) to create an **AVImageGenerator** instance. 3159 3160For details about the demo for obtaining video thumbnails, see [Obtaining Video Thumbnails](../../media/avimagegenerator.md). 3161 3162### Attributes 3163 3164**System capability**: SystemCapability.Multimedia.Media.AVImageGenerator 3165 3166**System API**: This is a system API. 3167 3168| Name | Type | Readable| Writable| Description | 3169| --------------------------------------------------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 3170| fdSrc<sup>11+</sup> | [AVFileDescriptor](#avfiledescriptor9) | Yes | Yes | Media file descriptor, which specifies the data source.<br> **Example:**<br>There is a media file that stores continuous assets, the address offset is 0, and the byte length is 100. Its file descriptor is **AVFileDescriptor {fd = resourceHandle; offset = 0; length = 100; }**.| 3171 3172### fetchFrameByTime<sup>11+</sup> 3173 3174fetchFrameByTime(timeUs: number, options: AVImageQueryOptions, param: PixelMapParams, callback: AsyncCallback\<image.PixelMap>): void 3175 3176Obtains a video thumbnail. This API uses an asynchronous callback to return the result. 3177 3178**System capability**: SystemCapability.Multimedia.Media.AVImageGenerator 3179 3180**System API**: This is a system API. 3181 3182**Parameters** 3183 3184| Name | Type | Mandatory| Description | 3185| -------- | -------------------------------------------- | ---- | ----------------------------------- | 3186| timeUs | number | Yes | Time of the video for which a thumbnail is to be obtained, in μs.| 3187| options | [AVImageQueryOptions](#avimagequeryoptions11) | Yes | Relationship between the time passed in and the video frame.| 3188| param | [PixelMapParams](#pixelmapparams11) | Yes | Format parameters of the thumbnail to be obtained.| 3189| callback | AsyncCallback\<[image.PixelMap](js-apis-image.md#pixelmap7)> | Yes | Callback used to return the video thumbnail.| 3190 3191**Error codes** 3192 3193For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3194 3195| ID| Error Message | 3196| -------- | ------------------------------------------ | 3197| 5400102 | Operation not allowed. Returned by callback. | 3198| 5400106 | Unsupported format. Returned by callback. | 3199 3200**Example** 3201 3202```ts 3203import image from '@ohos.multimedia.image'; 3204 3205let pixel_map : image.PixelMap | undefined = undefined; 3206 3207// Initialize input parameters. 3208let timeUs: number = 0 3209 3210let queryOption: media.AVImageQueryOptions = media.AVImageQueryOptions.AV_IMAGE_QUERY_NEXT_SYNC 3211 3212let param: media.PixelMapParams = { 3213 width : 300, 3214 height : 300, 3215 colorFormat : media.PixelFormat.RGB_565 3216} 3217 3218// Obtain the thumbnail. 3219avImageGenerator.fetchFrameByTime(timeUs, queryOption, param, (error: BusinessError, pixelMap) => { 3220 if (error) { 3221 console.error(`fetchFrameByTime callback failed, err = ${JSON.stringify(error)}`) 3222 return 3223 } 3224 pixel_map = pixelMap; 3225}) 3226``` 3227 3228### fetchFrameByTime<sup>11+</sup> 3229 3230fetchFrameByTime(timeUs: number, options: AVImageQueryOptions, param: PixelMapParams): Promise<image.PixelMap> 3231 3232Obtains a video thumbnail. This API uses a promise to return the result. 3233 3234**System capability**: SystemCapability.Multimedia.Media.AVImageGenerator 3235 3236**System API**: This is a system API. 3237 3238**Parameters** 3239 3240| Name | Type | Mandatory| Description | 3241| -------- | -------------------------------------------- | ---- | ----------------------------------- | 3242| timeUs | number | Yes | Time of the video for which a thumbnail is to be obtained, in μs.| 3243| options | [AVImageQueryOptions](#avimagequeryoptions11) | Yes | Relationship between the time passed in and the video frame.| 3244| param | [PixelMapParams](#pixelmapparams11) | Yes | Format parameters of the thumbnail to be obtained.| 3245 3246**Return value** 3247 3248| Type | Description | 3249| -------------- | ---------------------------------------- | 3250| Promise\<[image.PixelMap](js-apis-image.md#pixelmap7)> | Promise used to return the video thumbnail.| 3251 3252**Error codes** 3253 3254For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3255 3256| ID| Error Message | 3257| -------- | ----------------------------------------- | 3258| 5400102 | Operation not allowed. Returned by promise. | 3259| 5400106 | Unsupported format. Returned by promise. | 3260 3261**Example** 3262 3263```ts 3264import image from '@ohos.multimedia.image'; 3265 3266let pixel_map : image.PixelMap | undefined = undefined; 3267 3268// Initialize input parameters. 3269let timeUs: number = 0 3270 3271let queryOption: media.AVImageQueryOptions = media.AVImageQueryOptions.AV_IMAGE_QUERY_NEXT_SYNC 3272 3273let param: media.PixelMapParams = { 3274 width : 300, 3275 height : 300, 3276 colorFormat : media.PixelFormat.RGB_565 3277} 3278 3279// Obtain the thumbnail. 3280avImageGenerator.fetchFrameByTime(timeUs, queryOption, param).then((pixelMap: image.PixelMap) => { 3281 pixel_map = pixelMap; 3282}).catch((error: BusinessError) => { 3283 console.error(`fetchFrameByTime catchCallback, error message:${error.message}`); 3284}); 3285``` 3286 3287### release<sup>11+</sup> 3288 3289release(callback: AsyncCallback\<void>): void 3290 3291Releases this **AVMetadataExtractor** instance. This API uses an asynchronous callback to return the result. 3292 3293**System capability**: SystemCapability.Multimedia.Media.AVImageGenerator 3294 3295**System API**: This is a system API. 3296 3297**Parameters** 3298 3299| Name | Type | Mandatory| Description | 3300| -------- | -------------------------------------------- | ---- | ----------------------------------- | 3301| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3302 3303**Error codes** 3304 3305For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3306 3307| ID| Error Message | 3308| -------- | ------------------------------------------ | 3309| 5400102 | Operation not allowed. Returned by callback. | 3310 3311**Example** 3312 3313```ts 3314// Release the instance. 3315avImageGenerator.release((error: BusinessError) => { 3316 if (error) { 3317 console.error(`release failed, err = ${JSON.stringify(error)}`); 3318 return; 3319 } 3320 console.info(`release success.`); 3321}) 3322``` 3323 3324### release<sup>11+</sup> 3325 3326release(): Promise\<void> 3327 3328Releases this **AVMetadataExtractor** instance. This API uses a promise to return the result. 3329 3330**System capability**: SystemCapability.Multimedia.Media.AVImageGenerator 3331 3332**System API**: This is a system API. 3333 3334**Return value** 3335 3336| Type | Description | 3337| -------------- | ---------------------------------------- | 3338| Promise\<void> | Promise used to return the result.| 3339 3340**Error codes** 3341 3342For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3343 3344| ID| Error Message | 3345| -------- | ----------------------------------------- | 3346| 5400102 | Operation not allowed. Returned by promise. | 3347 3348**Example** 3349 3350```ts 3351// Release the instance. 3352avImageGenerator.release().then(() => { 3353 console.info(`release success.`); 3354}).catch((error: BusinessError) => { 3355 console.error(`release catchCallback, error message:${error.message}`); 3356}); 3357``` 3358 3359## AVImageQueryOptions<sup>11+</sup> 3360 3361Enumerates the relationship between the video frame and the time at which the video thumbnail is obtained. 3362 3363The time passed in for obtaining the thumbnail may be different from the time of the video frame for which the thumbnail is actually obtained. Therefore, you need to specify their relationship. 3364 3365**System capability**: SystemCapability.Multimedia.Media.AVImageGenerator 3366 3367**System API**: This is a system API. 3368 3369| Name | Value | Description | 3370| ------------------------ | --------------- | ------------------------------------------------------------ | 3371| AV_IMAGE_QUERY_NEXT_SYNC | 0 | The key frame at or next to the specified time is selected. | 3372| AV_IMAGE_QUERY_PREVIOUS_SYNC | 1 | The key frame at or prior to the specified time is selected.| 3373| AV_IMAGE_QUERY_CLOSEST_SYNC | 2 | The key frame closest to the specified time is selected. | 3374| AV_IMAGE_QUERY_CLOSEST | 3 | The frame (not necessarily a key frame) closest to the specified time is selected. | 3375 3376## PixelMapParams<sup>11+</sup> 3377 3378Defines the format parameters of the video thumbnail to be obtained. 3379 3380**System capability**: SystemCapability.Multimedia.Media.AVImageGenerator 3381 3382**System API**: This is a system API. 3383 3384| Name | Type | Readable | Writable | Description | 3385| -------- | ------ | ------| ------ | ---------------------- | 3386| width | number | Yes | Yes | Width of the thumbnail. | 3387| height | number | Yes | Yes | Height of the thumbnail.| 3388| colorFormat | [PixelFormat](#pixelformat11) | Yes | Yes | Color format of the thumbnail. | 3389 3390## PixelFormat<sup>11+</sup> 3391 3392Enumerates the color formats supported by the video thumbnail. 3393 3394**System capability**: SystemCapability.Multimedia.Media.AVImageGenerator 3395 3396**System API**: This is a system API. 3397 3398| Name | Value | Description | 3399| ------------------------ | --------------- | ------------------------------------------------------------ | 3400| RGB_565 | 2 | RGB_565. | 3401| RGBA_8888 | 3 | RGBA_8888.| 3402| RGB_888 | 5 | RGB_888. | 3403 3404## VideoRecorder<sup>9+</sup> 3405 3406> **NOTE** 3407> 3408> This class is deprecated after AVRecorder<sup>9+</sup> is released. You are advised to use [AVRecorder](#avrecorder9) instead. 3409 3410Implements video recording. Before calling any API in the **VideoRecorder** class, you must use [createVideoRecorder()](#mediacreatevideorecorder9) to create a [VideoRecorder](#videorecorder9) instance. 3411 3412### Attributes 3413 3414**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3415 3416**System API**: This is a system API. 3417 3418| Name | Type | Readable| Writable| Description | 3419| ------------------ | -------------------------------------- | ---- | ---- | ---------------- | 3420| state<sup>9+</sup> | [VideoRecordState](#videorecordstate9) | Yes | No | Video recording state.| 3421 3422### prepare<sup>9+</sup> 3423 3424prepare(config: VideoRecorderConfig, callback: AsyncCallback\<void>): void 3425 3426Sets video recording parameters. This API uses an asynchronous callback to return the result. 3427 3428**Required permissions:** ohos.permission.MICROPHONE 3429 3430**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3431 3432**System API**: This is a system API. 3433 3434**Parameters** 3435 3436| Name | Type | Mandatory| Description | 3437| -------- | -------------------------------------------- | ---- | ----------------------------------- | 3438| config | [VideoRecorderConfig](#videorecorderconfig9) | Yes | Video recording parameters to set. | 3439| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3440 3441**Error codes** 3442 3443For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3444 3445| ID| Error Message | 3446| -------- | ------------------------------------------ | 3447| 201 | Permission denied. Return by callback. | 3448| 401 | Parameter error. Return by callback. | 3449| 5400102 | Operation not allowed. Return by callback. | 3450| 5400105 | Service died. Return by callback. | 3451 3452**Example** 3453 3454```ts 3455import { BusinessError } from '@ohos.base'; 3456 3457// Configure the parameters based on those supported by the hardware device. 3458let videoProfile: media.VideoRecorderProfile = { 3459 audioBitrate : 48000, 3460 audioChannels : 2, 3461 audioCodec : media.CodecMimeType.AUDIO_AAC, 3462 audioSampleRate : 48000, 3463 fileFormat : media.ContainerFormatType.CFT_MPEG_4, 3464 videoBitrate : 2000000, 3465 videoCodec : media.CodecMimeType.VIDEO_AVC, 3466 videoFrameWidth : 640, 3467 videoFrameHeight : 480, 3468 videoFrameRate : 30 3469} 3470 3471let videoConfig: media.VideoRecorderConfig = { 3472 audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, 3473 videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 3474 profile : videoProfile, 3475 url : 'fd://xx', // The file must be created by the caller and granted with proper permissions. 3476 rotation : 0, 3477 location : { latitude : 30, longitude : 130 }, 3478} 3479 3480// asyncallback 3481videoRecorder.prepare(videoConfig, (err: BusinessError) => { 3482 if (err == null) { 3483 console.info('prepare success'); 3484 } else { 3485 console.error('prepare failed and error is ' + err.message); 3486 } 3487}) 3488``` 3489 3490### prepare<sup>9+</sup> 3491 3492prepare(config: VideoRecorderConfig): Promise\<void> 3493 3494Sets video recording parameters. This API uses a promise to return the result. 3495 3496**Required permissions:** ohos.permission.MICROPHONE 3497 3498**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3499 3500**System API**: This is a system API. 3501 3502**Parameters** 3503 3504| Name| Type | Mandatory| Description | 3505| ------ | -------------------------------------------- | ---- | ------------------------ | 3506| config | [VideoRecorderConfig](#videorecorderconfig9) | Yes | Video recording parameters to set.| 3507 3508**Return value** 3509 3510| Type | Description | 3511| -------------- | ---------------------------------------- | 3512| Promise\<void> | Promise used to return the result.| 3513 3514**Error codes** 3515 3516For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3517 3518| ID| Error Message | 3519| -------- | ----------------------------------------- | 3520| 201 | Permission denied. Return by promise. | 3521| 401 | Parameter error. Return by promise. | 3522| 5400102 | Operation not allowed. Return by promise. | 3523| 5400105 | Service died. Return by promise. | 3524 3525**Example** 3526 3527```ts 3528// Configure the parameters based on those supported by the hardware device. 3529let videoProfile: media.VideoRecorderProfile = { 3530 audioBitrate : 48000, 3531 audioChannels : 2, 3532 audioCodec : media.CodecMimeType.AUDIO_AAC, 3533 audioSampleRate : 48000, 3534 fileFormat : media.ContainerFormatType.CFT_MPEG_4, 3535 videoBitrate : 2000000, 3536 videoCodec : media.CodecMimeType.VIDEO_AVC, 3537 videoFrameWidth : 640, 3538 videoFrameHeight : 480, 3539 videoFrameRate : 30 3540} 3541 3542let videoConfig: media.VideoRecorderConfig = { 3543 audioSourceType : media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, 3544 videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 3545 profile : videoProfile, 3546 url : 'fd://xx', // The file must be created by the caller and granted with proper permissions. 3547 rotation : 0, 3548 location : { latitude : 30, longitude : 130 }, 3549} 3550 3551// promise 3552videoRecorder.prepare(videoConfig).then(() => { 3553 console.info('prepare success'); 3554}).catch((err: BusinessError) => { 3555 console.error('prepare failed and catch error is ' + err.message); 3556}); 3557``` 3558 3559### getInputSurface<sup>9+</sup> 3560 3561getInputSurface(callback: AsyncCallback\<string>): void 3562 3563Obtains 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. 3564 3565Note 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. 3566 3567This API can be called only after prepare() is called. 3568 3569**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3570 3571**System API**: This is a system API. 3572 3573**Parameters** 3574 3575| Name | Type | Mandatory| Description | 3576| -------- | ---------------------- | ---- | --------------------------- | 3577| callback | AsyncCallback\<string> | Yes | Callback used to obtain the result.| 3578 3579**Error codes** 3580 3581For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3582 3583| ID| Error Message | 3584| -------- | ------------------------------------------ | 3585| 5400102 | Operation not allowed. Return by callback. | 3586| 5400103 | I/O error. Return by callback. | 3587| 5400105 | Service died. Return by callback. | 3588 3589**Example** 3590 3591```ts 3592import { BusinessError } from '@ohos.base'; 3593 3594// asyncallback 3595let surfaceID: string; // Surface ID passed to the external system. 3596videoRecorder.getInputSurface((err: BusinessError, surfaceId: string) => { 3597 if (err == null) { 3598 console.info('getInputSurface success'); 3599 surfaceID = surfaceId; 3600 } else { 3601 console.error('getInputSurface failed and error is ' + err.message); 3602 } 3603}); 3604``` 3605 3606### getInputSurface<sup>9+</sup> 3607 3608getInputSurface(): Promise\<string>; 3609 3610 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. 3611 3612Note 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. 3613 3614This API can be called only after prepare() is called. 3615 3616**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3617 3618**System API**: This is a system API. 3619 3620**Return value** 3621 3622| Type | Description | 3623| ---------------- | -------------------------------- | 3624| Promise\<string> | Promise used to return the result.| 3625 3626**Error codes** 3627 3628For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3629 3630| ID| Error Message | 3631| -------- | ----------------------------------------- | 3632| 5400102 | Operation not allowed. Return by promise. | 3633| 5400103 | I/O error. Return by promise. | 3634| 5400105 | Service died. Return by promise. | 3635 3636**Example** 3637 3638```ts 3639// promise 3640let surfaceID: string; // Surface ID passed to the external system. 3641videoRecorder.getInputSurface().then((surfaceId: string) => { 3642 console.info('getInputSurface success'); 3643 surfaceID = surfaceId; 3644}).catch((err: BusinessError) => { 3645 console.error('getInputSurface failed and catch error is ' + err.message); 3646}); 3647``` 3648 3649### start<sup>9+</sup> 3650 3651start(callback: AsyncCallback\<void>): void 3652 3653Starts recording. This API uses an asynchronous callback to return the result. 3654 3655This API can be called only after prepare() and getInputSurface() are called, because the data source must pass data to the surface first. 3656 3657**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3658 3659**System API**: This is a system API. 3660 3661**Parameters** 3662 3663| Name | Type | Mandatory| Description | 3664| -------- | -------------------- | ---- | ---------------------------- | 3665| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3666 3667**Error codes** 3668 3669For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3670 3671| ID| Error Message | 3672| -------- | ------------------------------------------ | 3673| 5400102 | Operation not allowed. Return by callback. | 3674| 5400103 | I/O error. Return by callback. | 3675| 5400105 | Service died. Return by callback. | 3676 3677**Example** 3678 3679```ts 3680// asyncallback 3681videoRecorder.start((err: BusinessError) => { 3682 if (err == null) { 3683 console.info('start videorecorder success'); 3684 } else { 3685 console.error('start videorecorder failed and error is ' + err.message); 3686 } 3687}); 3688``` 3689 3690### start<sup>9+</sup> 3691 3692start(): Promise\<void> 3693 3694Starts recording. This API uses a promise to return the result. 3695 3696This API can be called only after prepare() and getInputSurface() are called, because the data source must pass data to the surface first. 3697 3698**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3699 3700**System API**: This is a system API. 3701 3702**Return value** 3703 3704| Type | Description | 3705| -------------- | ------------------------------------- | 3706| Promise\<void> | Promise used to return the result.| 3707 3708**Error codes** 3709 3710For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3711 3712| ID| Error Message | 3713| -------- | ----------------------------------------- | 3714| 5400102 | Operation not allowed. Return by promise. | 3715| 5400103 | I/O error. Return by promise. | 3716| 5400105 | Service died. Return by promise. | 3717 3718**Example** 3719 3720```ts 3721// promise 3722videoRecorder.start().then(() => { 3723 console.info('start videorecorder success'); 3724}).catch((err: BusinessError) => { 3725 console.error('start videorecorder failed and catch error is ' + err.message); 3726}); 3727``` 3728 3729### pause<sup>9+</sup> 3730 3731pause(callback: AsyncCallback\<void>): void 3732 3733Pauses recording. This API uses an asynchronous callback to return the result. 3734 3735This API can be called only after start() is called. You can resume recording by calling resume(). 3736 3737**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3738 3739**System API**: This is a system API. 3740 3741**Parameters** 3742 3743| Name | Type | Mandatory| Description | 3744| -------- | -------------------- | ---- | ---------------------------- | 3745| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3746 3747**Error codes** 3748 3749For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3750 3751| ID| Error Message | 3752| -------- | ------------------------------------------ | 3753| 5400102 | Operation not allowed. Return by callback. | 3754| 5400103 | I/O error. Return by callback. | 3755| 5400105 | Service died. Return by callback. | 3756 3757**Example** 3758 3759```ts 3760// asyncallback 3761videoRecorder.pause((err: BusinessError) => { 3762 if (err == null) { 3763 console.info('pause videorecorder success'); 3764 } else { 3765 console.error('pause videorecorder failed and error is ' + err.message); 3766 } 3767}); 3768``` 3769 3770### pause<sup>9+</sup> 3771 3772pause(): Promise\<void> 3773 3774Pauses recording. This API uses a promise to return the result. 3775 3776This API can be called only after start() is called. You can resume recording by calling resume(). 3777 3778**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3779 3780**System API**: This is a system API. 3781 3782**Return value** 3783 3784| Type | Description | 3785| -------------- | ------------------------------------- | 3786| Promise\<void> | Promise used to return the result.| 3787 3788**Error codes** 3789 3790For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3791 3792| ID| Error Message | 3793| -------- | ----------------------------------------- | 3794| 5400102 | Operation not allowed. Return by promise. | 3795| 5400103 | I/O error. Return by promise. | 3796| 5400105 | Service died. Return by promise. | 3797 3798**Example** 3799 3800```ts 3801// promise 3802videoRecorder.pause().then(() => { 3803 console.info('pause videorecorder success'); 3804}).catch((err: BusinessError) => { 3805 console.error('pause videorecorder failed and catch error is ' + err.message); 3806}); 3807``` 3808 3809### resume<sup>9+</sup> 3810 3811resume(callback: AsyncCallback\<void>): void 3812 3813Resumes recording. This API uses an asynchronous callback to return the result. 3814 3815**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3816 3817**System API**: This is a system API. 3818 3819**Parameters** 3820 3821| Name | Type | Mandatory| Description | 3822| -------- | -------------------- | ---- | ---------------------------- | 3823| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3824 3825**Error codes** 3826 3827For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3828 3829| ID| Error Message | 3830| -------- | ------------------------------------------ | 3831| 5400102 | Operation not allowed. Return by callback. | 3832| 5400103 | I/O error. Return by callback. | 3833| 5400105 | Service died. Return by callback. | 3834 3835**Example** 3836 3837```ts 3838// asyncallback 3839videoRecorder.resume((err: BusinessError) => { 3840 if (err == null) { 3841 console.info('resume videorecorder success'); 3842 } else { 3843 console.error('resume videorecorder failed and error is ' + err.message); 3844 } 3845}); 3846``` 3847 3848### resume<sup>9+</sup> 3849 3850resume(): Promise\<void> 3851 3852Resumes recording. This API uses a promise to return the result. 3853 3854**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3855 3856**System API**: This is a system API. 3857 3858**Return value** 3859 3860| Type | Description | 3861| -------------- | ------------------------------------- | 3862| Promise\<void> | Promise used to return the result.| 3863 3864**Error codes** 3865 3866For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3867 3868| ID| Error Message | 3869| -------- | ----------------------------------------- | 3870| 5400102 | Operation not allowed. Return by promise. | 3871| 5400103 | I/O error. Return by promise. | 3872| 5400105 | Service died. Return by promise. | 3873 3874**Example** 3875 3876```ts 3877// promise 3878videoRecorder.resume().then(() => { 3879 console.info('resume videorecorder success'); 3880}).catch((err: BusinessError) => { 3881 console.error('resume videorecorder failed and catch error is ' + err.message); 3882}); 3883``` 3884 3885### stop<sup>9+</sup> 3886 3887stop(callback: AsyncCallback\<void>): void 3888 3889Stops recording. This API uses an asynchronous callback to return the result. 3890 3891To start another recording, you must call prepare() and getInputSurface() again. 3892 3893**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3894 3895**System API**: This is a system API. 3896 3897**Parameters** 3898 3899| Name | Type | Mandatory| Description | 3900| -------- | -------------------- | ---- | ---------------------------- | 3901| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3902 3903**Error codes** 3904 3905For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3906 3907| ID| Error Message | 3908| -------- | ------------------------------------------ | 3909| 5400102 | Operation not allowed. Return by callback. | 3910| 5400103 | I/O error. Return by callback. | 3911| 5400105 | Service died. Return by callback. | 3912 3913**Example** 3914 3915```ts 3916// asyncallback 3917videoRecorder.stop((err: BusinessError) => { 3918 if (err == null) { 3919 console.info('stop videorecorder success'); 3920 } else { 3921 console.error('stop videorecorder failed and error is ' + err.message); 3922 } 3923}); 3924``` 3925 3926### stop<sup>9+</sup> 3927 3928stop(): Promise\<void> 3929 3930Stops recording. This API uses a promise to return the result. 3931 3932To start another recording, you must call prepare() and getInputSurface() again. 3933 3934**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3935 3936**System API**: This is a system API. 3937 3938**Return value** 3939 3940| Type | Description | 3941| -------------- | ------------------------------------- | 3942| Promise\<void> | Promise used to return the result.| 3943 3944**Error codes** 3945 3946For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3947 3948| ID| Error Message | 3949| -------- | ----------------------------------------- | 3950| 5400102 | Operation not allowed. Return by promise. | 3951| 5400103 | I/O error. Return by promise. | 3952| 5400105 | Service died. Return by promise. | 3953 3954**Example** 3955 3956```ts 3957// promise 3958videoRecorder.stop().then(() => { 3959 console.info('stop videorecorder success'); 3960}).catch((err: BusinessError) => { 3961 console.error('stop videorecorder failed and catch error is ' + err.message); 3962}); 3963``` 3964 3965### release<sup>9+</sup> 3966 3967release(callback: AsyncCallback\<void>): void 3968 3969Releases the video recording resources. This API uses an asynchronous callback to return the result. 3970 3971**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 3972 3973**System API**: This is a system API. 3974 3975**Parameters** 3976 3977| Name | Type | Mandatory| Description | 3978| -------- | -------------------- | ---- | -------------------------------- | 3979| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 3980 3981**Error codes** 3982 3983For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 3984 3985| ID| Error Message | 3986| -------- | --------------------------------- | 3987| 5400105 | Service died. Return by callback. | 3988 3989**Example** 3990 3991```ts 3992// asyncallback 3993videoRecorder.release((err: BusinessError) => { 3994 if (err == null) { 3995 console.info('release videorecorder success'); 3996 } else { 3997 console.error('release videorecorder failed and error is ' + err.message); 3998 } 3999}); 4000``` 4001 4002### release<sup>9+</sup> 4003 4004release(): Promise\<void> 4005 4006Releases the video recording resources. This API uses a promise to return the result. 4007 4008**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 4009 4010**System API**: This is a system API. 4011 4012**Return value** 4013 4014| Type | Description | 4015| -------------- | ----------------------------------------- | 4016| Promise\<void> | Promise used to return the result.| 4017 4018**Error codes** 4019 4020For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 4021 4022| ID| Error Message | 4023| -------- | --------------------------------- | 4024| 5400105 | Service died. Return by callback. | 4025 4026**Example** 4027 4028```ts 4029// promise 4030videoRecorder.release().then(() => { 4031 console.info('release videorecorder success'); 4032}).catch((err: BusinessError) => { 4033 console.error('release videorecorder failed and catch error is ' + err.message); 4034}); 4035``` 4036 4037### reset<sup>9+</sup> 4038 4039reset(callback: AsyncCallback\<void>): void 4040 4041Resets video recording. This API uses an asynchronous callback to return the result. 4042 4043To start another recording, you must call prepare() and getInputSurface() again. 4044 4045**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 4046 4047**System API**: This is a system API. 4048 4049**Parameters** 4050 4051| Name | Type | Mandatory| Description | 4052| -------- | -------------------- | ---- | ---------------------------- | 4053| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4054 4055**Error codes** 4056 4057For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 4058 4059| ID| Error Message | 4060| -------- | --------------------------------- | 4061| 5400103 | I/O error. Return by callback. | 4062| 5400105 | Service died. Return by callback. | 4063 4064**Example** 4065 4066```ts 4067// asyncallback 4068videoRecorder.reset((err: BusinessError) => { 4069 if (err == null) { 4070 console.info('reset videorecorder success'); 4071 } else { 4072 console.error('reset videorecorder failed and error is ' + err.message); 4073 } 4074}); 4075``` 4076 4077### reset<sup>9+</sup> 4078 4079reset(): Promise\<void> 4080 4081Resets video recording. This API uses a promise to return the result. 4082 4083To start another recording, you must call prepare() and getInputSurface() again. 4084 4085**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 4086 4087**System API**: This is a system API. 4088 4089**Return value** 4090 4091| Type | Description | 4092| -------------- | ------------------------------------- | 4093| Promise\<void> | Promise used to return the result.| 4094 4095**Error codes** 4096 4097For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 4098 4099| ID| Error Message | 4100| -------- | -------------------------------- | 4101| 5400103 | I/O error. Return by promise. | 4102| 5400105 | Service died. Return by promise. | 4103 4104**Example** 4105 4106```ts 4107// promise 4108videoRecorder.reset().then(() => { 4109 console.info('reset videorecorder success'); 4110}).catch((err: BusinessError) => { 4111 console.error('reset videorecorder failed and catch error is ' + err.message); 4112}); 4113``` 4114 4115### on('error')<sup>9+</sup> 4116 4117on(type: 'error', callback: ErrorCallback): void 4118 4119Subscribes to video recording error events. After an error event is reported, you must handle the event and exit the recording. 4120 4121**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 4122 4123**System API**: This is a system API. 4124 4125**Parameters** 4126 4127| Name | Type | Mandatory| Description | 4128| -------- | ------------- | ---- | ------------------------------------------------------------ | 4129| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during video recording.| 4130| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 4131 4132**Error codes** 4133 4134For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 4135 4136| ID| Error Message | 4137| -------- | --------------------------------- | 4138| 5400103 | I/O error. Return by callback. | 4139| 5400105 | Service died. Return by callback. | 4140 4141**Example** 4142 4143```ts 4144// This event is reported when an error occurs during the retrieval of videoRecordState. 4145videoRecorder.on('error', (error: BusinessError) => { // Set the 'error' event callback. 4146 console.error(`audio error called, error: ${error}`); 4147}) 4148``` 4149 4150## VideoRecordState<sup>9+</sup> 4151 4152Enumerates the video recording states. You can obtain the state through the **state** attribute. 4153 4154**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 4155 4156**System API**: This is a system API. 4157 4158| Name | Type | Description | 4159| -------- | ------ | ---------------------- | 4160| idle | string | The video recorder is idle. | 4161| prepared | string | The video recording parameters are set.| 4162| playing | string | Video recording is in progress. | 4163| paused | string | Video recording is paused. | 4164| stopped | string | Video recording is stopped. | 4165| error | string | Video recording is in the error state. | 4166 4167## VideoRecorderConfig<sup>9+</sup> 4168 4169Describes the video recording parameters. 4170 4171The **audioSourceType** and **videoSourceType** parameters are used to distinguish video-only recording from audio and video recording. (For audio-only recording, use **[AVRecorder](#avrecorder9)** or **[AudioRecorder](#audiorecorderdeprecated)**.) For video-only recording, set only **videoSourceType**. For audio and video recording, set both **audioSourceType** and **videoSourceType**. 4172 4173**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 4174 4175**System API**: This is a system API. 4176 4177| Name | Type | Mandatory| Description | 4178| --------------- | ---------------------------------------------- | ---- | ------------------------------------------------------------ | 4179| audioSourceType | [AudioSourceType](#audiosourcetype9) | No | Type of the audio source for video recording. This parameter is mandatory for audio recording. | 4180| videoSourceType | [VideoSourceType](#videosourcetype9) | Yes | Type of the video source for video recording. | 4181| profile | [VideoRecorderProfile](#videorecorderprofile9) | Yes | Video recording profile. | 4182| rotation | number | No | Rotation angle of the recorded video. The value can only be 0 (default), 90, 180, or 270. | 4183| location | [Location](#location) | No | Geographical location of the recorded video. By default, the geographical location information is not recorded. | 4184| url | string | Yes | Video output URL. Supported: fd://xx (fd number)<br> | 4185 4186## VideoRecorderProfile<sup>9+</sup> 4187 4188Describes the video recording profile. 4189 4190**System capability**: SystemCapability.Multimedia.Media.VideoRecorder 4191 4192**System API**: This is a system API. 4193 4194| Name | Type | Mandatory| Description | 4195| ---------------- | -------------------------------------------- | ---- | ---------------- | 4196| audioBitrate | number | No | Audio encoding bit rate. This parameter is mandatory for audio recording.| 4197| audioChannels | number | No | Number of audio channels. This parameter is mandatory for audio recording.| 4198| audioCodec | [CodecMimeType](#codecmimetype8) | No | Audio encoding format. This parameter is mandatory for audio recording. | 4199| audioSampleRate | number | No | Audio sampling rate. This parameter is mandatory for audio recording. | 4200| fileFormat | [ContainerFormatType](#containerformattype8) | Yes | Container format of a file.| 4201| videoBitrate | number | Yes | Video encoding bit rate.| 4202| videoCodec | [CodecMimeType](#codecmimetype8) | Yes | Video encoding format. | 4203| videoFrameWidth | number | Yes | Width of the recorded video frame.| 4204| videoFrameHeight | number | Yes | Height of the recorded video frame.| 4205| videoFrameRate | number | Yes | Video frame rate. | 4206 4207## media.createAudioPlayer<sup>(deprecated)</sup> 4208 4209createAudioPlayer(): AudioPlayer 4210 4211Creates an **AudioPlayer** instance in synchronous mode. 4212 4213> **NOTE** 4214> 4215> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [createAVPlayer](#mediacreateavplayer9) instead. 4216 4217**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4218 4219**Return value** 4220 4221| Type | Description | 4222| --------------------------- | ------------------------------------------------------------ | 4223| [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.| 4224 4225**Example** 4226 4227```ts 4228let audioPlayer: media.AudioPlayer = media.createAudioPlayer(); 4229``` 4230 4231## media.createVideoPlayer<sup>(deprecated)</sup> 4232 4233createVideoPlayer(callback: AsyncCallback\<VideoPlayer>): void 4234 4235Creates a **VideoPlayer** instance. This API uses an asynchronous callback to return the result. 4236 4237> **NOTE** 4238> 4239> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [createAVPlayer](#mediacreateavplayer9) instead. 4240 4241**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4242 4243**Parameters** 4244 4245| Name | Type | Mandatory| Description | 4246| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 4247| 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.| 4248 4249**Example** 4250 4251```ts 4252import { BusinessError } from '@ohos.base'; 4253 4254let videoPlayer: media.VideoPlayer; 4255media.createVideoPlayer((error: BusinessError, video: media.VideoPlayer) => { 4256 if (video != null) { 4257 videoPlayer = video; 4258 console.info('video createVideoPlayer success'); 4259 } else { 4260 console.error(`video createVideoPlayer fail, error:${error}`); 4261 } 4262}); 4263``` 4264 4265## media.createVideoPlayer<sup>(deprecated)</sup> 4266 4267createVideoPlayer(): Promise\<VideoPlayer> 4268 4269Creates a **VideoPlayer** instance. This API uses a promise to return the result. 4270 4271> **NOTE** 4272> 4273> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [createAVPlayer](#mediacreateavplayer9-1) instead. 4274 4275**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4276 4277**Return value** 4278 4279| Type | Description | 4280| ------------------------------------ | ------------------------------------------------------------ | 4281| 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.| 4282 4283**Example** 4284 4285```ts 4286import { BusinessError } from '@ohos.base'; 4287 4288let videoPlayer: media.VideoPlayer; 4289media.createVideoPlayer().then((video: media.VideoPlayer) => { 4290 if (video != null) { 4291 videoPlayer = video; 4292 console.info('video createVideoPlayer success'); 4293 } else { 4294 console.error('video createVideoPlayer fail'); 4295 } 4296}).catch((error: BusinessError) => { 4297 console.error(`video catchCallback, error:${error}`); 4298}); 4299``` 4300 4301## media.createAudioRecorder<sup>(deprecated)</sup> 4302 4303createAudioRecorder(): AudioRecorder 4304 4305Creates an **AudioRecorder** instance to control audio recording. 4306Only one **AudioRecorder** instance can be created per device. 4307 4308> **NOTE** 4309> 4310> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [createAVRecorder](#mediacreateavrecorder9) instead. 4311 4312**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 4313 4314**Return value** 4315 4316| Type | Description | 4317| ------------------------------- | ------------------------------------------------------------ | 4318| [AudioRecorder](#audiorecorderdeprecated) | If the operation is successful, an **AudioRecorder** instance is returned; otherwise, **null** is returned. The instance can be used to record audio.| 4319 4320**Example** 4321 4322```ts 4323let audioRecorder: media.AudioRecorder = media.createAudioRecorder(); 4324``` 4325 4326## MediaErrorCode<sup>(deprecated)</sup> 4327 4328Enumerates the media error codes. 4329 4330> **NOTE** 4331> 4332> This enum is supported since API version 8 and deprecated since API version 9. You are advised to use [Media Error Codes](#averrorcode9) instead. 4333 4334**System capability**: SystemCapability.Multimedia.Media.Core 4335 4336| Name | Value | Description | 4337| -------------------------- | ---- | -------------------------------------- | 4338| MSERR_OK | 0 | The operation is successful. | 4339| MSERR_NO_MEMORY | 1 | Failed to allocate memory. The system may have no available memory.| 4340| MSERR_OPERATION_NOT_PERMIT | 2 | No permission to perform the operation. | 4341| MSERR_INVALID_VAL | 3 | Invalid input parameter. | 4342| MSERR_IO | 4 | An I/O error occurs. | 4343| MSERR_TIMEOUT | 5 | The operation times out. | 4344| MSERR_UNKNOWN | 6 | An unknown error occurs. | 4345| MSERR_SERVICE_DIED | 7 | Invalid server. | 4346| MSERR_INVALID_STATE | 8 | The operation is not allowed in the current state. | 4347| MSERR_UNSUPPORTED | 9 | The operation is not supported in the current version. | 4348 4349## AudioPlayer<sup>(deprecated)</sup> 4350 4351> **NOTE** 4352> 4353> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayer](#avplayer9) instead. 4354 4355Provides APIs to manage and play audio. Before calling any API in **AudioPlayer**, you must use [createAudioPlayer()](#mediacreateaudioplayerdeprecated) to create an **AudioPlayer** instance. 4356 4357### Attributes<sup>(deprecated)</sup> 4358 4359**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4360 4361| Name | Type | Readable| Writable| Description | 4362| ------------------------------- | ------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 4363| src | string | Yes | Yes | Audio file URI. The mainstream audio formats (M4A, AAC, MP3, OGG, and WAV) 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>**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.INTERNET| 4364| 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>| 4365| loop | boolean | Yes | Yes | Whether to loop audio playback. The value **true** means to loop audio playback, and **false** means the opposite. | 4366| audioInterruptMode<sup>9+</sup> | [audio.InterruptMode](js-apis-audio.md#interruptmode9) | Yes | Yes | Audio interruption mode. | 4367| currentTime | number | Yes | No | Current audio playback position, in ms. | 4368| duration | number | Yes | No | Audio duration, in ms. | 4369| state | [AudioState](#audiostatedeprecated) | Yes | No | Audio playback state. This state cannot be used as the condition for triggering the call of **play()**, **pause()**, or **stop()**.| 4370 4371### play<sup>(deprecated)</sup> 4372 4373play(): void 4374 4375Starts to play an audio asset. This API can be called only after the **'dataLoad'** event is triggered. 4376 4377> **NOTE** 4378> 4379> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayer.play](#play9) instead. 4380 4381**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4382 4383**Example** 4384 4385```ts 4386audioPlayer.on('play', () => { // Set the 'play' event callback. 4387 console.info('audio play success'); 4388}); 4389audioPlayer.play(); 4390``` 4391 4392### pause<sup>(deprecated)</sup> 4393 4394pause(): void 4395 4396Pauses audio playback. 4397 4398> **NOTE** 4399> 4400> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayer.pause](#pause9) instead. 4401 4402**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4403 4404**Example** 4405 4406```ts 4407audioPlayer.on('pause', () => { // Set the 'pause' event callback. 4408 console.info('audio pause success'); 4409}); 4410audioPlayer.pause(); 4411``` 4412 4413### stop<sup>(deprecated)</sup> 4414 4415stop(): void 4416 4417Stops audio playback. 4418 4419> **NOTE** 4420> 4421> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayer.stop](#stop9) instead. 4422 4423**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4424 4425**Example** 4426 4427```ts 4428audioPlayer.on('stop', () => { // Set the 'stop' event callback. 4429 console.info('audio stop success'); 4430}); 4431audioPlayer.stop(); 4432``` 4433 4434### reset<sup>(deprecated)</sup> 4435 4436reset(): void 4437 4438Resets the audio asset to be played. 4439 4440> **NOTE** 4441> 4442> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [AVPlayer.reset](#reset9) instead. 4443 4444**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4445 4446**Example** 4447 4448```ts 4449audioPlayer.on('reset', () => { // Set the 'reset' event callback. 4450 console.info('audio reset success'); 4451}); 4452audioPlayer.reset(); 4453``` 4454 4455### seek<sup>(deprecated)</sup> 4456 4457seek(timeMs: number): void 4458 4459Seeks to the specified playback position. 4460 4461> **NOTE** 4462> 4463> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayer.seek](#seek9) instead. 4464 4465**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4466 4467**Parameters** 4468 4469| Name| Type | Mandatory| Description | 4470| ------ | ------ | ---- | ----------------------------------------------------------- | 4471| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, duration].| 4472 4473**Example** 4474 4475```ts 4476audioPlayer.on('timeUpdate', (seekDoneTime: number) => { // Set the 'timeUpdate' event callback. 4477 if (seekDoneTime == null) { 4478 console.error('audio seek fail'); 4479 return; 4480 } 4481 console.info('audio seek success. seekDoneTime: ' + seekDoneTime); 4482}); 4483audioPlayer.seek(30000); // Seek to 30000 ms. 4484``` 4485 4486### setVolume<sup>(deprecated)</sup> 4487 4488setVolume(vol: number): void 4489 4490Sets the volume. 4491 4492> **NOTE** 4493> 4494> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayer.setVolume](#setvolume9) instead. 4495 4496**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4497 4498**Parameters** 4499 4500| Name| Type | Mandatory| Description | 4501| ------ | ------ | ---- | ------------------------------------------------------------ | 4502| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1.00** indicates the maximum volume (100%).| 4503 4504**Example** 4505 4506```ts 4507audioPlayer.on('volumeChange', () => { // Set the 'volumeChange' event callback. 4508 console.info('audio volumeChange success'); 4509}); 4510audioPlayer.setVolume(1); // Set the volume to 100%. 4511``` 4512 4513### release<sup>(deprecated)</sup> 4514 4515release(): void 4516 4517Releases the audio playback resources. 4518 4519> **NOTE** 4520> 4521> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayer.release](#release9) instead. 4522 4523**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4524 4525**Example** 4526 4527```ts 4528audioPlayer.release(); 4529audioPlayer = undefined; 4530``` 4531 4532### getTrackDescription<sup>(deprecated)</sup> 4533 4534getTrackDescription(callback: AsyncCallback\<Array\<MediaDescription>>): void 4535 4536Obtains the audio track information. This API uses an asynchronous callback to return the result. It can be called only after the **'dataLoad'** event is triggered. 4537 4538> **NOTE** 4539> 4540> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.getTrackDescription](#gettrackdescription9) instead. 4541 4542**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4543 4544**Parameters** 4545 4546| Name | Type | Mandatory| Description | 4547| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------ | 4548| callback | AsyncCallback\<Array\<[MediaDescription](#mediadescription8)>> | Yes | Callback used to return a **MediaDescription** array, which records the audio track information.| 4549 4550**Example** 4551 4552```ts 4553audioPlayer.getTrackDescription((error: BusinessError, arrList: Array<media.MediaDescription>) => { 4554 if (arrList != null) { 4555 console.info('audio getTrackDescription success'); 4556 } else { 4557 console.error(`audio getTrackDescription fail, error:${error}`); 4558 } 4559}); 4560``` 4561 4562### getTrackDescription<sup>(deprecated)</sup> 4563 4564getTrackDescription(): Promise\<Array\<MediaDescription>> 4565 4566Obtains the audio track information. This API uses a promise to return the result. It can be called only after the **'dataLoad'** event is triggered. 4567 4568> **NOTE** 4569> 4570> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.getTrackDescription](#gettrackdescription9-1) instead. 4571 4572**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4573 4574**Return value** 4575 4576| Type | Description | 4577| ------------------------------------------------------ | ----------------------------------------------- | 4578| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return a **MediaDescription** array, which records the audio track information.| 4579 4580**Example** 4581 4582```ts 4583audioPlayer.getTrackDescription().then((arrList: Array<media.MediaDescription>) => { 4584 console.info('audio getTrackDescription success'); 4585}).catch((error: BusinessError) => { 4586 console.error(`audio catchCallback, error:${error}`); 4587}); 4588``` 4589 4590### on('bufferingUpdate')<sup>(deprecated)</sup> 4591 4592on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void 4593 4594Subscribes to the audio buffering update event. This API works only under online playback. 4595 4596> **NOTE** 4597> 4598> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.on('bufferingUpdate')](#onbufferingupdate9) instead. 4599 4600**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4601 4602**Parameters** 4603 4604| Name | Type | Mandatory| Description | 4605| -------- | -------- | ---- | ------------------------------------------------------------ | 4606| type | string | Yes | Event type, which is **'bufferingUpdate'** in this case. | 4607| 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**.| 4608 4609**Example** 4610 4611```ts 4612audioPlayer.on('bufferingUpdate', (infoType: media.BufferingInfoType, value: number) => { 4613 console.info('audio bufferingInfo type: ' + infoType); 4614 console.info('audio bufferingInfo value: ' + value); 4615}); 4616``` 4617 4618### on('play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeChange')<sup>(deprecated)</sup> 4619 4620on(type: 'play' | 'pause' | 'stop' | 'reset' | 'dataLoad' | 'finish' | 'volumeChange', callback: () => void): void 4621 4622Subscribes to the audio playback events. 4623 4624> **NOTE** 4625> 4626> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayer.on('stateChange')](#onstatechange9) instead. 4627 4628**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4629 4630**Parameters** 4631 4632| Name | Type | Mandatory| Description | 4633| -------- | ---------- | ---- | ------------------------------------------------------------ | 4634| type | string | Yes | Event type. The following events are supported:<br>- 'play': triggered when the **play()** API is called and audio playback starts.<br>- 'pause': triggered when the **pause()** API is called and audio playback is paused.<br>- 'stop': triggered when the **stop()** API is called and audio playback stops.<br>- 'reset': triggered when the **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()** API is called and the playback volume is changed.| 4635| callback | () => void | Yes | Callback invoked when the event is triggered. | 4636 4637**Example** 4638 4639```ts 4640import fs from '@ohos.file.fs'; 4641 4642let audioPlayer: media.AudioPlayer = media.createAudioPlayer(); // Create an AudioPlayer instance. 4643audioPlayer.on('dataLoad', () => { // Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully. 4644 console.info('audio set source success'); 4645 audioPlayer.play(); // Start the playback and trigger the 'play' event callback. 4646}); 4647audioPlayer.on('play', () => { // Set the 'play' event callback. 4648 console.info('audio play success'); 4649 audioPlayer.seek(30000); // Call the seek() API and trigger the 'timeUpdate' event callback. 4650}); 4651audioPlayer.on('pause', () => { // Set the 'pause' event callback. 4652 console.info('audio pause success'); 4653 audioPlayer.stop(); // Stop the playback and trigger the 'stop' event callback. 4654}); 4655audioPlayer.on('reset', () => { // Set the 'reset' event callback. 4656 console.info('audio reset success'); 4657 audioPlayer.release(); // Release the AudioPlayer instance. 4658 audioPlayer = undefined; 4659}); 4660audioPlayer.on('timeUpdate', (seekDoneTime: number) => { // Set the 'timeUpdate' event callback. 4661 if (seekDoneTime == null) { 4662 console.error('audio seek fail'); 4663 return; 4664 } 4665 console.info('audio seek success, and seek time is ' + seekDoneTime); 4666 audioPlayer.setVolume(0.5); // Set the volume to 50% and trigger the 'volumeChange' event callback. 4667}); 4668audioPlayer.on('volumeChange', () => { // Set the 'volumeChange' event callback. 4669 console.info('audio volumeChange success'); 4670 audioPlayer.pause(); // Pause the playback and trigger the 'pause' event callback. 4671}); 4672audioPlayer.on('finish', () => { // Set the 'finish' event callback. 4673 console.info('audio play finish'); 4674 audioPlayer.stop(); // Stop the playback and trigger the 'stop' event callback. 4675}); 4676audioPlayer.on('error', (error: BusinessError) => { // Set the 'error' event callback. 4677 console.info(`audio error called, error: ${error}`); 4678}); 4679 4680// Set the FD (local playback) of the audio file selected by the user. 4681let fdPath = 'fd://'; 4682// 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. 4683let path = '/data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3'; 4684fs.open(path).then((file) => { 4685 fdPath = fdPath + '' + file.fd; 4686 console.info('open fd success fd is' + fdPath); 4687 audioPlayer.src = fdPath; // Set the src attribute and trigger the 'dataLoad' event callback. 4688}, (err: BusinessError) => { 4689 console.error('open fd failed err is' + err); 4690}).catch((err: BusinessError) => { 4691 console.error('open fd failed err is' + err); 4692}); 4693``` 4694 4695### on('timeUpdate')<sup>(deprecated)</sup> 4696 4697on(type: 'timeUpdate', callback: Callback\<number>): void 4698 4699Subscribes to the **'timeUpdate'** event. This event is reported every second when the audio playback is in progress. 4700 4701> **NOTE** 4702> 4703> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayer.on('timeUpdate')](#ontimeupdate9) instead. 4704 4705**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4706 4707**Parameters** 4708 4709| Name | Type | Mandatory| Description | 4710| -------- | ----------------- | ---- | ------------------------------------------------------------ | 4711| 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.| 4712| callback | Callback\<number> | Yes | Callback invoked when the event is triggered. The input parameter is the updated timestamp. | 4713 4714**Example** 4715 4716```ts 4717audioPlayer.on('timeUpdate', (newTime: number) => { // Set the 'timeUpdate' event callback. 4718 if (newTime == null) { 4719 console.error('audio timeUpadate fail'); 4720 return; 4721 } 4722 console.info('audio timeUpadate success. seekDoneTime: ' + newTime); 4723}); 4724audioPlayer.play(); // The 'timeUpdate' event is triggered when the playback starts. 4725``` 4726 4727### on('error')<sup>(deprecated)</sup> 4728 4729on(type: 'error', callback: ErrorCallback): void 4730 4731Subscribes to audio playback error events. After an error event is reported, you must handle the event and exit the playback. 4732 4733> **NOTE** 4734> 4735> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayer.on('error')](#onerror9) instead. 4736 4737**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4738 4739**Parameters** 4740 4741| Name | Type | Mandatory| Description | 4742| -------- | ------------- | ---- | ------------------------------------------------------------ | 4743| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during audio playback.| 4744| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 4745 4746**Example** 4747 4748```ts 4749audioPlayer.on('error', (error: BusinessError) => { // Set the 'error' event callback. 4750 console.info(`audio error called, error: ${error}`); 4751}); 4752audioPlayer.setVolume(3); // Set volume to an invalid value to trigger the 'error' event. 4753``` 4754 4755## AudioState<sup>(deprecated)</sup> 4756 4757Enumerates the audio playback states. You can obtain the state through the **state** attribute. 4758 4759> **NOTE** 4760> 4761> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVPlayerState](#avplayerstate9) instead. 4762 4763**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 4764 4765| Name | Type | Description | 4766| ------- | ------ | ---------------------------------------------- | 4767| idle | string | No audio playback is in progress. The audio player is in this state after the **'dataload'** or **'reset'** event is triggered.| 4768| playing | string | Audio playback is in progress. The audio player is in this state after the **'play'** event is triggered. | 4769| paused | string | Audio playback is paused. The audio player is in this state after the **'pause'** event is triggered. | 4770| stopped | string | Audio playback is stopped. The audio player is in this state after the **'stop'** event is triggered. | 4771| error | string | Audio playback is in the error state. | 4772 4773## VideoPlayer<sup>(deprecated)</sup> 4774 4775> **NOTE** 4776> 4777> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer](#avplayer9) instead. 4778 4779Provides APIs to manage and play video. Before calling any API of **VideoPlayer**, you must use [createVideoPlayer()](#mediacreatevideoplayerdeprecated) to create a **VideoPlayer** instance. 4780 4781### Attributes 4782 4783**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4784 4785| Name | Type | Readable| Writable| Description | 4786| ------------------------------- | ------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 4787| url<sup>8+</sup> | string | Yes | Yes | Video URL. The mainstream video formats (MP4, MPEG-TS, 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>**NOTE**<br>WebM is not supported since API version 11.| 4788| 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>| 4789| 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. | 4790| videoScaleType<sup>9+</sup> | [VideoScaleType](#videoscaletype9) | Yes | Yes | Video scale type. | 4791| audioInterruptMode<sup>9+</sup> | [audio.InterruptMode](js-apis-audio.md#interruptmode9) | Yes | Yes | Audio interruption mode. | 4792| currentTime<sup>8+</sup> | number | Yes | No | Current video playback position, in ms. | 4793| duration<sup>8+</sup> | number | Yes | No | Video duration, in ms. The value **-1** indicates the live mode. | 4794| state<sup>8+</sup> | [VideoPlayState](#videoplaystatedeprecated) | Yes | No | Video playback state. | 4795| width<sup>8+</sup> | number | Yes | No | Video width, in pixels. | 4796| height<sup>8+</sup> | number | Yes | No | Video height, in pixels. | 4797 4798### setDisplaySurface<sup>(deprecated)</sup> 4799 4800setDisplaySurface(surfaceId: string, callback: AsyncCallback\<void>): void 4801 4802Sets **SurfaceId**. This API uses an asynchronous callback to return the result. 4803 4804*Note: **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. 4805 4806> **NOTE** 4807> 4808> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.surfaceId](#attributes) instead. 4809 4810**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4811 4812**Parameters** 4813 4814| Name | Type | Mandatory| Description | 4815| --------- | -------------------- | ---- | ------------------------- | 4816| surfaceId | string | Yes | Surface ID to set. | 4817| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4818 4819**Example** 4820 4821```ts 4822let surfaceId: string = ''; 4823videoPlayer.setDisplaySurface(surfaceId, (err: BusinessError) => { 4824 if (err == null) { 4825 console.info('setDisplaySurface success!'); 4826 } else { 4827 console.error('setDisplaySurface fail!'); 4828 } 4829}); 4830``` 4831 4832### setDisplaySurface<sup>(deprecated)</sup> 4833 4834setDisplaySurface(surfaceId: string): Promise\<void> 4835 4836Sets **SurfaceId**. This API uses a promise to return the result. 4837 4838*Note: **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. 4839 4840> **NOTE** 4841> 4842> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.surfaceId](#attributes) instead. 4843 4844**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4845 4846**Parameters** 4847 4848| Name | Type | Mandatory| Description | 4849| --------- | ------ | ---- | --------- | 4850| surfaceId | string | Yes | Surface ID to set.| 4851 4852**Return value** 4853 4854| Type | Description | 4855| -------------- | ------------------------------ | 4856| Promise\<void> | Promise used to return the result.| 4857 4858**Example** 4859 4860```ts 4861let surfaceId: string = ''; 4862videoPlayer.setDisplaySurface(surfaceId).then(() => { 4863 console.info('setDisplaySurface success'); 4864}).catch((error: BusinessError) => { 4865 console.error(`video catchCallback, error:${error}`); 4866}); 4867``` 4868 4869### prepare<sup>(deprecated)</sup> 4870 4871prepare(callback: AsyncCallback\<void>): void 4872 4873Prepares for video playback. This API uses an asynchronous callback to return the result. 4874 4875> **NOTE** 4876> 4877> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.prepare](#prepare9) instead. 4878 4879**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4880 4881**Parameters** 4882 4883| Name | Type | Mandatory| Description | 4884| -------- | -------------------- | ---- | ------------------------ | 4885| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4886 4887**Example** 4888 4889```ts 4890videoPlayer.prepare((err: BusinessError) => { 4891 if (err == null) { 4892 console.info('prepare success!'); 4893 } else { 4894 console.error('prepare fail!'); 4895 } 4896}); 4897``` 4898 4899### prepare<sup>(deprecated)</sup> 4900 4901prepare(): Promise\<void> 4902 4903Prepares for video playback. This API uses a promise to return the result. 4904 4905> **NOTE** 4906> 4907> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.prepare](#prepare9-1) instead. 4908 4909**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4910 4911**Return value** 4912 4913| Type | Description | 4914| -------------- | ----------------------------- | 4915| Promise\<void> | Promise used to return the result.| 4916 4917**Example** 4918 4919```ts 4920videoPlayer.prepare().then(() => { 4921 console.info('prepare success'); 4922}).catch((error: BusinessError) => { 4923 console.error(`video catchCallback, error:${error}`); 4924}); 4925``` 4926 4927### play<sup>(deprecated)</sup> 4928 4929play(callback: AsyncCallback\<void>): void 4930 4931Starts to play video assets. This API uses an asynchronous callback to return the result. 4932 4933> **NOTE** 4934> 4935> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.play](#play9) instead. 4936 4937**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4938 4939**Parameters** 4940 4941| Name | Type | Mandatory| Description | 4942| -------- | -------------------- | ---- | ------------------------ | 4943| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 4944 4945**Example** 4946 4947```ts 4948videoPlayer.play((err: BusinessError) => { 4949 if (err == null) { 4950 console.info('play success!'); 4951 } else { 4952 console.error('play fail!'); 4953 } 4954}); 4955``` 4956 4957### play<sup>(deprecated)</sup> 4958 4959play(): Promise\<void> 4960 4961Starts to play video assets. This API uses a promise to return the result. 4962 4963> **NOTE** 4964> 4965> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.play](#play9-1) instead. 4966 4967**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4968 4969**Return value** 4970 4971| Type | Description | 4972| -------------- | ----------------------------- | 4973| Promise\<void> | Promise used to return the result.| 4974 4975**Example** 4976 4977```ts 4978videoPlayer.play().then(() => { 4979 console.info('play success'); 4980}).catch((error: BusinessError) => { 4981 console.error(`video catchCallback, error:${error}`); 4982}); 4983``` 4984 4985### pause<sup>(deprecated)</sup> 4986 4987pause(callback: AsyncCallback\<void>): void 4988 4989Pauses video playback. This API uses an asynchronous callback to return the result. 4990 4991> **NOTE** 4992> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.pause](#pause9) instead. 4993 4994**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 4995 4996**Parameters** 4997 4998| Name | Type | Mandatory| Description | 4999| -------- | -------------------- | ---- | ------------------------ | 5000| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 5001 5002**Example** 5003 5004```ts 5005videoPlayer.pause((err: BusinessError) => { 5006 if (err == null) { 5007 console.info('pause success!'); 5008 } else { 5009 console.error('pause fail!'); 5010 } 5011}); 5012``` 5013 5014### pause<sup>(deprecated)</sup> 5015 5016pause(): Promise\<void> 5017 5018Pauses video playback. This API uses a promise to return the result. 5019 5020> **NOTE** 5021> 5022> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.pause](#pause9-1) instead. 5023 5024**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5025 5026**Return value** 5027 5028| Type | Description | 5029| -------------- | ----------------------------- | 5030| Promise\<void> | Promise used to return the result.| 5031 5032**Example** 5033 5034```ts 5035videoPlayer.pause().then(() => { 5036 console.info('pause success'); 5037}).catch((error: BusinessError) => { 5038 console.error(`video catchCallback, error:${error}`); 5039}); 5040``` 5041 5042### stop<sup>(deprecated)</sup> 5043 5044stop(callback: AsyncCallback\<void>): void 5045 5046Stops video playback. This API uses an asynchronous callback to return the result. 5047 5048> **NOTE** 5049> 5050> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.stop](#stop9) instead. 5051 5052**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5053 5054**Parameters** 5055 5056| Name | Type | Mandatory| Description | 5057| -------- | -------------------- | ---- | ------------------------ | 5058| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 5059 5060**Example** 5061 5062```ts 5063videoPlayer.stop((err: BusinessError) => { 5064 if (err == null) { 5065 console.info('stop success!'); 5066 } else { 5067 console.error('stop fail!'); 5068 } 5069}); 5070``` 5071 5072### stop<sup>(deprecated)</sup> 5073 5074stop(): Promise\<void> 5075 5076Stops video playback. This API uses a promise to return the result. 5077 5078> **NOTE** 5079> 5080> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.stop](#stop9-1) instead. 5081 5082**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5083 5084**Return value** 5085 5086| Type | Description | 5087| -------------- | ----------------------------- | 5088| Promise\<void> | Promise used to return the result.| 5089 5090**Example** 5091 5092```ts 5093videoPlayer.stop().then(() => { 5094 console.info('stop success'); 5095}).catch((error: BusinessError) => { 5096 console.error(`video catchCallback, error:${error}`); 5097}); 5098``` 5099 5100### reset<sup>(deprecated)</sup> 5101 5102reset(callback: AsyncCallback\<void>): void 5103 5104Resets the video asset to be played. This API uses an asynchronous callback to return the result. 5105 5106> **NOTE** 5107> 5108> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.reset](#reset9) instead. 5109 5110**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5111 5112**Parameters** 5113 5114| Name | Type | Mandatory| Description | 5115| -------- | -------------------- | ---- | ------------------------ | 5116| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 5117 5118**Example** 5119 5120```ts 5121videoPlayer.reset((err: BusinessError) => { 5122 if (err == null) { 5123 console.info('reset success!'); 5124 } else { 5125 console.error('reset fail!'); 5126 } 5127}); 5128``` 5129 5130### reset<sup>(deprecated)</sup> 5131 5132reset(): Promise\<void> 5133 5134Resets the video asset to be played. This API uses a promise to return the result. 5135 5136> **NOTE** 5137> 5138> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.reset](#reset9-1) instead. 5139 5140**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5141 5142**Return value** 5143 5144| Type | Description | 5145| -------------- | ----------------------------- | 5146| Promise\<void> | Promise used to return the result.| 5147 5148**Example** 5149 5150```ts 5151videoPlayer.reset().then(() => { 5152 console.info('reset success'); 5153}).catch((error: BusinessError) => { 5154 console.error(`video catchCallback, error:${error}`); 5155}); 5156``` 5157 5158### seek<sup>(deprecated)</sup> 5159 5160seek(timeMs: number, callback: AsyncCallback\<number>): void 5161 5162Seeks 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. 5163 5164> **NOTE** 5165> 5166> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.seek](#seek9) instead. 5167 5168**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5169 5170**Parameters** 5171 5172| Name | Type | Mandatory| Description | 5173| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 5174| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, duration].| 5175| callback | AsyncCallback\<number> | Yes | Callback used to return the result. | 5176 5177**Example** 5178 5179```ts 5180let videoPlayer: media.VideoPlayer; 5181media.createVideoPlayer((error: BusinessError, video: media.VideoPlayer) => { 5182 if (video != null) { 5183 videoPlayer = video; 5184 console.info('video createVideoPlayer success'); 5185 } else { 5186 console.error(`video createVideoPlayer fail, error:${error}`); 5187 } 5188}); 5189 5190let seekTime: number = 5000; 5191videoPlayer.seek(seekTime, (err: BusinessError, result: number) => { 5192 if (err == null) { 5193 console.info('seek success!'); 5194 } else { 5195 console.error('seek fail!'); 5196 } 5197}); 5198``` 5199 5200### seek<sup>(deprecated)</sup> 5201 5202seek(timeMs: number, mode:SeekMode, callback: AsyncCallback\<number>): void 5203 5204Seeks to the specified playback position. This API uses an asynchronous callback to return the result. 5205 5206> **NOTE** 5207> 5208> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.seek](#seek9) instead. 5209 5210**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5211 5212**Parameters** 5213 5214| Name | Type | Mandatory| Description | 5215| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 5216| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, duration].| 5217| mode | [SeekMode](#seekmode8) | Yes | Seek mode. | 5218| callback | AsyncCallback\<number> | Yes | Callback used to return the result. | 5219 5220**Example** 5221 5222```ts 5223let videoPlayer: media.VideoPlayer | null = null; 5224media.createVideoPlayer((error: BusinessError, video: media.VideoPlayer) => { 5225 if (video != null) { 5226 videoPlayer = video; 5227 console.info('video createVideoPlayer success'); 5228 } else { 5229 console.error(`video createVideoPlayer fail, error:${error}`); 5230 } 5231}); 5232let seekTime: number = 5000; 5233if (videoPlayer) { 5234 (videoPlayer as media.VideoPlayer).seek(seekTime, media.SeekMode.SEEK_NEXT_SYNC, (err: BusinessError, result: number) => { 5235 if (err == null) { 5236 console.info('seek success!'); 5237 } else { 5238 console.error('seek fail!'); 5239 } 5240 }); 5241} 5242``` 5243 5244### seek<sup>(deprecated)</sup> 5245 5246seek(timeMs: number, mode?:SeekMode): Promise\<number> 5247 5248Seeks 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. 5249 5250> **NOTE** 5251> 5252> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.seek](#seek9) instead. 5253 5254**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5255 5256**Parameters** 5257 5258| Name| Type | Mandatory| Description | 5259| ------ | ---------------------- | ---- | ------------------------------------------------------------ | 5260| timeMs | number | Yes | Position to seek to, in ms. The value range is [0, duration].| 5261| mode | [SeekMode](#seekmode8) | No | Seek mode based on the video I frame. The default value is **SEEK_PREV_SYNC**. | 5262 5263**Return value** 5264 5265| Type | Description | 5266| ---------------- | ------------------------------------------- | 5267| Promise\<number>| Promise used to return the playback position, in ms.| 5268 5269**Example** 5270 5271```ts 5272let videoPlayer: media.VideoPlayer | null = null; 5273media.createVideoPlayer((error: BusinessError, video: media.VideoPlayer) => { 5274 if (video != null) { 5275 videoPlayer = video; 5276 console.info('video createVideoPlayer success'); 5277 } else { 5278 console.error(`video createVideoPlayer fail, error:${error}`); 5279 } 5280}); 5281let seekTime: number = 5000; 5282if (videoPlayer) { 5283 (videoPlayer as media.VideoPlayer).seek(seekTime).then((seekDoneTime: number) => { // seekDoneTime indicates the position after the seek operation is complete. 5284 console.info('seek success'); 5285 }).catch((error: BusinessError) => { 5286 console.error(`video catchCallback, error:${error}`); 5287 }); 5288 5289 (videoPlayer as media.VideoPlayer).seek(seekTime, media.SeekMode.SEEK_NEXT_SYNC).then((seekDoneTime: number) => { 5290 console.info('seek success'); 5291 }).catch((error: BusinessError) => { 5292 console.error(`video catchCallback, error:${error}`); 5293 }); 5294} 5295``` 5296 5297### setVolume<sup>(deprecated)</sup> 5298 5299setVolume(vol: number, callback: AsyncCallback\<void>): void 5300 5301Sets the volume. This API uses an asynchronous callback to return the result. 5302 5303> **NOTE** 5304> 5305> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.setVolume](#setvolume9) instead. 5306 5307**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5308 5309**Parameters** 5310 5311| Name | Type | Mandatory| Description | 5312| -------- | -------------------- | ---- | ------------------------------------------------------------ | 5313| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1.00** indicates the maximum volume (100%).| 5314| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 5315 5316**Example** 5317 5318```ts 5319let vol: number = 0.5; 5320videoPlayer.setVolume(vol, (err: BusinessError) => { 5321 if (err == null) { 5322 console.info('setVolume success!'); 5323 } else { 5324 console.error('setVolume fail!'); 5325 } 5326}); 5327``` 5328 5329### setVolume<sup>(deprecated)</sup> 5330 5331setVolume(vol: number): Promise\<void> 5332 5333Sets the volume. This API uses a promise to return the result. 5334 5335> **NOTE** 5336> 5337> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.setVolume](#setvolume9) instead. 5338 5339**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5340 5341**Parameters** 5342 5343| Name| Type | Mandatory| Description | 5344| ------ | ------ | ---- | ------------------------------------------------------------ | 5345| vol | number | Yes | Relative volume. The value ranges from 0.00 to 1.00. The value **1.00** indicates the maximum volume (100%).| 5346 5347**Return value** 5348 5349| Type | Description | 5350| -------------- | ------------------------- | 5351| Promise\<void> | Promise used to return the result.| 5352 5353**Example** 5354 5355```ts 5356let vol: number = 0.5; 5357videoPlayer.setVolume(vol).then(() => { 5358 console.info('setVolume success'); 5359}).catch((error: BusinessError) => { 5360 console.error(`video catchCallback, error:${error}`); 5361}); 5362``` 5363 5364### release<sup>(deprecated)</sup> 5365 5366release(callback: AsyncCallback\<void>): void 5367 5368Releases the video playback resources. This API uses an asynchronous callback to return the result. 5369 5370> **NOTE** 5371> 5372> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.release](#release9) instead. 5373 5374**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5375 5376**Parameters** 5377 5378| Name | Type | Mandatory| Description | 5379| -------- | -------------------- | ---- | ------------------------ | 5380| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 5381 5382**Example** 5383 5384```ts 5385videoPlayer.release((err: BusinessError) => { 5386 if (err == null) { 5387 console.info('release success!'); 5388 } else { 5389 console.error('release fail!'); 5390 } 5391}); 5392``` 5393 5394### release<sup>(deprecated)</sup> 5395 5396release(): Promise\<void> 5397 5398Releases the video playback resources. This API uses a promise to return the result. 5399 5400> **NOTE** 5401> 5402> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.release](#release9-1) instead. 5403 5404**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5405 5406**Return value** 5407 5408| Type | Description | 5409| -------------- | ----------------------------- | 5410| Promise\<void> | Promise used to return the result.| 5411 5412**Example** 5413 5414```ts 5415videoPlayer.release().then(() => { 5416 console.info('release success'); 5417}).catch((error: BusinessError) => { 5418 console.error(`video catchCallback, error:${error}`); 5419}); 5420``` 5421 5422### getTrackDescription<sup>(deprecated)</sup> 5423 5424getTrackDescription(callback: AsyncCallback\<Array\<MediaDescription>>): void 5425 5426Obtains the video track information. This API uses an asynchronous callback to return the result. 5427 5428> **NOTE** 5429> 5430> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.getTrackDescription](#gettrackdescription9) instead. 5431 5432**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5433 5434**Parameters** 5435 5436| Name | Type | Mandatory| Description | 5437| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------ | 5438| callback | AsyncCallback\<Array\<[MediaDescription](#mediadescription8)>> | Yes | Callback used to return a **MediaDescription** array, which records the video track information.| 5439 5440**Example** 5441 5442```ts 5443videoPlayer.getTrackDescription((error: BusinessError, arrList: Array<media.MediaDescription>) => { 5444 if ((arrList) != null) { 5445 console.info('video getTrackDescription success'); 5446 } else { 5447 console.error(`video getTrackDescription fail, error:${error}`); 5448 } 5449}); 5450``` 5451 5452### getTrackDescription<sup>(deprecated)</sup> 5453 5454getTrackDescription(): Promise\<Array\<MediaDescription>> 5455 5456Obtains the video track information. This API uses a promise to return the result. 5457 5458> **NOTE** 5459> 5460> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.getTrackDescription](#gettrackdescription9-1) instead. 5461 5462**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5463 5464**Return value** 5465 5466| Type | Description | 5467| ------------------------------------------------------ | ----------------------------------------------- | 5468| Promise<Array<[MediaDescription](#mediadescription8)>> | Promise used to return a **MediaDescription** array, which records the video track information.| 5469 5470**Example** 5471 5472```ts 5473videoPlayer.getTrackDescription().then((arrList: Array<media.MediaDescription>) => { 5474 if (arrList != null) { 5475 console.info('video getTrackDescription success'); 5476 } else { 5477 console.error('video getTrackDescription fail'); 5478 } 5479}).catch((error: BusinessError) => { 5480 console.error(`video catchCallback, error:${error}`); 5481}); 5482``` 5483 5484### setSpeed<sup>(deprecated)</sup> 5485 5486setSpeed(speed:number, callback: AsyncCallback\<number>): void 5487 5488Sets the video playback speed. This API uses an asynchronous callback to return the result. 5489 5490> **NOTE** 5491> 5492> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.setSpeed](#setspeed9) instead. 5493 5494**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5495 5496**Parameters** 5497 5498| Name | Type | Mandatory| Description | 5499| -------- | ---------------------- | ---- | ---------------------------------------------------------- | 5500| speed | number | Yes | Video playback speed. For details, see [PlaybackSpeed](#playbackspeed8).| 5501| callback | AsyncCallback\<number> | Yes | Callback used to return the result. | 5502 5503**Example** 5504 5505```ts 5506let videoPlayer: media.VideoPlayer | null = null; 5507media.createVideoPlayer((error: BusinessError, video: media.VideoPlayer) => { 5508 if (video != null) { 5509 videoPlayer = video; 5510 console.info('video createVideoPlayer success'); 5511 } else { 5512 console.error(`video createVideoPlayer fail, error:${error}`); 5513 } 5514}); 5515let speed = media.PlaybackSpeed.SPEED_FORWARD_2_00_X; 5516if (videoPlayer) { 5517 (videoPlayer as media.VideoPlayer).setSpeed(speed, (err: BusinessError, result: number) => { 5518 if (err == null) { 5519 console.info('setSpeed success!'); 5520 } else { 5521 console.error('setSpeed fail!'); 5522 } 5523 }); 5524} 5525``` 5526 5527### setSpeed<sup>(deprecated)</sup> 5528 5529setSpeed(speed:number): Promise\<number> 5530 5531Sets the video playback speed. This API uses a promise to return the result. 5532 5533> **NOTE** 5534> 5535> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.setSpeed](#setspeed9) instead. 5536 5537**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5538 5539**Parameters** 5540 5541| Name| Type | Mandatory| Description | 5542| ------ | ------ | ---- | ---------------------------------------------------------- | 5543| speed | number | Yes | Video playback speed. For details, see [PlaybackSpeed](#playbackspeed8).| 5544 5545**Return value** 5546 5547| Type | Description | 5548| ---------------- | ------------------------------------------------------------ | 5549| Promise\<number>| Promise used to return playback speed. For details, see [PlaybackSpeed](#playbackspeed8).| 5550 5551**Example** 5552 5553```ts 5554let videoPlayer: media.VideoPlayer | null = null; 5555media.createVideoPlayer((error: BusinessError, video: media.VideoPlayer) => { 5556 if (video != null) { 5557 videoPlayer = video; 5558 console.info('video createVideoPlayer success'); 5559 } else { 5560 console.error(`video createVideoPlayer fail, error:${error}`); 5561 } 5562}); 5563let speed = media.PlaybackSpeed.SPEED_FORWARD_2_00_X; 5564if (videoPlayer) { 5565 (videoPlayer as media.VideoPlayer).setSpeed(speed).then((result: number) => { 5566 console.info('setSpeed success'); 5567 }).catch((error: BusinessError) => { 5568 console.error(`video catchCallback, error:${error}`); 5569 }); 5570} 5571``` 5572 5573### on('playbackCompleted')<sup>(deprecated)</sup> 5574 5575on(type: 'playbackCompleted', callback: Callback\<void>): void 5576 5577Subscribes to the video playback completion event. 5578 5579> **NOTE** 5580> 5581> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.on('stateChange')](#onstatechange9) instead. 5582 5583**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5584 5585**Parameters** 5586 5587| Name | Type | Mandatory| Description | 5588| -------- | -------- | ---- | ----------------------------------------------------------- | 5589| type | string | Yes | Event type, which is **'playbackCompleted'** in this case.| 5590| callback | function | Yes | Callback invoked when the event is triggered. | 5591 5592**Example** 5593 5594```ts 5595videoPlayer.on('playbackCompleted', () => { 5596 console.info('playbackCompleted success!'); 5597}); 5598``` 5599 5600### on('bufferingUpdate')<sup>(deprecated)</sup> 5601 5602on(type: 'bufferingUpdate', callback: (infoType: BufferingInfoType, value: number) => void): void 5603 5604Subscribes to the video buffering update event. Only network playback supports this subscription. 5605 5606> **NOTE** 5607> 5608> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.on('bufferingUpdate')](#onbufferingupdate9) instead. 5609 5610**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5611 5612**Parameters** 5613 5614| Name | Type | Mandatory| Description | 5615| -------- | -------- | ---- | ------------------------------------------------------------ | 5616| type | string | Yes | Event type, which is **'bufferingUpdate'** in this case. | 5617| 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**.| 5618 5619**Example** 5620 5621```ts 5622videoPlayer.on('bufferingUpdate', (infoType: media.BufferingInfoType, value: number) => { 5623 console.info('video bufferingInfo type: ' + infoType); 5624 console.info('video bufferingInfo value: ' + value); 5625}); 5626``` 5627 5628### on('startRenderFrame')<sup>(deprecated)</sup> 5629 5630on(type: 'startRenderFrame', callback: Callback\<void>): void 5631 5632Subscribes to the frame rendering start event. 5633 5634> **NOTE** 5635> 5636> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.on('startRenderFrame')](#onstartrenderframe9) instead. 5637 5638**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5639 5640**Parameters** 5641 5642| Name | Type | Mandatory| Description | 5643| -------- | --------------- | ---- | ------------------------------------------------------------ | 5644| type | string | Yes | Event type, which is **'startRenderFrame'** in this case.| 5645| callback | Callback\<void> | Yes | Callback invoked when the event is triggered. | 5646 5647**Example** 5648 5649```ts 5650videoPlayer.on('startRenderFrame', () => { 5651 console.info('startRenderFrame success!'); 5652}); 5653``` 5654 5655### on('videoSizeChanged')<sup>(deprecated)</sup> 5656 5657on(type: 'videoSizeChanged', callback: (width: number, height: number) => void): void 5658 5659Subscribes to the video width and height change event. 5660 5661> **NOTE** 5662> 5663> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.on('videoSizeChange')](#onvideosizechange9) instead. 5664 5665**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5666 5667**Parameters** 5668 5669| Name | Type | Mandatory| Description | 5670| -------- | -------- | ---- | ------------------------------------------------------------ | 5671| type | string | Yes | Event type, which is **'videoSizeChanged'** in this case.| 5672| callback | function | Yes | Callback invoked when the event is triggered. **width** indicates the video width, and **height** indicates the video height. | 5673 5674**Example** 5675 5676```ts 5677videoPlayer.on('videoSizeChanged', (width: number, height: number) => { 5678 console.info('video width is: ' + width); 5679 console.info('video height is: ' + height); 5680}); 5681``` 5682 5683### on('error')<sup>(deprecated)</sup> 5684 5685on(type: 'error', callback: ErrorCallback): void 5686 5687Subscribes to video playback error events. After an error event is reported, you must handle the event and exit the playback. 5688 5689> **NOTE** 5690> 5691> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayer.on('error')](#onerror9) instead. 5692 5693**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5694 5695**Parameters** 5696 5697| Name | Type | Mandatory| Description | 5698| -------- | ------------- | ---- | ------------------------------------------------------------ | 5699| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during video playback.| 5700| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 5701 5702**Example** 5703 5704```ts 5705videoPlayer.on('error', (error: BusinessError) => { // Set the 'error' event callback. 5706 console.info(`video error called, error: ${error}`); 5707}); 5708videoPlayer.url = 'fd://error'; // Set an incorrect URL to trigger the 'error' event. 5709``` 5710 5711## VideoPlayState<sup>(deprecated)</sup> 5712 5713Enumerates the video playback states. You can obtain the state through the **state** attribute. 5714 5715> **NOTE** 5716> 5717> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [AVPlayerState](#avplayerstate9) instead. 5718 5719**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 5720 5721| Name | Type | Description | 5722| -------- | ------ | -------------- | 5723| idle | string | The video player is idle.| 5724| prepared | string | Video playback is being prepared.| 5725| playing | string | Video playback is in progress.| 5726| paused | string | Video playback is paused.| 5727| stopped | string | Video playback is stopped.| 5728| error | string | Video playback is in the error state. | 5729 5730## AudioRecorder<sup>(deprecated)</sup> 5731 5732> **NOTE** 5733> 5734> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder](#avrecorder9) instead. 5735 5736Implements audio recording. Before calling any API of **AudioRecorder**, you must use [createAudioRecorder()](#mediacreateaudiorecorderdeprecated) to create an **AudioRecorder** instance. 5737 5738### prepare<sup>(deprecated)</sup> 5739 5740prepare(config: AudioRecorderConfig): void 5741 5742Prepares for recording. 5743 5744> **NOTE** 5745> 5746> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.prepare](#prepare9-2) instead. 5747 5748**Required permissions:** ohos.permission.MICROPHONE 5749 5750**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 5751 5752**Parameters** 5753 5754| Name| Type | Mandatory| Description | 5755| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ | 5756| config | [AudioRecorderConfig](#audiorecorderconfigdeprecated) | Yes | Audio recording parameters, including the audio output URI, encoding format, sampling rate, number of audio channels, and output format.| 5757 5758**Example** 5759 5760```ts 5761let audioRecorderConfig: media.AudioRecorderConfig = { 5762 audioEncoder : media.AudioEncoder.AAC_LC, 5763 audioEncodeBitRate : 22050, 5764 audioSampleRate : 22050, 5765 numberOfChannels : 2, 5766 format : media.AudioOutputFormat.AAC_ADTS, 5767 uri : 'fd://1', // The file must be created by the caller and granted with proper permissions. 5768 location : { latitude : 30, longitude : 130}, 5769} 5770audioRecorder.on('prepare', () => { // Set the 'prepare' event callback. 5771 console.info('prepare success'); 5772}); 5773audioRecorder.prepare(audioRecorderConfig); 5774``` 5775 5776### start<sup>(deprecated)</sup> 5777 5778start(): void 5779 5780Starts audio recording. This API can be called only after the **'prepare'** event is triggered. 5781 5782> **NOTE** 5783> 5784> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.start](#start9) instead. 5785 5786**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 5787 5788**Example** 5789 5790```ts 5791audioRecorder.on('start', () => { // Set the 'start' event callback. 5792 console.info('audio recorder start success'); 5793}); 5794audioRecorder.start(); 5795``` 5796 5797### pause<sup>(deprecated)</sup> 5798 5799pause():void 5800 5801Pauses audio recording. This API can be called only after the **'start'** event is triggered. 5802 5803> **NOTE** 5804> 5805> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.pause](#pause9-2) instead. 5806 5807**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 5808 5809**Example** 5810 5811```ts 5812audioRecorder.on('pause', () => { // Set the 'pause' event callback. 5813 console.info('audio recorder pause success'); 5814}); 5815audioRecorder.pause(); 5816``` 5817 5818### resume<sup>(deprecated)</sup> 5819 5820resume():void 5821 5822Resumes audio recording. This API can be called only after the **'pause'** event is triggered. 5823 5824> **NOTE** 5825> 5826> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.resume](#resume9) instead. 5827 5828**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 5829 5830**Example** 5831 5832```ts 5833audioRecorder.on('resume', () => { // Set the 'resume' event callback. 5834 console.info('audio recorder resume success'); 5835}); 5836audioRecorder.resume(); 5837``` 5838 5839### stop<sup>(deprecated)</sup> 5840 5841stop(): void 5842 5843Stops audio recording. 5844 5845> **NOTE** 5846> 5847> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.stop](#stop9-2) instead. 5848 5849**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 5850 5851**Example** 5852 5853```ts 5854audioRecorder.on('stop', () => { // Set the 'stop' event callback. 5855 console.info('audio recorder stop success'); 5856}); 5857audioRecorder.stop(); 5858``` 5859 5860### release<sup>(deprecated)</sup> 5861 5862release(): void 5863 5864Releases the audio recording resources. 5865 5866> **NOTE** 5867> 5868> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.release](#release9-2) instead. 5869 5870**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 5871 5872**Example** 5873 5874```ts 5875audioRecorder.on('release', () => { // Set the 'release' event callback. 5876 console.info('audio recorder release success'); 5877}); 5878audioRecorder.release(); 5879audioRecorder = undefined; 5880``` 5881 5882### reset<sup>(deprecated)</sup> 5883 5884reset(): void 5885 5886Resets audio recording. 5887 5888Before resetting audio recording, you must call **stop()** to stop recording. After audio recording is reset, you must call **prepare()** to set the recording configurations for another recording. 5889 5890> **NOTE** 5891> 5892> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.reset](#reset9-2) instead. 5893 5894**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 5895 5896**Example** 5897 5898```ts 5899audioRecorder.on('reset', () => { // Set the 'reset' event callback. 5900 console.info('audio recorder reset success'); 5901}); 5902audioRecorder.reset(); 5903``` 5904 5905### on('prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset')<sup>(deprecated)</sup> 5906 5907on(type: 'prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset', callback: () => void): void 5908 5909Subscribes to the audio recording events. 5910 5911> **NOTE** 5912> 5913> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.on('stateChange')](#onstatechange9-1) instead. 5914 5915**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 5916 5917**Parameters** 5918 5919| Name | Type | Mandatory| Description | 5920| -------- | -------- | ---- | ------------------------------------------------------------ | 5921| type | string | Yes | Event type. The following events are supported:<br>- 'prepare': triggered when the **prepare()** API is called and the audio recording parameters are set.<br>- 'start': triggered when the **start()** API is called and audio recording starts.<br>- 'pause': triggered when the **pause()** API is called and audio recording is paused.<br>- 'resume': triggered when the **resume()** API is called and audio recording is resumed.<br>- 'stop': triggered when the **stop()** API is called and audio recording stops.<br>- 'release': triggered when the **release()** API is called and the recording resources are released.<br>- 'reset': triggered when the **reset()** API is called and audio recording is reset.| 5922| callback | ()=>void | Yes | Callback invoked when the event is triggered. | 5923 5924**Example** 5925 5926```ts 5927let audioRecorder: media.AudioRecorder = media.createAudioRecorder(); // Create an AudioRecorder instance. 5928let audioRecorderConfig: media.AudioRecorderConfig = { 5929 audioEncoder : media.AudioEncoder.AAC_LC, 5930 audioEncodeBitRate : 22050, 5931 audioSampleRate : 22050, 5932 numberOfChannels : 2, 5933 format : media.AudioOutputFormat.AAC_ADTS, 5934 uri : 'fd://xx', // The file must be created by the caller and granted with proper permissions. 5935 location : { latitude : 30, longitude : 130}, 5936} 5937audioRecorder.on('error', (error: BusinessError) => { // Set the 'error' event callback. 5938 console.info(`audio error called, error: ${error}`); 5939}); 5940audioRecorder.on('prepare', () => { // Set the 'prepare' event callback. 5941 console.info('prepare success'); 5942 audioRecorder.start(); // Start recording and trigger the 'start' event callback. 5943}); 5944audioRecorder.on('start', () => { // Set the 'start' event callback. 5945 console.info('audio recorder start success'); 5946}); 5947audioRecorder.on('pause', () => { // Set the 'pause' event callback. 5948 console.info('audio recorder pause success'); 5949}); 5950audioRecorder.on('resume', () => { // Set the 'resume' event callback. 5951 console.info('audio recorder resume success'); 5952}); 5953audioRecorder.on('stop', () => { // Set the 'stop' event callback. 5954 console.info('audio recorder stop success'); 5955}); 5956audioRecorder.on('release', () => { // Set the 'release' event callback. 5957 console.info('audio recorder release success'); 5958}); 5959audioRecorder.on('reset', () => { // Set the 'reset' event callback. 5960 console.info('audio recorder reset success'); 5961}); 5962audioRecorder.prepare(audioRecorderConfig) // Set recording parameters and trigger the 'prepare' event callback. 5963``` 5964 5965### on('error')<sup>(deprecated)</sup> 5966 5967on(type: 'error', callback: ErrorCallback): void 5968 5969Subscribes to audio recording error events. After an error event is reported, you must handle the event and exit the recording. 5970 5971> **NOTE** 5972> 5973> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.on('error')](#onerror9-1) instead. 5974 5975**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 5976 5977**Parameters** 5978 5979| Name | Type | Mandatory| Description | 5980| -------- | ------------- | ---- | ------------------------------------------------------------ | 5981| type | string | Yes | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during audio recording.| 5982| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. | 5983 5984**Example** 5985 5986```ts 5987let audioRecorderConfig: media.AudioRecorderConfig = { 5988 audioEncoder : media.AudioEncoder.AAC_LC, 5989 audioEncodeBitRate : 22050, 5990 audioSampleRate : 22050, 5991 numberOfChannels : 2, 5992 format : media.AudioOutputFormat.AAC_ADTS, 5993 uri : 'fd://xx', // The file must be created by the caller and granted with proper permissions. 5994 location : { latitude : 30, longitude : 130}, 5995} 5996audioRecorder.on('error', (error: BusinessError) => { // Set the 'error' event callback. 5997 console.info(`audio error called, error: ${error}`); 5998}); 5999audioRecorder.prepare(audioRecorderConfig); // Do no set any parameter in prepare and trigger the 'error' event callback. 6000``` 6001 6002## AudioRecorderConfig<sup>(deprecated)</sup> 6003 6004> **NOTE** 6005> 6006> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorderConfig](#avrecorderconfig9) instead. 6007 6008Describes audio recording configurations. 6009 6010**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 6011 6012| Name | Type | Mandatory| Description | 6013| ----------------------------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 6014| 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.| 6015| audioEncodeBitRate | number | No | Audio encoding bit rate. The default value is **48000**. | 6016| audioSampleRate | number | No | Audio sampling rate. The default value is **48000**. | 6017| numberOfChannels | number | No | Number of audio channels. The default value is **2**. | 6018| 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.| 6019| location | [Location](#location) | No | Geographical location of the recorded audio. | 6020| 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.| 6021| audioEncoderMime<sup>8+</sup> | [CodecMimeType](#codecmimetype8) | No | Audio encoding format. | 6022| fileFormat<sup>8+</sup> | [ContainerFormatType](#containerformattype8) | No | Audio encoding format. | 6023 6024## AudioEncoder<sup>(deprecated)</sup> 6025 6026> **NOTE** 6027> 6028> This API is supported since API version 6 and deprecated since API version 8. You are advised to use [CodecMimeType](#codecmimetype8) instead. 6029 6030Enumerates the audio encoding formats. 6031 6032**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 6033 6034| Name | Value | Description | 6035| ------- | ---- | ------------------------------------------------------------ | 6036| DEFAULT | 0 | Default encoding format.<br>This API is defined but not implemented yet. | 6037| AMR_NB | 1 | AMR-NB.<br>This API is defined but not implemented yet.| 6038| AMR_WB | 2 | Adaptive Multi Rate-Wide Band Speech Codec (AMR-WB).<br>This API is defined but not implemented yet.| 6039| AAC_LC | 3 | Advanced Audio Coding Low Complexity (AAC-LC).| 6040| HE_AAC | 4 | High-Efficiency Advanced Audio Coding (HE_AAC).<br>This API is defined but not implemented yet.| 6041 6042## AudioOutputFormat<sup>(deprecated)</sup> 6043 6044> **NOTE** 6045> 6046> This API is supported since API version 6 and deprecated since API version 8. You are advised to use [ContainerFormatType](#containerformattype8) instead. 6047 6048Enumerates the audio output formats. 6049 6050**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 6051 6052| Name | Value | Description | 6053| -------- | ---- | ------------------------------------------------------------ | 6054| DEFAULT | 0 | Default encapsulation format.<br>This API is defined but not implemented yet. | 6055| MPEG_4 | 2 | MPEG-4. | 6056| AMR_NB | 3 | AMR_NB.<br>This API is defined but not implemented yet. | 6057| AMR_WB | 4 | AMR_WB.<br>This API is defined but not implemented yet. | 6058| AAC_ADTS | 6 | Audio Data Transport Stream (ADTS), which is a transport stream format of AAC-based audio.| 6059