1# Functions 2<!--Kit: Media Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @wang-haizhou6--> 5<!--Designer: @HmQQQ--> 6<!--Tester: @xchaosioda--> 7<!--Adviser: @zengyawen--> 8 9> **NOTE** 10> 11> 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. 12 13## Modules to Import 14 15```ts 16import { media } from '@kit.MediaKit'; 17``` 18 19## media.createAVPlayer<sup>9+</sup> 20 21createAVPlayer(callback: AsyncCallback\<AVPlayer>): void 22 23Creates an AVPlayer instance. This API uses an asynchronous callback to return the result. 24 25> **NOTE** 26> 27> - You are advised to create a maximum of 16 AVPlayer instances for an application in both audio and video playback scenarios.<!--Del--> 28> - 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, you are advised to create a maximum of 6 AVPlayer instances for an application in audio and video playback scenarios.<!--DelEnd--> 29> - Applications must properly manage AVPlayer instances according to their specific needs, creating and freeing them when necessary. Holding too many AVPlayer instances can lead to high memory usage, and in some cases, the system might terminate applications to free up resources. 30 31**Atomic service API**: This API can be used in atomic services since API version 11. 32 33**System capability**: SystemCapability.Multimedia.Media.AVPlayer 34 35**Parameters** 36 37| Name | Type | Mandatory| Description | 38| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 39| callback | AsyncCallback\<[AVPlayer](arkts-apis-media-AVPlayer.md)> | 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.| 40 41**Error codes** 42 43For details about the error codes, see [Media Error Codes](errorcode-media.md). 44 45| ID| Error Message | 46| -------- | ------------------------------ | 47| 5400101 | No memory. Return by callback. | 48 49**Example** 50 51```ts 52import { BusinessError } from '@kit.BasicServicesKit'; 53 54let avPlayer: media.AVPlayer; 55media.createAVPlayer((error: BusinessError, video: media.AVPlayer) => { 56 if (video != null) { 57 avPlayer = video; 58 console.info('Succeeded in creating AVPlayer'); 59 } else { 60 console.error(`Failed to create AVPlayer, error message:${error.message}`); 61 } 62}); 63``` 64 65## media.createAVPlayer<sup>9+</sup> 66 67createAVPlayer(): Promise\<AVPlayer> 68 69Creates an AVPlayer instance. This API uses a promise to return the result. 70 71> **NOTE** 72> 73> - You are advised to create a maximum of 16 AVPlayer instances for an application in both audio and video playback scenarios.<!--Del--> 74> - 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, you are advised to create a maximum of 6 AVPlayer instances for an application in audio and video playback scenarios.<!--DelEnd--> 75> - Applications must properly manage AVPlayer instances according to their specific needs, creating and freeing them when necessary. Holding too many AVPlayer instances can lead to high memory usage, and in some cases, the system might terminate applications to free up resources. 76 77**Atomic service API**: This API can be used in atomic services since API version 11. 78 79**System capability**: SystemCapability.Multimedia.Media.AVPlayer 80 81**Return value** 82 83| Type | Description | 84| ------------------------------- | ------------------------------------------------------------ | 85| Promise\<[AVPlayer](arkts-apis-media-AVPlayer.md)> | 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.| 86 87**Error codes** 88 89For details about the error codes, see [Media Error Codes](errorcode-media.md). 90 91| ID| Error Message | 92| -------- | ----------------------------- | 93| 5400101 | No memory. Return by promise. | 94 95**Example** 96 97```ts 98import { BusinessError } from '@kit.BasicServicesKit'; 99 100let avPlayer: media.AVPlayer; 101media.createAVPlayer().then((video: media.AVPlayer) => { 102 if (video != null) { 103 avPlayer = video; 104 console.info('Succeeded in creating AVPlayer'); 105 } else { 106 console.error('Failed to create AVPlayer'); 107 } 108}).catch((error: BusinessError) => { 109 console.error(`Failed to create AVPlayer, error message:${error.message}`); 110}); 111``` 112 113## media.createAVRecorder<sup>9+</sup> 114 115createAVRecorder(callback: AsyncCallback\<AVRecorder>): void 116 117Creates an AVRecorder instance. This API uses an asynchronous callback to return the result. 118 119> **NOTE** 120> 121> An application can create multiple AVRecorder instances. However, because the device shares a common audio channel, only one instance can record audio at a time. Any attempt to create the second instance for audio recording fails due to audio channel conflicts. 122 123**System capability**: SystemCapability.Multimedia.Media.AVRecorder 124 125**Parameters** 126 127| Name | Type | Mandatory| Description | 128| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 129| callback | AsyncCallback\<[AVRecorder](arkts-apis-media-AVRecorder.md)> | 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.| 130 131**Error codes** 132 133For details about the error codes, see [Media Error Codes](errorcode-media.md). 134 135| ID| Error Message | 136| -------- | ------------------------------ | 137| 5400101 | No memory. Return by callback. | 138 139**Example** 140 141```ts 142import { BusinessError } from '@kit.BasicServicesKit'; 143let avRecorder: media.AVRecorder; 144 145media.createAVRecorder((error: BusinessError, recorder: media.AVRecorder) => { 146 if (recorder != null) { 147 avRecorder = recorder; 148 console.info('Succeeded in creating AVRecorder'); 149 } else { 150 console.error(`Failed to create AVRecorder, error message:${error.message}`); 151 } 152}); 153``` 154 155## media.createAVRecorder<sup>9+</sup> 156 157createAVRecorder(): Promise\<AVRecorder> 158 159Creates an AVRecorder instance. This API uses a promise to return the result. 160 161> **NOTE** 162> 163> An application can create multiple AVRecorder instances. However, because the device shares a common audio channel, only one instance can record audio at a time. Any attempt to create the second instance for audio recording fails due to audio channel conflicts. 164 165**Atomic service API**: This API can be used in atomic services since API version 12. 166 167**System capability**: SystemCapability.Multimedia.Media.AVRecorder 168 169**Return value** 170 171| Type | Description | 172| ------------------------------------ | ------------------------------------------------------------ | 173| Promise\<[AVRecorder](arkts-apis-media-AVRecorder.md)> | 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.| 174 175**Error codes** 176 177For details about the error codes, see [Media Error Codes](errorcode-media.md). 178 179| ID| Error Message | 180| -------- | ----------------------------- | 181| 5400101 | No memory. Return by promise. | 182 183**Example** 184 185```ts 186import { BusinessError } from '@kit.BasicServicesKit'; 187let avRecorder: media.AVRecorder; 188media.createAVRecorder().then((recorder: media.AVRecorder) => { 189 if (recorder != null) { 190 avRecorder = recorder; 191 console.info('Succeeded in creating AVRecorder'); 192 } else { 193 console.error('Failed to create AVRecorder'); 194 } 195}).catch((error: BusinessError) => { 196 console.error(`Failed to create AVRecorder, error message:${error.message}`); 197}); 198``` 199 200## media.createAVTranscoder<sup>12+</sup> 201 202createAVTranscoder(): Promise\<AVTranscoder> 203 204Creates an AVTranscoder instance. This API uses a promise to return the result. 205 206> **NOTE** 207 208> A maximum of 2 AVTranscoder instances can be created. 209 210**System capability**: SystemCapability.Multimedia.Media.AVTranscoder 211 212**Return value** 213 214| Type | Description | 215| ------------------------------- | ------------------------------------------------------------ | 216| Promise\<[AVTranscoder](arkts-apis-media-AVTranscoder.md)> | Promise used to return the result. If the operation is successful, an AVTranscoder instance is returned; otherwise, **null** is returned. The instance can be used for video transcoding.| 217 218**Error codes** 219 220For details about the error codes, see [Media Error Codes](errorcode-media.md). 221 222| ID| Error Message | 223| -------- | ----------------------------- | 224| 5400101 | No memory. Return by promise. | 225 226**Example** 227 228```ts 229import { BusinessError } from '@kit.BasicServicesKit'; 230 231let avTranscoder: media.AVTranscoder | undefined = undefined; 232media.createAVTranscoder().then((transcoder: media.AVTranscoder) => { 233 if (transcoder != null) { 234 avTranscoder = transcoder; 235 console.info('Succeeded in creating AVTranscoder'); 236 } else { 237 console.error('Failed to create AVTranscoder'); 238 } 239}).catch((error: BusinessError) => { 240 console.error(`Failed to create AVTranscoder, error message:${error.message}`); 241}); 242``` 243 244## media.createAVMetadataExtractor<sup>11+</sup> 245 246createAVMetadataExtractor(callback: AsyncCallback\<AVMetadataExtractor>): void 247 248Creates an AVMetadataExtractor instance. This API uses an asynchronous callback to return the result. 249 250**System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor 251 252**Parameters** 253 254| Name | Type | Mandatory| Description | 255| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 256| callback | AsyncCallback\<[AVMetadataExtractor](arkts-apis-media-AVMetadataExtractor.md)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the AVMetadataExtractor instance created; otherwise, **err** is an error object.| 257 258**Error codes** 259 260For details about the error codes, see [Media Error Codes](errorcode-media.md). 261 262| ID| Error Message | 263| -------- | ------------------------------ | 264| 5400101 | No memory. Returned by callback. | 265 266**Example** 267 268```ts 269import { BusinessError } from '@kit.BasicServicesKit'; 270 271let avMetadataExtractor: media.AVMetadataExtractor; 272media.createAVMetadataExtractor((error: BusinessError, extractor: media.AVMetadataExtractor) => { 273 if (extractor != null) { 274 avMetadataExtractor = extractor; 275 console.info('Succeeded in creating AVMetadataExtractor'); 276 } else { 277 console.error(`Failed to create AVMetadataExtractor, error message:${error.message}`); 278 } 279}); 280``` 281 282## media.createAVMetadataExtractor<sup>11+</sup> 283 284createAVMetadataExtractor(): Promise\<AVMetadataExtractor> 285 286Creates an AVMetadataExtractor instance. This API uses a promise to return the result. 287 288**System capability**: SystemCapability.Multimedia.Media.AVMetadataExtractor 289 290**Return value** 291 292| Type | Description | 293| -------------- | ---------------------------------------- | 294| Promise\<[AVMetadataExtractor](arkts-apis-media-AVMetadataExtractor.md)> | Promise used to return the AVMetadataExtractor instance.| 295 296**Error codes** 297 298For details about the error codes, see [Media Error Codes](errorcode-media.md). 299 300| ID| Error Message | 301| -------- | ------------------------------ | 302| 5400101 | No memory. Returned by promise. | 303 304**Example** 305 306```ts 307import { BusinessError } from '@kit.BasicServicesKit'; 308 309let avMetadataExtractor: media.AVMetadataExtractor; 310media.createAVMetadataExtractor().then((extractor: media.AVMetadataExtractor) => { 311 if (extractor != null) { 312 avMetadataExtractor = extractor; 313 console.info('Succeeded in creating AVMetadataExtractor'); 314 } else { 315 console.error(`Failed to create AVMetadataExtractor`); 316 } 317}).catch((error: BusinessError) => { 318 console.error(`Failed to create AVMetadataExtractor, error message:${error.message}`); 319}); 320``` 321 322## media.createSoundPool<sup>10+</sup> 323 324createSoundPool(maxStreams: number, audioRenderInfo: audio.AudioRendererInfo, callback: AsyncCallback\<SoundPool>): void 325 326Creates a SoundPool instance. This API uses an asynchronous callback to return the result. 327 328> **NOTE** 329> 330> - In versions earlier than API version 18, the bottom layer of the created SoundPool object is in singleton mode. Therefore, an application process can create only one SoundPool instance. 331> - In API version 18 and later versions, the bottom layer of the created SoundPool object is in multiton mode. Therefore, an application process can create a maximum of 128 SoundPool instances. 332 333**System capability**: SystemCapability.Multimedia.Media.SoundPool 334 335**Parameters** 336 337| Name | Type | Mandatory| Description | 338| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 339| maxStreams | number | Yes | Maximum number of streams that can be played by the SoundPool instance. The value is an integer ranging from 1 to 32.| 340| audioRenderInfo | [audio.AudioRendererInfo](../apis-audio-kit/arkts-apis-audio-i.md#audiorendererinfo8) | Yes | Audio renderer parameters. When the **usage** parameter in **audioRenderInfo** is set to **STREAM_USAGE_UNKNOWN**, **STREAM_USAGE_MUSIC**, **STREAM_USAGE_MOVIE**, or **STREAM_USAGE_AUDIOBOOK**, the SoundPool uses the audio mixing mode when playing a short sound, without interrupting the playback of other audios.| 341| 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.| 342 343**Error codes** 344 345For details about the error codes, see [Media Error Codes](errorcode-media.md). 346 347| ID| Error Message | 348| -------- | ------------------------------ | 349| 5400101 | No memory. Return by callback. | 350 351**Example** 352 353```js 354import { audio } from '@kit.AudioKit'; 355 356let soundPool: media.SoundPool; 357let audioRendererInfo: audio.AudioRendererInfo = { 358 usage : audio.StreamUsage.STREAM_USAGE_MUSIC, 359 rendererFlags : 0 360}; 361 362media.createSoundPool(5, audioRendererInfo, (error, soundPool_: media.SoundPool) => { 363 if (error) { 364 console.error(`Failed to createSoundPool`); 365 return; 366 } else { 367 soundPool = soundPool_; 368 console.info(`Succeeded in createSoundPool`); 369 } 370}); 371``` 372 373## media.createSoundPool<sup>10+</sup> 374 375createSoundPool(maxStreams: number, audioRenderInfo: audio.AudioRendererInfo): Promise\<SoundPool> 376 377Creates a SoundPool instance. This API uses a promise to return the result. 378 379> **NOTE** 380> 381> - In versions earlier than API version 18, the bottom layer of the created SoundPool object is in singleton mode. Therefore, an application process can create only one SoundPool instance. 382> - In API version 18 and later versions, the bottom layer of the created SoundPool object is in multiton mode. Therefore, an application process can create a maximum of 128 SoundPool instances. 383 384**System capability**: SystemCapability.Multimedia.Media.SoundPool 385 386**Parameters** 387 388| Name | Type | Mandatory| Description | 389| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 390| maxStreams | number | Yes | Maximum number of streams that can be played by the SoundPool instance. The value is an integer ranging from 1 to 32.| 391| audioRenderInfo | [audio.AudioRendererInfo](../apis-audio-kit/arkts-apis-audio-i.md#audiorendererinfo8) | Yes | Audio renderer parameters.| 392 393**Return value** 394 395| Type | Description | 396| ----------------------------------------- | ------------------------------------------------------------ | 397| 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.| 398 399**Error codes** 400 401For details about the error codes, see [Media Error Codes](errorcode-media.md). 402 403| ID| Error Message | 404| -------- | ----------------------------- | 405| 5400101 | No memory. Return by promise. | 406 407**Example** 408 409```js 410import { audio } from '@kit.AudioKit'; 411import { BusinessError } from '@kit.BasicServicesKit'; 412 413let soundPool: media.SoundPool; 414let audioRendererInfo: audio.AudioRendererInfo = { 415 usage : audio.StreamUsage.STREAM_USAGE_MUSIC, 416 rendererFlags : 0 417}; 418 419media.createSoundPool(5, audioRendererInfo).then((soundpool_: media.SoundPool) => { 420 if (soundpool_ != null) { 421 soundPool = soundpool_; 422 console.info('Succeeded in creating SoundPool'); 423 } else { 424 console.error('Failed to create SoundPool'); 425 } 426}, (error: BusinessError) => { 427 console.error(`soundpool catchCallback, error message:${error.message}`); 428}); 429``` 430 431## media.createAVScreenCaptureRecorder<sup>12+</sup> 432 433createAVScreenCaptureRecorder(): Promise\<AVScreenCaptureRecorder> 434 435Creates an AVScreenCaptureRecorder instance. This API uses a promise to return the result. 436 437**System capability**: SystemCapability.Multimedia.Media.AVScreenCapture 438 439**Return value** 440 441| Type | Description | 442| ------------------------------------------------------------ | ------------------------------------------------------------ | 443| Promise\<[AVScreenCaptureRecorder](arkts-apis-media-AVScreenCaptureRecorder.md)> | Promise used to return the result. If the operation is successful, an AVScreenCaptureRecorder instance is returned; otherwise, **null** is returned. The instance can be used for screen capture.| 444 445**Error codes** 446 447| ID| Error Message | 448| -------- | ------------------------------ | 449| 5400101 | No memory. Return by promise. | 450 451**Example** 452 453```ts 454import { BusinessError } from '@kit.BasicServicesKit'; 455 456let avScreenCaptureRecorder: media.AVScreenCaptureRecorder; 457media.createAVScreenCaptureRecorder().then((captureRecorder: media.AVScreenCaptureRecorder) => { 458 if (captureRecorder != null) { 459 avScreenCaptureRecorder = captureRecorder; 460 console.info('Succeeded in createAVScreenCaptureRecorder'); 461 } else { 462 console.error('Failed to createAVScreenCaptureRecorder'); 463 } 464}).catch((error: BusinessError) => { 465 console.error(`createAVScreenCaptureRecorder catchCallback, error message:${error.message}`); 466}); 467``` 468 469## media.createAVImageGenerator<sup>12+</sup> 470 471createAVImageGenerator(callback: AsyncCallback\<AVImageGenerator>): void 472 473Creates an AVImageGenerator instance. This API uses an asynchronous callback to return the result. 474 475**System capability**: SystemCapability.Multimedia.Media.AVImageGenerator 476 477**Parameters** 478 479| Name | Type | Mandatory| Description | 480| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 481| callback | AsyncCallback\<[AVImageGenerator](arkts-apis-media-AVImageGenerator.md)> | Yes | Callback used to return the result. If the operation is successful, an AVImageGenerator instance is returned; otherwise, **null** is returned. The API can be used to obtain a video thumbnail.| 482 483**Error codes** 484 485For details about the error codes, see [Media Error Codes](errorcode-media.md). 486 487| ID| Error Message | 488| -------- | ------------------------------ | 489| 5400101 | No memory. Returned by callback. | 490 491**Example** 492 493```ts 494import { BusinessError } from '@kit.BasicServicesKit'; 495 496let avImageGenerator: media.AVImageGenerator; 497media.createAVImageGenerator((error: BusinessError, generator: media.AVImageGenerator) => { 498 if (generator != null) { 499 avImageGenerator = generator; 500 console.info('Succeeded in creating AVImageGenerator'); 501 } else { 502 console.error(`Failed to creat AVImageGenerator, error message:${error.message}`); 503 } 504}); 505``` 506 507## media.createAVImageGenerator<sup>12+</sup> 508 509createAVImageGenerator(): Promise\<AVImageGenerator> 510 511Creates an AVImageGenerator instance. This API uses a promise to return the result. 512 513**System capability**: SystemCapability.Multimedia.Media.AVImageGenerator 514 515**Return value** 516 517| Type | Description | 518| ------------------------------- | ------------------------------------------------------------ | 519| Promise\<[AVImageGenerator](arkts-apis-media-AVImageGenerator.md)> | Promise used to return the result. If the operation is successful, an AVImageGenerator instance is returned; otherwise, **null** is returned. The API can be used to obtain a video thumbnail.| 520 521**Error codes** 522 523For details about the error codes, see [Media Error Codes](errorcode-media.md). 524 525| ID| Error Message | 526| -------- | ----------------------------- | 527| 5400101 | No memory. Returned by promise. | 528 529**Example** 530 531```ts 532import { BusinessError } from '@kit.BasicServicesKit'; 533 534let avImageGenerator: media.AVImageGenerator; 535media.createAVImageGenerator().then((generator: media.AVImageGenerator) => { 536 if (generator != null) { 537 avImageGenerator = generator; 538 console.info('Succeeded in creating AVImageGenerator'); 539 } else { 540 console.error('Failed to creat AVImageGenerator'); 541 } 542}).catch((error: BusinessError) => { 543 console.error(`Failed to creat AVImageGenerator, error message:${error.message}`); 544}); 545``` 546 547## media.createMediaSourceWithUrl<sup>12+</sup> 548 549createMediaSourceWithUrl(url: string, headers?: Record\<string, string>): MediaSource 550 551Creates a media source for streaming media to be pre-downloaded. 552 553**Atomic service API**: This API can be used in atomic services since API version 13. 554 555**System capability**: SystemCapability.Multimedia.Media.Core 556 557**Parameters** 558 559| Name | Type | Mandatory| Description | 560| -------- | -------- | ---- | -------------------- | 561| url | string | Yes | - URL of the media source. The following streaming media formats are supported: HLS, HTTP-FLV, DASH, and HTTPS.<br> - FD path of the local M3U8 file. | 562| headers | Record\<string, string> | No | HTTP header customized for streaming media pre-download.| 563 564**Return value** 565 566| Type | Description | 567| -------------- | ------------------------------------------ | 568| [MediaSource](arkts-apis-media-MediaSource.md) | MediaSource instance.| 569 570**Error codes** 571 572For details about the error codes, see [Media Error Codes](errorcode-media.md). 573 574| ID| Error Message | 575| -------- | ----------------------------------------- | 576| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 577| 5400101 | No memory. | 578 579**Example 1** 580 581```ts 582let headers: Record<string, string> = {"User-Agent" : "User-Agent-Value"}; 583let mediaSource : media.MediaSource = media.createMediaSourceWithUrl("http://xxx", headers); 584``` 585 586**Example 2** 587 588<!--code_no_check--> 589```ts 590import { media } from "@kit.MediaKit"; 591 592async function test(context: Context){ 593 // this.getUIContext().getHostContext(); 594 let mgr = context?.resourceManager; 595 if (!mgr) { 596 return; 597 } 598 let fileDescriptor = await mgr.getRawFd("xxx.m3u8"); 599 600 let fd: string = fileDescriptor.fd.toString(); 601 let offset: string = fileDescriptor.offset.toString(); 602 let length: string = fileDescriptor.length.toString(); 603 let fdUrl: string = "fd://" + fd + "?offset=" + offset + "&size=" + length; 604 605 let mediaSource: media.MediaSource = media.createMediaSourceWithUrl(fdUrl); 606} 607 608``` 609 610## media.createMediaSourceWithStreamData<sup>19+</sup> 611 612createMediaSourceWithStreamData(streams: Array\<MediaStream>): MediaSource 613 614Creates a multi-bitrate media source for streaming media. Currently, only the HTTP-FLV multi-bitrate media source is supported. 615 616**Atomic service API**: This API can be used in atomic services since API version 19. 617 618**System capability**: SystemCapability.Multimedia.Media.Core 619 620**Parameters** 621 622| Name | Type | Mandatory| Description | 623| ------- | ------------------------------------ | ---- | ----------------------------------------------------- | 624| streams | Array<[MediaStream](arkts-apis-media-i.md#mediastream19)> | Yes| Array of MediaStream objects. The supported streaming media format is HTTP-FLV.| 625 626**Return value** 627 628| Type | Description | 629| ----------------------------- | ------------------- | 630| [MediaSource](arkts-apis-media-MediaSource.md) | MediaSource instance.| 631 632**Example** 633 634```ts 635let streams : Array<media.MediaStream> = []; 636streams.push({url: "http://xxx/480p.flv", width: 854, height: 480, bitrate: 800000}); 637streams.push({url: "http://xxx/720p.flv", width: 1280, height: 720, bitrate: 2000000}); 638streams.push({url: "http://xxx/1080p.flv", width: 1920, height: 1080, bitrate: 2000000}); 639let mediaSource : media.MediaSource = media.createMediaSourceWithStreamData(streams); 640``` 641 642## media.createAudioPlayer<sup>(deprecated)</sup> 643 644createAudioPlayer(): AudioPlayer 645 646Creates an AudioPlayer instance in synchronous mode. 647 648> **NOTE** 649> 650> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [createAVPlayer](#mediacreateavplayer9) instead. 651 652**System capability**: SystemCapability.Multimedia.Media.AudioPlayer 653 654**Return value** 655 656| Type | Description | 657| --------------------------- | ------------------------------------------------------------ | 658| [AudioPlayer](arkts-apis-media-AudioPlayer.md) | 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.| 659 660**Example** 661 662```ts 663let audioPlayer: media.AudioPlayer = media.createAudioPlayer(); 664``` 665 666## media.createVideoPlayer<sup>(deprecated)</sup> 667 668createVideoPlayer(callback: AsyncCallback\<VideoPlayer>): void 669 670Creates a VideoPlayer instance. This API uses an asynchronous callback to return the result. 671 672> **NOTE** 673> 674> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [createAVPlayer](#mediacreateavplayer9) instead. 675 676**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 677 678**Parameters** 679 680| Name | Type | Mandatory| Description | 681| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 682| callback | AsyncCallback<[VideoPlayer](arkts-apis-media-VideoPlayer.md)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the VideoPlayer instance created; otherwise, **err** is an error object.| 683 684**Example** 685 686```ts 687import { BusinessError } from '@kit.BasicServicesKit'; 688 689let videoPlayer: media.VideoPlayer; 690media.createVideoPlayer((error: BusinessError, video: media.VideoPlayer) => { 691 if (video != null) { 692 videoPlayer = video; 693 console.info('Succeeded in creating VideoPlayer'); 694 } else { 695 console.error(`Failed to create VideoPlayer, error:${error}`); 696 } 697}); 698``` 699 700## media.createVideoPlayer<sup>(deprecated)</sup> 701 702createVideoPlayer(): Promise\<VideoPlayer> 703 704Creates a VideoPlayer instance. This API uses a promise to return the result. 705 706> **NOTE** 707> 708> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [createAVPlayer](#mediacreateavplayer9-1) instead. 709 710**System capability**: SystemCapability.Multimedia.Media.VideoPlayer 711 712**Return value** 713 714| Type | Description | 715| ------------------------------------ | ------------------------------------------------------------ | 716| Promise<[VideoPlayer](arkts-apis-media-VideoPlayer.md)> | 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.| 717 718**Example** 719 720```ts 721import { BusinessError } from '@kit.BasicServicesKit'; 722 723let videoPlayer: media.VideoPlayer; 724media.createVideoPlayer().then((video: media.VideoPlayer) => { 725 if (video != null) { 726 videoPlayer = video; 727 console.info('Succeeded in creating VideoPlayer'); 728 } else { 729 console.error('Failed to create VideoPlayer'); 730 } 731}).catch((error: BusinessError) => { 732 console.error(`Failed to create VideoPlayer, error:${error}`); 733}); 734``` 735 736## media.createAudioRecorder<sup>(deprecated)</sup> 737 738createAudioRecorder(): AudioRecorder 739 740Creates an AudioRecorder instance to control audio recording. 741Only one AudioRecorder instance can be created per device. 742 743> **NOTE** 744> 745> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [createAVRecorder](#mediacreateavrecorder9) instead. 746 747**System capability**: SystemCapability.Multimedia.Media.AudioRecorder 748 749**Return value** 750 751| Type | Description | 752| ------------------------------- | ------------------------------------------------------------ | 753| [AudioRecorder](arkts-apis-media-AudioRecorder.md) | If the operation is successful, an AudioRecorder instance is returned; otherwise, **null** is returned. The instance can be used to record audio.| 754 755**Example** 756 757```ts 758let audioRecorder: media.AudioRecorder = media.createAudioRecorder(); 759``` 760