1# Interface (AudioRenderer) 2 3> **NOTE** 4> 5> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 6> - The initial APIs of this interface are supported since API version 8. 7 8This interface provides APIs for audio rendering. 9 10Before calling any API in AudioRenderer, you must use [createAudioRenderer](arkts-apis-audio-f.md#audiocreateaudiorenderer8) to create an AudioRenderer instance. 11 12## Modules to Import 13 14```ts 15import { audio } from '@kit.AudioKit'; 16``` 17 18## Properties 19 20**System capability**: SystemCapability.Multimedia.Audio.Renderer 21 22| Name | Type | Read-Only| Optional| Description | 23| ----- | -------------------------- | ---- | ---- | ------------------ | 24| state<sup>8+</sup> | [AudioState](arkts-apis-audio-e.md#audiostate8) | Yes | No | Audio renderer state.| 25 26**Example** 27 28```ts 29import { audio } from '@kit.AudioKit'; 30 31let state: audio.AudioState = audioRenderer.state; 32``` 33 34## getRendererInfo<sup>8+</sup> 35 36getRendererInfo(callback: AsyncCallback<AudioRendererInfo\>): void 37 38Obtains the information about this audio renderer. This API uses an asynchronous callback to return the result. 39 40**System capability**: SystemCapability.Multimedia.Audio.Renderer 41 42**Parameters** 43 44| Name | Type | Mandatory| Description | 45| :------- | :------------------------------------------------------- | :--- | :--------------------- | 46| callback | AsyncCallback<[AudioRendererInfo](arkts-apis-audio-i.md#audiorendererinfo8)\> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the audio renderer information obtained; otherwise, **err** is an error object.| 47 48**Example** 49 50```ts 51import { BusinessError } from '@kit.BasicServicesKit'; 52 53audioRenderer.getRendererInfo((err: BusinessError, rendererInfo: audio.AudioRendererInfo) => { 54 console.info('Renderer GetRendererInfo:'); 55 console.info(`Renderer content: ${rendererInfo.content}`); 56 console.info(`Renderer usage: ${rendererInfo.usage}`); 57 console.info(`Renderer flags: ${rendererInfo.rendererFlags}`); 58}); 59``` 60 61## getRendererInfo<sup>8+</sup> 62 63getRendererInfo(): Promise<AudioRendererInfo\> 64 65Obtains the information about this audio renderer. This API uses a promise to return the result. 66 67**System capability**: SystemCapability.Multimedia.Audio.Renderer 68 69**Return value** 70 71| Type | Description | 72| -------------------------------------------------- | ------------------------------- | 73| Promise<[AudioRendererInfo](arkts-apis-audio-i.md#audiorendererinfo8)\> | Promise used to return the audio renderer information.| 74 75**Example** 76 77```ts 78import { BusinessError } from '@kit.BasicServicesKit'; 79 80audioRenderer.getRendererInfo().then((rendererInfo: audio.AudioRendererInfo) => { 81 console.info('Renderer GetRendererInfo:'); 82 console.info(`Renderer content: ${rendererInfo.content}`); 83 console.info(`Renderer usage: ${rendererInfo.usage}`); 84 console.info(`Renderer flags: ${rendererInfo.rendererFlags}`) 85}).catch((err: BusinessError) => { 86 console.error(`AudioFrameworkRenderLog: RendererInfo :ERROR: ${err}`); 87}); 88``` 89 90## getRendererInfoSync<sup>10+</sup> 91 92getRendererInfoSync(): AudioRendererInfo 93 94Obtains the information about this audio renderer. This API returns the result synchronously. 95 96**System capability**: SystemCapability.Multimedia.Audio.Renderer 97 98**Return value** 99 100| Type | Description | 101| -------------------------------------------------- | ------------------------------- | 102| [AudioRendererInfo](arkts-apis-audio-i.md#audiorendererinfo8) | Audio renderer information.| 103 104**Example** 105 106```ts 107import { BusinessError } from '@kit.BasicServicesKit'; 108 109try { 110 let rendererInfo: audio.AudioRendererInfo = audioRenderer.getRendererInfoSync(); 111 console.info(`Renderer content: ${rendererInfo.content}`); 112 console.info(`Renderer usage: ${rendererInfo.usage}`); 113 console.info(`Renderer flags: ${rendererInfo.rendererFlags}`) 114} catch (err) { 115 let error = err as BusinessError; 116 console.error(`AudioFrameworkRenderLog: RendererInfo :ERROR: ${error}`); 117} 118``` 119 120## getStreamInfo<sup>8+</sup> 121 122getStreamInfo(callback: AsyncCallback<AudioStreamInfo\>): void 123 124Obtains the stream information of this audio renderer. This API uses an asynchronous callback to return the result. 125 126**System capability**: SystemCapability.Multimedia.Audio.Renderer 127 128**Parameters** 129 130| Name | Type | Mandatory| Description | 131| :------- | :--------------------------------------------------- | :--- | :------------------- | 132| callback | AsyncCallback<[AudioStreamInfo](arkts-apis-audio-i.md#audiostreaminfo8)\> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the stream information obtained; otherwise, **err** is an error object.| 133 134**Example** 135 136```ts 137import { BusinessError } from '@kit.BasicServicesKit'; 138 139audioRenderer.getStreamInfo((err: BusinessError, streamInfo: audio.AudioStreamInfo) => { 140 console.info('Renderer GetStreamInfo:'); 141 console.info(`Renderer sampling rate: ${streamInfo.samplingRate}`); 142 console.info(`Renderer channel: ${streamInfo.channels}`); 143 console.info(`Renderer format: ${streamInfo.sampleFormat}`); 144 console.info(`Renderer encoding type: ${streamInfo.encodingType}`); 145}); 146``` 147 148## getStreamInfo<sup>8+</sup> 149 150getStreamInfo(): Promise<AudioStreamInfo\> 151 152Obtains the stream information of this audio renderer. This API uses a promise to return the result. 153 154**System capability**: SystemCapability.Multimedia.Audio.Renderer 155 156**Return value** 157 158| Type | Description | 159| :--------------------------------------------- | :--------------------- | 160| Promise<[AudioStreamInfo](arkts-apis-audio-i.md#audiostreaminfo8)\> | Promise used to return the stream information.| 161 162**Example** 163 164```ts 165import { BusinessError } from '@kit.BasicServicesKit'; 166 167audioRenderer.getStreamInfo().then((streamInfo: audio.AudioStreamInfo) => { 168 console.info('Renderer GetStreamInfo:'); 169 console.info(`Renderer sampling rate: ${streamInfo.samplingRate}`); 170 console.info(`Renderer channel: ${streamInfo.channels}`); 171 console.info(`Renderer format: ${streamInfo.sampleFormat}`); 172 console.info(`Renderer encoding type: ${streamInfo.encodingType}`); 173}).catch((err: BusinessError) => { 174 console.error(`ERROR: ${err}`); 175}); 176``` 177 178## getStreamInfoSync<sup>10+</sup> 179 180getStreamInfoSync(): AudioStreamInfo 181 182Obtains the stream information of this audio renderer. This API returns the result synchronously. 183 184**System capability**: SystemCapability.Multimedia.Audio.Renderer 185 186**Return value** 187 188| Type | Description | 189| :--------------------------------------------- | :--------------------- | 190| [AudioStreamInfo](arkts-apis-audio-i.md#audiostreaminfo8) | Stream information.| 191 192**Example** 193 194```ts 195import { BusinessError } from '@kit.BasicServicesKit'; 196 197try { 198 let streamInfo: audio.AudioStreamInfo = audioRenderer.getStreamInfoSync(); 199 console.info(`Renderer sampling rate: ${streamInfo.samplingRate}`); 200 console.info(`Renderer channel: ${streamInfo.channels}`); 201 console.info(`Renderer format: ${streamInfo.sampleFormat}`); 202 console.info(`Renderer encoding type: ${streamInfo.encodingType}`); 203} catch (err) { 204 let error = err as BusinessError; 205 console.error(`ERROR: ${error}`); 206} 207``` 208 209## getAudioStreamId<sup>9+</sup> 210 211getAudioStreamId(callback: AsyncCallback<number\>): void 212 213Obtains the stream ID of this audio renderer. This API uses an asynchronous callback to return the result. 214 215**System capability**: SystemCapability.Multimedia.Audio.Renderer 216 217**Parameters** 218 219| Name | Type | Mandatory| Description | 220| :------- | :--------------------------------------------------- | :--- | :------------------- | 221| callback | AsyncCallback<number\> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the stream ID obtained; otherwise, **err** is an error object.| 222 223**Example** 224 225```ts 226import { BusinessError } from '@kit.BasicServicesKit'; 227 228audioRenderer.getAudioStreamId((err: BusinessError, streamId: number) => { 229 console.info(`Renderer GetStreamId: ${streamId}`); 230}); 231``` 232 233## getAudioStreamId<sup>9+</sup> 234 235getAudioStreamId(): Promise<number\> 236 237Obtains the stream ID of this audio renderer. This API uses a promise to return the result. 238 239**System capability**: SystemCapability.Multimedia.Audio.Renderer 240 241**Return value** 242 243| Type | Description | 244| :--------------------------------------------- | :--------------------- | 245| Promise<number\> | Promise used to return the stream ID.| 246 247**Example** 248 249```ts 250import { BusinessError } from '@kit.BasicServicesKit'; 251 252audioRenderer.getAudioStreamId().then((streamId: number) => { 253 console.info(`Renderer getAudioStreamId: ${streamId}`); 254}).catch((err: BusinessError) => { 255 console.error(`ERROR: ${err}`); 256}); 257``` 258 259## getAudioStreamIdSync<sup>10+</sup> 260 261getAudioStreamIdSync(): number 262 263Obtains the stream ID of this audio renderer. This API returns the result synchronously. 264 265**System capability**: SystemCapability.Multimedia.Audio.Renderer 266 267**Return value** 268 269| Type | Description | 270| :--------------------------------------------- | :--------------------- | 271| number | Stream ID.| 272 273**Example** 274 275```ts 276import { BusinessError } from '@kit.BasicServicesKit'; 277 278try { 279 let streamId: number = audioRenderer.getAudioStreamIdSync(); 280 console.info(`Renderer getAudioStreamIdSync: ${streamId}`); 281} catch (err) { 282 let error = err as BusinessError; 283 console.error(`ERROR: ${error}`); 284} 285``` 286 287## setAudioEffectMode<sup>10+</sup> 288 289setAudioEffectMode(mode: AudioEffectMode, callback: AsyncCallback\<void>): void 290 291Sets an audio effect mode. This API uses an asynchronous callback to return the result. 292 293**System capability**: SystemCapability.Multimedia.Audio.Renderer 294 295**Parameters** 296 297| Name | Type | Mandatory| Description | 298| -------- | ---------------------------------------- | ---- | ------------------------ | 299| mode | [AudioEffectMode](arkts-apis-audio-e.md#audioeffectmode10) | Yes | Audio effect mode to set. | 300| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 301 302**Error codes** 303 304For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 305 306| ID| Error Message| 307| ------- | ----------------------------------------------| 308| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 309| 6800101 | Parameter verification failed. Return by callback. | 310 311**Example** 312 313```ts 314import { BusinessError } from '@kit.BasicServicesKit'; 315 316audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_DEFAULT, (err: BusinessError) => { 317 if (err) { 318 console.error('Failed to set params'); 319 } else { 320 console.info('Callback invoked to indicate a successful audio effect mode setting.'); 321 } 322}); 323``` 324 325## setAudioEffectMode<sup>10+</sup> 326 327setAudioEffectMode(mode: AudioEffectMode): Promise\<void> 328 329Sets an audio effect mode. This API uses a promise to return the result. 330 331**System capability**: SystemCapability.Multimedia.Audio.Renderer 332 333**Parameters** 334 335| Name| Type | Mandatory| Description | 336| ------ | ---------------------------------------- | ---- | ------------ | 337| mode | [AudioEffectMode](arkts-apis-audio-e.md#audioeffectmode10) | Yes | Audio effect mode to set.| 338 339**Return value** 340 341| Type | Description | 342| -------------- | ------------------------- | 343| Promise\<void> | Promise that returns no value.| 344 345**Error codes** 346 347For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 348 349| ID| Error Message| 350| ------- | ---------------------------------------------| 351| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 352| 6800101 | Parameter verification failed. Return by promise. | 353 354**Example** 355 356```ts 357import { BusinessError } from '@kit.BasicServicesKit'; 358 359audioRenderer.setAudioEffectMode(audio.AudioEffectMode.EFFECT_DEFAULT).then(() => { 360 console.info('setAudioEffectMode SUCCESS'); 361}).catch((err: BusinessError) => { 362 console.error(`ERROR: ${err}`); 363}); 364``` 365 366## getAudioEffectMode<sup>10+</sup> 367 368getAudioEffectMode(callback: AsyncCallback\<AudioEffectMode>): void 369 370Obtains the audio effect mode in use. This API uses an asynchronous callback to return the result. 371 372**System capability**: SystemCapability.Multimedia.Audio.Renderer 373 374**Parameters** 375 376| Name | Type | Mandatory| Description | 377| -------- | ------------------------------------------------------- | ---- | ------------------ | 378| callback | AsyncCallback<[AudioEffectMode](arkts-apis-audio-e.md#audioeffectmode10)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the audio effect mode obtained; otherwise, **err** is an error object.| 379 380**Example** 381 382```ts 383import { BusinessError } from '@kit.BasicServicesKit'; 384 385audioRenderer.getAudioEffectMode((err: BusinessError, effectMode: audio.AudioEffectMode) => { 386 if (err) { 387 console.error('Failed to get params'); 388 } else { 389 console.info(`getAudioEffectMode: ${effectMode}`); 390 } 391}); 392``` 393 394## getAudioEffectMode<sup>10+</sup> 395 396getAudioEffectMode(): Promise\<AudioEffectMode> 397 398Obtains the audio effect mode in use. This API uses a promise to return the result. 399 400**System capability**: SystemCapability.Multimedia.Audio.Renderer 401 402**Return value** 403 404| Type | Description | 405| ------------------------------------------------- | ------------------------- | 406| Promise<[AudioEffectMode](arkts-apis-audio-e.md#audioeffectmode10)> | Promise used to return the audio effect mode.| 407 408**Example** 409 410```ts 411import { BusinessError } from '@kit.BasicServicesKit'; 412 413audioRenderer.getAudioEffectMode().then((effectMode: audio.AudioEffectMode) => { 414 console.info(`getAudioEffectMode: ${effectMode}`); 415}).catch((err: BusinessError) => { 416 console.error(`ERROR: ${err}`); 417}); 418``` 419 420## start<sup>8+</sup> 421 422start(callback: AsyncCallback<void\>): void 423 424Starts this audio renderer. This API uses an asynchronous callback to return the result. 425 426**System capability**: SystemCapability.Multimedia.Audio.Renderer 427 428**Parameters** 429 430| Name | Type | Mandatory| Description | 431| -------- | -------------------- | ---- | ---------- | 432| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object. If the operation fails, an error object with one of the following error codes is returned:<br>Error code 6800301: indicates abnormal status, focus preemption failure, and abnormal system processing. For details, see system logs.| 433 434**Example** 435 436```ts 437import { BusinessError } from '@kit.BasicServicesKit'; 438 439audioRenderer.start((err: BusinessError) => { 440 if (err) { 441 console.error('Renderer start failed.'); 442 } else { 443 console.info('Renderer start success.'); 444 } 445}); 446``` 447 448## start<sup>8+</sup> 449 450start(): Promise<void\> 451 452Starts this audio renderer. This API uses a promise to return the result. 453 454**System capability**: SystemCapability.Multimedia.Audio.Renderer 455 456**Return value** 457 458| Type | Description | 459| -------------- | ------------------------- | 460| Promise\<void> | Promise object, which indicates that the renderer is started successfully. If the operation fails, an error object with one of the following error codes is returned:<br>Error code 6800301: indicates abnormal status, focus preemption failure, and abnormal system processing. For details, see system logs.| 461 462**Example** 463 464```ts 465import { BusinessError } from '@kit.BasicServicesKit'; 466 467audioRenderer.start().then(() => { 468 console.info('Renderer started'); 469}).catch((err: BusinessError) => { 470 console.error(`ERROR: ${err}`); 471}); 472``` 473 474## pause<sup>8+</sup> 475 476pause(callback: AsyncCallback\<void>): void 477 478Pauses this audio renderer. This API uses an asynchronous callback to return the result. 479 480**System capability**: SystemCapability.Multimedia.Audio.Renderer 481 482**Parameters** 483 484| Name | Type | Mandatory| Description | 485| -------- | -------------------- | ---- | ---------------- | 486| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 487 488**Example** 489 490```ts 491import { BusinessError } from '@kit.BasicServicesKit'; 492 493audioRenderer.pause((err: BusinessError) => { 494 if (err) { 495 console.error('Renderer pause failed'); 496 } else { 497 console.info('Renderer paused.'); 498 } 499}); 500``` 501 502## pause<sup>8+</sup> 503 504pause(): Promise\<void> 505 506Pauses this audio renderer. This API uses a promise to return the result. 507 508**System capability**: SystemCapability.Multimedia.Audio.Renderer 509 510**Return value** 511 512| Type | Description | 513| -------------- | ------------------------- | 514| Promise\<void> | Promise that returns no value.| 515 516**Example** 517 518```ts 519import { BusinessError } from '@kit.BasicServicesKit'; 520 521audioRenderer.pause().then(() => { 522 console.info('Renderer paused'); 523}).catch((err: BusinessError) => { 524 console.error(`ERROR: ${err}`); 525}); 526``` 527 528## drain<sup>8+</sup> 529 530drain(callback: AsyncCallback\<void>): void 531 532Drains the playback buffer. This API uses an asynchronous callback to return the result. 533 534**System capability**: SystemCapability.Multimedia.Audio.Renderer 535 536**Parameters** 537 538| Name | Type | Mandatory| Description | 539| -------- | -------------------- | ---- | ---------------- | 540| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 541 542**Example** 543 544```ts 545import { BusinessError } from '@kit.BasicServicesKit'; 546 547audioRenderer.drain((err: BusinessError) => { 548 if (err) { 549 console.error('Renderer drain failed'); 550 } else { 551 console.info('Renderer drained.'); 552 } 553}); 554``` 555 556## drain<sup>8+</sup> 557 558drain(): Promise\<void> 559 560Drains the playback buffer. This API uses a promise to return the result. 561 562**System capability**: SystemCapability.Multimedia.Audio.Renderer 563 564**Return value** 565 566| Type | Description | 567| -------------- | ------------------------- | 568| Promise\<void> | Promise that returns no value.| 569 570**Example** 571 572```ts 573import { BusinessError } from '@kit.BasicServicesKit'; 574 575audioRenderer.drain().then(() => { 576 console.info('Renderer drained successfully'); 577}).catch((err: BusinessError) => { 578 console.error(`ERROR: ${err}`); 579}); 580``` 581 582## flush<sup>11+</sup> 583 584flush(): Promise\<void> 585 586Flushes the buffer. This API is available when [AudioState](arkts-apis-audio-e.md#audiostate8) is **STATE_RUNNING**, **STATE_PAUSED**, or **STATE_STOPPED**. This API uses a promise to return the result. 587 588**System capability**: SystemCapability.Multimedia.Audio.Renderer 589 590**Return value** 591 592| Type | Description | 593| -------------- | ------------------------- | 594| Promise\<void> | Promise that returns no value.| 595 596**Error codes** 597 598For details about the error codes, see [Audio Error Codes](errorcode-audio.md). 599 600| ID| Error Message| 601| ------- | --------------------------------------------| 602| 6800103 | Operation not permit at current state. Return by promise. | 603 604**Example** 605 606```ts 607import { BusinessError } from '@kit.BasicServicesKit'; 608 609audioRenderer.flush().then(() => { 610 console.info('Renderer flushed successfully'); 611}).catch((err: BusinessError) => { 612 console.error(`ERROR: ${err}`); 613}); 614``` 615 616## stop<sup>8+</sup> 617 618stop(callback: AsyncCallback\<void>): void 619 620Stops this audio renderer. This API uses an asynchronous callback to return the result. 621 622**System capability**: SystemCapability.Multimedia.Audio.Renderer 623 624**Parameters** 625 626| Name | Type | Mandatory| Description | 627| -------- | -------------------- | ---- | ---------------- | 628| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 629 630**Example** 631 632```ts 633import { BusinessError } from '@kit.BasicServicesKit'; 634 635audioRenderer.stop((err: BusinessError) => { 636 if (err) { 637 console.error('Renderer stop failed'); 638 } else { 639 console.info('Renderer stopped.'); 640 } 641}); 642``` 643 644## stop<sup>8+</sup> 645 646stop(): Promise\<void> 647 648Stops this audio renderer. This API uses a promise to return the result. 649 650**System capability**: SystemCapability.Multimedia.Audio.Renderer 651 652**Return value** 653 654| Type | Description | 655| -------------- | ------------------------- | 656| Promise\<void> | Promise that returns no value.| 657 658**Example** 659 660```ts 661import { BusinessError } from '@kit.BasicServicesKit'; 662 663audioRenderer.stop().then(() => { 664 console.info('Renderer stopped successfully'); 665}).catch((err: BusinessError) => { 666 console.error(`ERROR: ${err}`); 667}); 668``` 669 670## release<sup>8+</sup> 671 672release(callback: AsyncCallback\<void>): void 673 674Releases the renderer. This API uses an asynchronous callback to return the result. 675 676**System capability**: SystemCapability.Multimedia.Audio.Renderer 677 678**Parameters** 679 680| Name | Type | Mandatory| Description | 681| -------- | -------------------- | ---- | ---------------- | 682| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 683 684**Example** 685 686```ts 687import { BusinessError } from '@kit.BasicServicesKit'; 688 689audioRenderer.release((err: BusinessError) => { 690 if (err) { 691 console.error('Renderer release failed'); 692 } else { 693 console.info('Renderer released.'); 694 } 695}); 696``` 697 698## release<sup>8+</sup> 699 700release(): Promise\<void> 701 702Releases the renderer. This API uses a promise to return the result. 703 704**System capability**: SystemCapability.Multimedia.Audio.Renderer 705 706**Return value** 707 708| Type | Description | 709| -------------- | ------------------------- | 710| Promise\<void> | Promise that returns no value.| 711 712**Example** 713 714```ts 715import { BusinessError } from '@kit.BasicServicesKit'; 716 717audioRenderer.release().then(() => { 718 console.info('Renderer released successfully'); 719}).catch((err: BusinessError) => { 720 console.error(`ERROR: ${err}`); 721}); 722``` 723 724## getAudioTime<sup>8+</sup> 725 726getAudioTime(callback: AsyncCallback\<number>): void 727 728Obtains the timestamp of the current playback position, measured in nanoseconds from the Unix epoch (January 1, 1970). This API uses an asynchronous callback to return the result. 729 730**System capability**: SystemCapability.Multimedia.Audio.Renderer 731 732**Parameters** 733 734| Name | Type | Mandatory| Description | 735| -------- | ---------------------- | ---- | ---------------- | 736| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the number of nanoseconds obtained; otherwise, **err** is an error object.| 737 738**Example** 739 740```ts 741import { BusinessError } from '@kit.BasicServicesKit'; 742 743audioRenderer.getAudioTime((err: BusinessError, timestamp: number) => { 744 console.info(`Current timestamp: ${timestamp}`); 745}); 746``` 747 748## getAudioTime<sup>8+</sup> 749 750getAudioTime(): Promise\<number> 751 752Obtains the timestamp of the current playback position, measured in nanoseconds from the Unix epoch (January 1, 1970). This API uses a promise to return the result. 753 754**System capability**: SystemCapability.Multimedia.Audio.Renderer 755 756**Return value** 757 758| Type | Description | 759| ---------------- | ----------------------- | 760| Promise\<number> | Promise used to return the timestamp.| 761 762**Example** 763 764```ts 765import { BusinessError } from '@kit.BasicServicesKit'; 766 767audioRenderer.getAudioTime().then((timestamp: number) => { 768 console.info(`Current timestamp: ${timestamp}`); 769}).catch((err: BusinessError) => { 770 console.error(`ERROR: ${err}`); 771}); 772``` 773 774## getAudioTimeSync<sup>10+</sup> 775 776getAudioTimeSync(): number 777 778Obtains the timestamp of the current playback position, measured in nanoseconds from the Unix epoch (January 1, 1970). This API returns the result synchronously. 779 780**System capability**: SystemCapability.Multimedia.Audio.Renderer 781 782**Return value** 783 784| Type | Description | 785| ---------------- | ----------------------- | 786| number | Timestamp.| 787 788**Example** 789 790```ts 791import { BusinessError } from '@kit.BasicServicesKit'; 792 793try { 794 let timestamp: number = audioRenderer.getAudioTimeSync(); 795 console.info(`Current timestamp: ${timestamp}`); 796} catch (err) { 797 let error = err as BusinessError; 798 console.error(`ERROR: ${error}`); 799} 800``` 801 802## getAudioTimestampInfo<sup>19+</sup> 803 804getAudioTimestampInfo(): Promise\<AudioTimestampInfo> 805 806Obtains the timestamp and position information of an output audio stream. It adapts to the speed adjustment interface. This API uses a promise to return the result. 807 808This information is commonly used for audio and video synchronization. 809 810Note that when the actual playback position (**framePosition**) is 0, the timestamp remains fixed until the stream begins to play. The playback position is also reset when **Flush** is called. 811 812Additionally, changes in the audio stream route, such as switching devices or output types, will reset the playback position, whereas the timestamp keeps increasing. You are advised to call this API to obtain the corresponding value only when the actual playback position and timestamp are stable. This API adapts to the speed adjustment interface. For example, if the playback speed is set to 2x, the rate at which the playback position increases is also twice the normal speed. 813 814**System capability**: SystemCapability.Multimedia.Audio.Renderer 815 816**Return value** 817 818| Type | Description | 819|-------------------------------------------------------| ----------------------- | 820| Promise\<[AudioTimestampInfo](arkts-apis-audio-i.md#audiotimestampinfo19)> | Promise used to return the audio stream timestamp and the current data frame position.| 821 822**Error codes** 823 824For details about the error codes, see [Audio Error Codes](errorcode-audio.md). 825 826| ID| Error Message| 827| ------- | --------------------------------------------| 828| 6800103 | Operation not permit at current state. | 829 830**Example** 831 832```ts 833import { BusinessError } from '@kit.BasicServicesKit'; 834 835audioRenderer.getAudioTimestampInfo().then((audioTimestampInfo: audio.AudioTimestampInfo) => { 836 console.info(`Current timestamp: ${audioTimestampInfo.timestamp}`); 837}).catch((err: BusinessError) => { 838 console.error(`ERROR: ${err}`); 839}); 840``` 841 842## getAudioTimestampInfoSync<sup>19+</sup> 843 844getAudioTimestampInfoSync(): AudioTimestampInfo 845 846Obtains the information about the audio stream timestamp and the current data frame position. This API returns the result synchronously. 847 848**System capability**: SystemCapability.Multimedia.Audio.Renderer 849 850**Return value** 851 852| Type | Description | 853| ---------------- | ----------------------- | 854| [AudioTimestampInfo](arkts-apis-audio-i.md#audiotimestampinfo19) | Information about the audio stream timestamp and the current data frame position.| 855 856**Error codes** 857 858For details about the error codes, see [Audio Error Codes](errorcode-audio.md). 859 860| ID| Error Message| 861| ------- | --------------------------------------------| 862| 6800103 | Operation not permit at current state. | 863 864**Example** 865 866```ts 867import { BusinessError } from '@kit.BasicServicesKit'; 868 869try { 870 let audioTimestampInfo: audio.AudioTimestampInfo = audioRenderer.getAudioTimestampInfoSync(); 871 console.info(`Current timestamp: ${audioTimestampInfo.timestamp}`); 872} catch (err) { 873 let error = err as BusinessError; 874 console.error(`ERROR: ${error}`); 875} 876``` 877 878## getBufferSize<sup>8+</sup> 879 880getBufferSize(callback: AsyncCallback\<number>): void 881 882Obtains a reasonable minimum buffer size in bytes for rendering. This API uses an asynchronous callback to return the result. 883 884**System capability**: SystemCapability.Multimedia.Audio.Renderer 885 886**Parameters** 887 888| Name | Type | Mandatory| Description | 889| -------- | ---------------------- | ---- | -------------------- | 890| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the minimum buffer size obtained; otherwise, **err** is an error object.| 891 892**Example** 893 894```ts 895import { BusinessError } from '@kit.BasicServicesKit'; 896 897let bufferSize: number; 898 899audioRenderer.getBufferSize((err: BusinessError, data: number) => { 900 if (err) { 901 console.error('getBufferSize error'); 902 } else { 903 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 904 bufferSize = data; 905 } 906}); 907``` 908 909## getBufferSize<sup>8+</sup> 910 911getBufferSize(): Promise\<number> 912 913Obtains a reasonable minimum buffer size in bytes for rendering. This API uses a promise to return the result. 914 915**System capability**: SystemCapability.Multimedia.Audio.Renderer 916 917**Return value** 918 919| Type | Description | 920| ---------------- | --------------------------- | 921| Promise\<number> | Promise used to return the buffer size.| 922 923**Example** 924 925```ts 926import { BusinessError } from '@kit.BasicServicesKit'; 927 928let bufferSize: number; 929 930audioRenderer.getBufferSize().then((data: number) => { 931 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 932 bufferSize = data; 933}).catch((err: BusinessError) => { 934 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${err}`); 935}); 936``` 937 938## getBufferSizeSync<sup>10+</sup> 939 940getBufferSizeSync(): number 941 942Obtains a reasonable minimum buffer size in bytes for rendering. This API returns the result synchronously. 943 944**System capability**: SystemCapability.Multimedia.Audio.Renderer 945 946**Return value** 947 948| Type | Description | 949| ---------------- | --------------------------- | 950| number | Buffer size.| 951 952**Example** 953 954```ts 955import { BusinessError } from '@kit.BasicServicesKit'; 956 957let bufferSize: number = 0; 958 959try { 960 bufferSize = audioRenderer.getBufferSizeSync(); 961 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${bufferSize}`); 962} catch (err) { 963 let error = err as BusinessError; 964 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${error}`); 965} 966``` 967 968## setSpeed<sup>11+</sup> 969 970setSpeed(speed: number): void 971 972Sets the playback speed. 973 974**System capability**: SystemCapability.Multimedia.Audio.Renderer 975 976**Parameters** 977 978| Name| Type | Mandatory| Description | 979| ------ | ---------------------------------------- | ---- |----------------------| 980| speed | number | Yes | Playback speed, which ranges from 0.25 to 4.0.| 981 982**Error codes** 983 984For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 985 986| ID| Error Message| 987| ------- | --------------------------------------------| 988| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 989| 6800101 | Parameter verification failed. | 990 991**Example** 992 993```ts 994audioRenderer.setSpeed(1.5); 995``` 996 997## getSpeed<sup>11+</sup> 998 999getSpeed(): number 1000 1001Obtains the playback speed. 1002 1003**System capability**: SystemCapability.Multimedia.Audio.Renderer 1004 1005**Return value** 1006 1007| Type | Description | 1008| ------------------------------------------------- |-----------| 1009| number | Playback speed.| 1010 1011**Example** 1012 1013```ts 1014let speed = audioRenderer.getSpeed(); 1015``` 1016 1017## setInterruptMode<sup>9+</sup> 1018 1019setInterruptMode(mode: InterruptMode): Promise<void> 1020 1021Sets the audio interruption mode for the application. This API uses a promise to return the result. 1022 1023**System capability**: SystemCapability.Multimedia.Audio.Interrupt 1024 1025**Parameters** 1026 1027| Name | Type | Mandatory | Description | 1028| ---------- | ---------------------------------- | ------ | ---------- | 1029| mode | [InterruptMode](arkts-apis-audio-e.md#interruptmode9) | Yes | Audio interruption mode. | 1030 1031**Return value** 1032 1033| Type | Description | 1034| ------------------- | ----------------------------- | 1035| Promise<void> | Promise that returns no value.| 1036 1037**Example** 1038 1039```ts 1040import { BusinessError } from '@kit.BasicServicesKit'; 1041 1042let mode = 0; 1043 1044audioRenderer.setInterruptMode(mode).then(() => { 1045 console.info('setInterruptMode Success!'); 1046}).catch((err: BusinessError) => { 1047 console.error(`setInterruptMode Fail: ${err}`); 1048}); 1049``` 1050## setInterruptMode<sup>9+</sup> 1051 1052setInterruptMode(mode: InterruptMode, callback: AsyncCallback\<void>): void 1053 1054Sets the audio interruption mode for the application. This API uses an asynchronous callback to return the result. 1055 1056**System capability**: SystemCapability.Multimedia.Audio.Interrupt 1057 1058**Parameters** 1059 1060| Name | Type | Mandatory | Description | 1061| ------- | ----------------------------------- | ------ | -------------- | 1062|mode | [InterruptMode](arkts-apis-audio-e.md#interruptmode9) | Yes | Audio interruption mode.| 1063|callback | AsyncCallback\<void> | Yes |Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1064 1065**Example** 1066 1067```ts 1068import { BusinessError } from '@kit.BasicServicesKit'; 1069 1070let mode = 1; 1071 1072audioRenderer.setInterruptMode(mode, (err: BusinessError) => { 1073 if(err){ 1074 console.error(`setInterruptMode Fail: ${err}`); 1075 } 1076 console.info('setInterruptMode Success!'); 1077}); 1078``` 1079 1080## setInterruptModeSync<sup>10+</sup> 1081 1082setInterruptModeSync(mode: InterruptMode): void 1083 1084Sets the audio interruption mode for the application. This API returns the result synchronously. 1085 1086**System capability**: SystemCapability.Multimedia.Audio.Interrupt 1087 1088**Parameters** 1089 1090| Name | Type | Mandatory | Description | 1091| ---------- | ---------------------------------- | ------ | ---------- | 1092| mode | [InterruptMode](arkts-apis-audio-e.md#interruptmode9) | Yes | Audio interruption mode. | 1093 1094**Error codes** 1095 1096For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 1097 1098| ID| Error Message| 1099| ------- | --------------------------------------------| 1100| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 1101| 6800101 | Parameter verification failed. | 1102 1103**Example** 1104 1105```ts 1106import { BusinessError } from '@kit.BasicServicesKit'; 1107 1108try { 1109 audioRenderer.setInterruptModeSync(0); 1110 console.info('setInterruptMode Success!'); 1111} catch (err) { 1112 let error = err as BusinessError; 1113 console.error(`setInterruptMode Fail: ${error}`); 1114} 1115``` 1116 1117## setVolume<sup>9+</sup> 1118 1119setVolume(volume: number): Promise<void> 1120 1121Sets the volume for the audio stream. This API uses a promise to return the result. 1122 1123**System capability**: SystemCapability.Multimedia.Audio.Renderer 1124 1125**Parameters** 1126 1127| Name | Type | Mandatory | Description | 1128| ---------- | ------- | ------ | ------------------- | 1129| volume | number | Yes | Volume to set, which is in the range [0.0, 1.0].| 1130 1131**Return value** 1132 1133| Type | Description | 1134| ------------------- | ----------------------------- | 1135| Promise<void> | Promise that returns no value.| 1136 1137**Example** 1138 1139```ts 1140import { BusinessError } from '@kit.BasicServicesKit'; 1141 1142audioRenderer.setVolume(0.5).then(() => { 1143 console.info('setVolume Success!'); 1144}).catch((err: BusinessError) => { 1145 console.error(`setVolume Fail: ${err}`); 1146}); 1147``` 1148## setVolume<sup>9+</sup> 1149 1150setVolume(volume: number, callback: AsyncCallback\<void>): void 1151 1152Sets the volume for the audio stream. This API uses an asynchronous callback to return the result. 1153 1154**System capability**: SystemCapability.Multimedia.Audio.Renderer 1155 1156**Parameters** 1157 1158| Name | Type | Mandatory | Description | 1159| ------- | -----------| ------ | ------------------- | 1160|volume | number | Yes | Volume to set, which is in the range [0.0, 1.0].| 1161|callback | AsyncCallback\<void> | Yes |Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1162 1163**Example** 1164 1165```ts 1166import { BusinessError } from '@kit.BasicServicesKit'; 1167 1168audioRenderer.setVolume(0.5, (err: BusinessError) => { 1169 if(err){ 1170 console.error(`setVolume Fail: ${err}`); 1171 return; 1172 } 1173 console.info('setVolume Success!'); 1174}); 1175``` 1176## getVolume<sup>12+</sup> 1177 1178getVolume(): number 1179 1180Obtains the volume of the audio stream. This API returns the result synchronously. 1181 1182**System capability**: SystemCapability.Multimedia.Audio.Renderer 1183 1184**Return value** 1185 1186| Type | Description | 1187| ---------------- | --------------------------- | 1188| number | Volume, in the range [0.0, 1.0].| 1189 1190**Example** 1191 1192```ts 1193import { BusinessError } from '@kit.BasicServicesKit'; 1194 1195try { 1196 let value: number = audioRenderer.getVolume(); 1197 console.info(`Indicate that the volume is obtained ${value}.`); 1198} catch (err) { 1199 let error = err as BusinessError; 1200 console.error(`Failed to obtain the volume, error ${error}.`); 1201} 1202``` 1203 1204## getMinStreamVolume<sup>10+</sup> 1205 1206getMinStreamVolume(callback: AsyncCallback<number>): void 1207 1208Obtains the minimum volume of the audio stream. This API uses an asynchronous callback to return the result. 1209 1210**System capability**: SystemCapability.Multimedia.Audio.Renderer 1211 1212**Parameters** 1213 1214| Name | Type | Mandatory | Description | 1215| ------- | -----------| ------ | ------------------- | 1216|callback |AsyncCallback<number> | Yes |Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the minimum volume (range [0, 1]) obtained; otherwise, **err** is an error object.| 1217 1218**Example** 1219 1220```ts 1221import { BusinessError } from '@kit.BasicServicesKit'; 1222 1223audioRenderer.getMinStreamVolume((err: BusinessError, minVolume: number) => { 1224 if (err) { 1225 console.error(`getMinStreamVolume error: ${err}`); 1226 } else { 1227 console.info(`getMinStreamVolume Success! ${minVolume}`); 1228 } 1229}); 1230``` 1231## getMinStreamVolume<sup>10+</sup> 1232 1233getMinStreamVolume(): Promise<number> 1234 1235Obtains the minimum volume of the audio stream. This API uses a promise to return the result. 1236 1237**System capability**: SystemCapability.Multimedia.Audio.Renderer 1238 1239**Return value** 1240 1241| Type | Description | 1242| ------------------- | ----------------------------- | 1243| Promise<number>| Promise used to return the minimum volume, which is in the range [0, 1].| 1244 1245**Example** 1246 1247```ts 1248import { BusinessError } from '@kit.BasicServicesKit'; 1249 1250audioRenderer.getMinStreamVolume().then((value: number) => { 1251 console.info(`Get min stream volume Success! ${value}`); 1252}).catch((err: BusinessError) => { 1253 console.error(`Get min stream volume Fail: ${err}`); 1254}); 1255``` 1256 1257## getMinStreamVolumeSync<sup>10+</sup> 1258 1259getMinStreamVolumeSync(): number 1260 1261Obtains the minimum volume of the audio stream. This API returns the result synchronously. 1262 1263**System capability**: SystemCapability.Multimedia.Audio.Renderer 1264 1265**Return value** 1266 1267| Type | Description | 1268| ------------------- | ----------------------------- | 1269| number| Minimum volume, which is in the range [0, 1].| 1270 1271**Example** 1272 1273```ts 1274import { BusinessError } from '@kit.BasicServicesKit'; 1275 1276try { 1277 let value: number = audioRenderer.getMinStreamVolumeSync(); 1278 console.info(`Get min stream volume Success! ${value}`); 1279} catch (err) { 1280 let error = err as BusinessError; 1281 console.error(`Get min stream volume Fail: ${error}`); 1282} 1283``` 1284 1285## getMaxStreamVolume<sup>10+</sup> 1286 1287getMaxStreamVolume(callback: AsyncCallback<number>): void 1288 1289Obtains the maximum volume of the audio stream. This API uses an asynchronous callback to return the result. 1290 1291**System capability**: SystemCapability.Multimedia.Audio.Renderer 1292 1293**Parameters** 1294 1295| Name | Type | Mandatory | Description | 1296| ------- | -----------| ------ | ------------------- | 1297|callback | AsyncCallback<number> | Yes |Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the maximum volume (range [0, 1]) obtained; otherwise, **err** is an error object.| 1298 1299**Example** 1300 1301```ts 1302import { BusinessError } from '@kit.BasicServicesKit'; 1303 1304audioRenderer.getMaxStreamVolume((err: BusinessError, maxVolume: number) => { 1305 if (err) { 1306 console.error(`getMaxStreamVolume Fail: ${err}`); 1307 } else { 1308 console.info(`getMaxStreamVolume Success! ${maxVolume}`); 1309 } 1310}); 1311``` 1312## getMaxStreamVolume<sup>10+</sup> 1313 1314getMaxStreamVolume(): Promise<number> 1315 1316Obtains the maximum volume of the audio stream. This API uses a promise to return the result. 1317 1318**System capability**: SystemCapability.Multimedia.Audio.Renderer 1319 1320**Return value** 1321 1322| Type | Description | 1323| ------------------- | ----------------------------- | 1324| Promise<number>| Promise used to return the maximum volume, which is in the range [0, 1].| 1325 1326**Example** 1327 1328```ts 1329import { BusinessError } from '@kit.BasicServicesKit'; 1330 1331audioRenderer.getMaxStreamVolume().then((value: number) => { 1332 console.info(`Get max stream volume Success! ${value}`); 1333}).catch((err: BusinessError) => { 1334 console.error(`Get max stream volume Fail: ${err}`); 1335}); 1336``` 1337 1338## getMaxStreamVolumeSync<sup>10+</sup> 1339 1340getMaxStreamVolumeSync(): number 1341 1342Obtains the maximum volume of the audio stream. This API returns the result synchronously. 1343 1344**System capability**: SystemCapability.Multimedia.Audio.Renderer 1345 1346**Return value** 1347 1348| Type | Description | 1349| ------------------- | ----------------------------- | 1350| number| Maximum volume, which is in the range [0, 1].| 1351 1352**Example** 1353 1354```ts 1355import { BusinessError } from '@kit.BasicServicesKit'; 1356 1357try { 1358 let value: number = audioRenderer.getMaxStreamVolumeSync(); 1359 console.info(`Get max stream volume Success! ${value}`); 1360} catch (err) { 1361 let error = err as BusinessError; 1362 console.error(`Get max stream volume Fail: ${error}`); 1363} 1364``` 1365 1366## getUnderflowCount<sup>10+</sup> 1367 1368getUnderflowCount(callback: AsyncCallback<number>): void 1369 1370Obtains the number of underflow audio frames in the audio stream that is being played. This API uses an asynchronous callback to return the result. 1371 1372**System capability**: SystemCapability.Multimedia.Audio.Renderer 1373 1374**Parameters** 1375 1376| Name | Type | Mandatory | Description | 1377| ------- | -----------| ------ | ------------------- | 1378|callback | AsyncCallback<number> | Yes |Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the number of underloaded audio frames obtained; otherwise, **err** is an error object.| 1379 1380**Example** 1381 1382```ts 1383import { BusinessError } from '@kit.BasicServicesKit'; 1384 1385audioRenderer.getUnderflowCount((err: BusinessError, underflowCount: number) => { 1386 if (err) { 1387 console.error(`getUnderflowCount Fail: ${err}`); 1388 } else { 1389 console.info(`getUnderflowCount Success! ${underflowCount}`); 1390 } 1391}); 1392``` 1393## getUnderflowCount<sup>10+</sup> 1394 1395getUnderflowCount(): Promise<number> 1396 1397Obtains the number of underflow audio frames in the audio stream that is being played. This API uses a promise to return the result. 1398 1399**System capability**: SystemCapability.Multimedia.Audio.Renderer 1400 1401**Return value** 1402 1403| Type | Description | 1404| ------------------- | ----------------------------- | 1405| Promise<number>| Promise used to return the number of underflow audio frames.| 1406 1407**Example** 1408 1409```ts 1410import { BusinessError } from '@kit.BasicServicesKit'; 1411 1412audioRenderer.getUnderflowCount().then((value: number) => { 1413 console.info(`Get underflow count Success! ${value}`); 1414}).catch((err: BusinessError) => { 1415 console.error(`Get underflow count Fail: ${err}`); 1416}); 1417``` 1418 1419## getUnderflowCountSync<sup>10+</sup> 1420 1421getUnderflowCountSync(): number 1422 1423Obtains the number of underflow audio frames in the audio stream that is being played. This API returns the result synchronously. 1424 1425**System capability**: SystemCapability.Multimedia.Audio.Renderer 1426 1427**Return value** 1428 1429| Type | Description | 1430| ------------------- | ----------------------------- | 1431| number| Number of underflow audio frames.| 1432 1433**Example** 1434 1435```ts 1436import { BusinessError } from '@kit.BasicServicesKit'; 1437 1438try { 1439 let value: number = audioRenderer.getUnderflowCountSync(); 1440 console.info(`Get underflow count Success! ${value}`); 1441} catch (err) { 1442 let error = err as BusinessError; 1443 console.error(`Get underflow count Fail: ${error}`); 1444} 1445``` 1446 1447## getCurrentOutputDevices<sup>10+</sup> 1448 1449getCurrentOutputDevices(callback: AsyncCallback<AudioDeviceDescriptors>): void 1450 1451Obtains the output device information of the audio stream. This API uses an asynchronous callback to return the result. 1452 1453**System capability**: SystemCapability.Multimedia.Audio.Device 1454 1455**Parameters** 1456 1457| Name | Type | Mandatory | Description | 1458| ------- | -----------| ------ | ------------------- | 1459|callback | AsyncCallback\<[AudioDeviceDescriptors](arkts-apis-audio-t.md#audiodevicedescriptors)>| Yes |Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the output device information obtained; otherwise, **err** is an error object.| 1460 1461**Example** 1462 1463```ts 1464import { BusinessError } from '@kit.BasicServicesKit'; 1465 1466audioRenderer.getCurrentOutputDevices((err: BusinessError, deviceInfo: audio.AudioDeviceDescriptors) => { 1467 if (err) { 1468 console.error(`getCurrentOutputDevices Fail: ${err}`); 1469 } else { 1470 for (let i = 0; i < deviceInfo.length; i++) { 1471 console.info(`DeviceInfo id: ${deviceInfo[i].id}`); 1472 console.info(`DeviceInfo type: ${deviceInfo[i].deviceType}`); 1473 console.info(`DeviceInfo role: ${deviceInfo[i].deviceRole}`); 1474 console.info(`DeviceInfo name: ${deviceInfo[i].name}`); 1475 console.info(`DeviceInfo address: ${deviceInfo[i].address}`); 1476 console.info(`DeviceInfo samplerate: ${deviceInfo[i].sampleRates[0]}`); 1477 console.info(`DeviceInfo channelcount: ${deviceInfo[i].channelCounts[0]}`); 1478 console.info(`DeviceInfo channelmask: ${deviceInfo[i].channelMasks[0]}`); 1479 } 1480 } 1481}); 1482``` 1483## getCurrentOutputDevices<sup>10+</sup> 1484 1485getCurrentOutputDevices(): Promise<AudioDeviceDescriptors> 1486 1487Obtains the output device information of the audio stream. This API uses a promise to return the result. 1488 1489**System capability**: SystemCapability.Multimedia.Audio.Device 1490 1491**Return value** 1492 1493| Type | Description | 1494| ------------------- | ----------------------------- | 1495| Promise<[AudioDeviceDescriptors](arkts-apis-audio-t.md#audiodevicedescriptors)>| Promise used to return the output device information.| 1496 1497**Example** 1498 1499```ts 1500import { BusinessError } from '@kit.BasicServicesKit'; 1501 1502audioRenderer.getCurrentOutputDevices().then((deviceInfo: audio.AudioDeviceDescriptors) => { 1503 for (let i = 0; i < deviceInfo.length; i++) { 1504 console.info(`DeviceInfo id: ${deviceInfo[i].id}`); 1505 console.info(`DeviceInfo type: ${deviceInfo[i].deviceType}`); 1506 console.info(`DeviceInfo role: ${deviceInfo[i].deviceRole}`); 1507 console.info(`DeviceInfo name: ${deviceInfo[i].name}`); 1508 console.info(`DeviceInfo address: ${deviceInfo[i].address}`); 1509 console.info(`DeviceInfo samplerate: ${deviceInfo[i].sampleRates[0]}`); 1510 console.info(`DeviceInfo channelcount: ${deviceInfo[i].channelCounts[0]}`); 1511 console.info(`DeviceInfo channelmask: ${deviceInfo[i].channelMasks[0]}`); 1512 } 1513}).catch((err: BusinessError) => { 1514 console.error(`Get current output devices Fail: ${err}`); 1515}); 1516``` 1517 1518## getCurrentOutputDevicesSync<sup>10+</sup> 1519 1520getCurrentOutputDevicesSync(): AudioDeviceDescriptors 1521 1522Obtains the output device information of the audio stream. This API returns the result synchronously. 1523 1524**System capability**: SystemCapability.Multimedia.Audio.Device 1525 1526**Return value** 1527 1528| Type | Description | 1529| ------------------- | ----------------------------- | 1530| [AudioDeviceDescriptors](arkts-apis-audio-t.md#audiodevicedescriptors) | Output device information.| 1531 1532**Example** 1533 1534```ts 1535import { BusinessError } from '@kit.BasicServicesKit'; 1536 1537try { 1538 let deviceInfo: audio.AudioDeviceDescriptors = audioRenderer.getCurrentOutputDevicesSync(); 1539 for (let i = 0; i < deviceInfo.length; i++) { 1540 console.info(`DeviceInfo id: ${deviceInfo[i].id}`); 1541 console.info(`DeviceInfo type: ${deviceInfo[i].deviceType}`); 1542 console.info(`DeviceInfo role: ${deviceInfo[i].deviceRole}`); 1543 console.info(`DeviceInfo name: ${deviceInfo[i].name}`); 1544 console.info(`DeviceInfo address: ${deviceInfo[i].address}`); 1545 console.info(`DeviceInfo samplerate: ${deviceInfo[i].sampleRates[0]}`); 1546 console.info(`DeviceInfo channelcount: ${deviceInfo[i].channelCounts[0]}`); 1547 console.info(`DeviceInfo channelmask: ${deviceInfo[i].channelMasks[0]}`); 1548 } 1549} catch (err) { 1550 let error = err as BusinessError; 1551 console.error(`Get current output devices Fail: ${error}`); 1552} 1553``` 1554## setChannelBlendMode<sup>11+</sup> 1555 1556setChannelBlendMode(mode: ChannelBlendMode): void 1557 1558Sets the audio channel blending mode. This API returns the result synchronously. 1559 1560**System capability**: SystemCapability.Multimedia.Audio.Renderer 1561 1562**Parameters** 1563 1564| Name | Type | Mandatory| Description | 1565| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 1566| mode | [ChannelBlendMode](arkts-apis-audio-e.md#channelblendmode11) | Yes | Audio channel blending mode. | 1567 1568**Error codes** 1569 1570For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 1571 1572| ID| Error Message| 1573| ------- | --------------------------------------------| 1574| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 1575| 6800101 | Parameter verification failed. | 1576| 6800103 | Operation not permit at current state. | 1577 1578**Example** 1579 1580```ts 1581let mode = audio.ChannelBlendMode.MODE_DEFAULT; 1582 1583audioRenderer.setChannelBlendMode(mode); 1584console.info(`BlendMode: ${mode}`); 1585``` 1586## setVolumeWithRamp<sup>11+</sup> 1587 1588setVolumeWithRamp(volume: number, duration: number): void 1589 1590Sets a volume ramp. This API returns the result synchronously. 1591 1592**System capability**: SystemCapability.Multimedia.Audio.Renderer 1593 1594**Parameters** 1595 1596| Name | Type | Mandatory| Description | 1597| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | 1598| volume | number | Yes | Target volume, within the range [0.0, 1.0]. | 1599| duration | number | Yes | Time range during which the ramp applies, in ms. | 1600 1601**Error codes** 1602 1603For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 1604 1605| ID| Error Message| 1606| ------- | --------------------------------------------| 1607| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 1608| 6800101 | Parameter verification failed. | 1609 1610**Example** 1611 1612```ts 1613let volume = 0.5; 1614let duration = 1000; 1615 1616audioRenderer.setVolumeWithRamp(volume, duration); 1617console.info(`setVolumeWithRamp: ${volume}`); 1618``` 1619 1620## setSilentModeAndMixWithOthers<sup>12+</sup> 1621 1622setSilentModeAndMixWithOthers(on: boolean): void 1623 1624Sets the silent mode in concurrent playback for the audio stream. 1625 1626If the silent mode in concurrent playback is enabled, the system mutes the audio stream and does not interrupt other audio streams. If the silent mode in concurrent playback is disabled, the audio stream can gain focus based on the system focus policy. 1627 1628**System capability**: SystemCapability.Multimedia.Audio.Renderer 1629 1630**Parameters** 1631 1632| Name| Type | Mandatory| Description | 1633| ------ | ---------------------------------------- | ---- |----------------------| 1634| on | boolean | Yes | Whether to enable or disable the silent mode in concurrent playback for the audio stream. The value **true** means to enable the silent mode in concurrent playback, and **false** means the opposite.| 1635 1636**Example** 1637 1638```ts 1639audioRenderer.setSilentModeAndMixWithOthers(true); 1640``` 1641 1642## getSilentModeAndMixWithOthers<sup>12+</sup> 1643 1644getSilentModeAndMixWithOthers(): boolean 1645 1646Obtains the silent mode in concurrent playback for the audio stream. 1647 1648**System capability**: SystemCapability.Multimedia.Audio.Renderer 1649 1650**Return value** 1651 1652| Type | Description | 1653| ------------------------------------------------- |-----------| 1654| boolean | Enabled status. The value **true** means that the silent mode in concurrent playback is enabled, and **false** means the opposite.| 1655 1656**Example** 1657 1658```ts 1659let on = audioRenderer.getSilentModeAndMixWithOthers(); 1660``` 1661 1662## setDefaultOutputDevice<sup>12+</sup> 1663 1664setDefaultOutputDevice(deviceType: DeviceType): Promise<void> 1665 1666Sets the default audio output device. This API uses a promise to return the result. 1667 1668> **NOTE** 1669> 1670> - This API applies only to the scenarios where [StreamUsage](arkts-apis-audio-e.md#streamusage) is set to voice message, VoIP voice calls, or VoIP video calls. It supports only receivers, speakers, and system default devices. 1671> 1672> - This API can be called at any time after an AudioRenderer instance is created. The system records the device set by the application. When the application is started, if an external device such as a Bluetooth or wired headset is connected, the system preferentially uses the external device to play sound. Otherwise, the system uses this default device to play sound. 1673> 1674> - This API has a lower priority than [AVCastPicker](../apis-avsession-kit/ohos-multimedia-avcastpicker.md#avcastpicker). If you have already switched the audio device using AVCastPicker, using this API to switch devices again does not take effect. 1675 1676**System capability**: SystemCapability.Multimedia.Audio.Renderer 1677 1678**Parameters** 1679 1680| Name | Type | Mandatory | Description | 1681| ---------- |----------------| ------ |---------------------------------------------------------| 1682| deviceType | [DeviceType](arkts-apis-audio-e.md#devicetype) | Yes | Device type.<br>The options are **EARPIECE**, **SPEAKER**, and **DEFAULT**.| 1683 1684**Return value** 1685 1686| Type | Description | 1687| ------------------- | ----------------------------- | 1688| Promise<void> | Promise that returns no value.| 1689 1690**Error codes** 1691 1692For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 1693 1694| ID| Error Message| 1695| ------- | --------------------------------------------| 1696| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 1697| 6800101 | Parameter verification failed. | 1698| 6800103 | Operation not permit at current state. | 1699 1700**Example** 1701 1702```ts 1703import { BusinessError } from '@kit.BasicServicesKit'; 1704 1705// This API can be called at any time after an AudioRenderer instance is created. 1706// If the API is called when no audio is being played, the system records the default device set by the application. When the application starts playing, the sound is played from this default device. 1707// If the API is called when audio is being played and no external device, such as a Bluetooth or wired headset, is connected, the system immediately switches to the default device. If an external device is connected, the system records the default device and switches to it once the external device is disconnected. 1708audioRenderer.setDefaultOutputDevice(audio.DeviceType.SPEAKER).then(() => { 1709 console.info('setDefaultOutputDevice Success!'); 1710}).catch((err: BusinessError) => { 1711 console.error(`setDefaultOutputDevice Fail: ${err}`); 1712}); 1713``` 1714 1715## on('audioInterrupt')<sup>9+</sup> 1716 1717on(type: 'audioInterrupt', callback: Callback\<InterruptEvent>): void 1718 1719Subscribes to the audio interruption event, which is triggered when the audio focus is changed. This API uses an asynchronous callback to return the result. 1720 1721The AudioRenderer instance proactively gains the focus when the **start** event occurs and releases the focus when the **pause** or **stop** event occurs. Therefore, you do not need to request to gain or release the focus. 1722 1723After this API is called, an [InterruptEvent](arkts-apis-audio-i.md#interruptevent9) is received when the AudioRenderer instance fails to obtain the focus or an audio interruption event occurs (for example, the audio stream is interrupted by others). It is recommended that the application perform further processing based on the **InterruptEvent** information. For details, see [Introduction to Audio Focus and Audio Sessions](../../media/audio/audio-playback-concurrency.md). 1724 1725**System capability**: SystemCapability.Multimedia.Audio.Interrupt 1726 1727**Parameters** 1728 1729| Name | Type | Mandatory| Description | 1730| -------- | -------------------------------------------- | ---- | ----------------------------------------------------------- | 1731| type | string | Yes | Event type. The event **'audioInterrupt'** is triggered when the audio focus is changed.| 1732| callback | Callback\<[InterruptEvent](arkts-apis-audio-i.md#interruptevent9)\> | Yes | Callback used to return the event information.| 1733 1734**Error codes** 1735 1736For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 1737 1738| ID| Error Message| 1739| ------- | --------------------------------------------| 1740| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 1741| 6800101 | Parameter verification failed. | 1742 1743**Example** 1744 1745```ts 1746import { audio } from '@kit.AudioKit'; 1747 1748let isPlaying: boolean; // An identifier specifying whether rendering is in progress. 1749let isDucked: boolean; // An identifier specifying whether the audio volume is reduced. 1750onAudioInterrupt(); 1751 1752async function onAudioInterrupt(){ 1753 audioRenderer.on('audioInterrupt', (interruptEvent: audio.InterruptEvent) => { 1754 // When an audio interruption event occurs, the AudioRenderer receives the interruptEvent callback and performs processing based on the content in the callback. 1755 // 1. (Optional) The AudioRenderer reads the value of interruptEvent.forceType to see whether the system has forcibly performed the operation. 1756 // Note: In the default focus policy, INTERRUPT_HINT_RESUME maps to the force type INTERRUPT_SHARE, and others map to INTERRUPT_FORCE. Therefore, the value of forceType does not need to be checked. 1757 // 2. (Mandatory) The AudioRenderer then reads the value of interruptEvent.hintType and performs corresponding processing. 1758 if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 1759 // The audio focus event has been forcibly executed by the system. The application needs to update its status and displayed content. 1760 switch (interruptEvent.hintType) { 1761 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 1762 // The audio stream has been paused and temporarily loses the focus. It will receive the interruptEvent corresponding to resume when it is able to regain the focus. 1763 console.info('Force paused. Update playing status and stop writing'); 1764 isPlaying = false; // A simplified processing indicating several operations for switching the application to the paused state. 1765 break; 1766 case audio.InterruptHint.INTERRUPT_HINT_STOP: 1767 // The audio stream has been stopped and permanently loses the focus. The user must manually trigger the operation to resume rendering. 1768 console.info('Force stopped. Update playing status and stop writing'); 1769 isPlaying = false; // A simplified processing indicating several operations for switching the application to the paused state. 1770 break; 1771 case audio.InterruptHint.INTERRUPT_HINT_DUCK: 1772 // The audio stream is rendered at a reduced volume. 1773 console.info('Force ducked. Update volume status'); 1774 isDucked = true; // A simplified processing indicating several operations for updating the volume status. 1775 break; 1776 case audio.InterruptHint.INTERRUPT_HINT_UNDUCK: 1777 // The audio stream is rendered at the normal volume. 1778 console.info('Force ducked. Update volume status'); 1779 isDucked = false; // A simplified processing indicating several operations for updating the volume status. 1780 break; 1781 default: 1782 console.info('Invalid interruptEvent'); 1783 break; 1784 } 1785 } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 1786 // The audio focus event needs to be operated by the application, which can choose the processing mode. It is recommended that the application process the event according to the value of InterruptHint. 1787 switch (interruptEvent.hintType) { 1788 case audio.InterruptHint.INTERRUPT_HINT_RESUME: 1789 // It is recommended that the application continue rendering. (The audio stream has been forcibly paused and temporarily lost the focus. It can resume rendering now.) 1790 // The INTERRUPT_HINT_RESUME operation must be proactively executed by the application and cannot be forcibly executed by the system. Therefore, the INTERRUPT_HINT_RESUME event must map to INTERRUPT_SHARE. 1791 console.info('Resume force paused renderer or ignore'); 1792 // To continue rendering, the application must perform the required operations. 1793 break; 1794 default: 1795 console.info('Invalid interruptEvent'); 1796 break; 1797 } 1798 } 1799 }); 1800} 1801``` 1802 1803## off('audioInterrupt')<sup>18+</sup> 1804 1805off(type: 'audioInterrupt', callback?: Callback<InterruptEvent>): void 1806 1807Unsubscribes from the audio interruption event. This API uses an asynchronous callback to return the result. 1808 1809**System capability**: SystemCapability.Multimedia.Audio.Interrupt 1810 1811**Parameters** 1812 1813| Name | Type | Mandatory| Description | 1814| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 1815| type | string | Yes | Event type. The event **'audioInterrupt'** is triggered when the audio focus is changed.| 1816| callback | Callback\<[InterruptEvent](arkts-apis-audio-i.md#interruptevent9)\> | No| Callback used to return the event information.| 1817 1818**Error codes** 1819 1820For details about the error codes, see [Audio Error Codes](errorcode-audio.md). 1821 1822| ID| Error Message| 1823| ------- | --------------------------------------------| 1824| 6800101 | Parameter verification failed. | 1825 1826**Example** 1827 1828```ts 1829// Cancel all subscriptions to the event. 1830audioRenderer.off('audioInterrupt'); 1831 1832// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter. 1833let isPlaying: boolean; // An identifier specifying whether rendering is in progress. 1834let isDucked: boolean; // An identifier specifying whether the audio volume is reduced. 1835 1836let audioInterruptCallback = (interruptEvent: audio.InterruptEvent) => { 1837 // When an audio interruption event occurs, the AudioRenderer receives the interruptEvent callback and performs processing based on the content in the callback. 1838 // 1. (Optional) The AudioRenderer reads the value of interruptEvent.forceType to see whether the system has forcibly performed the operation. 1839 // Note: In the default focus policy, INTERRUPT_HINT_RESUME maps to the force type INTERRUPT_SHARE, and others map to INTERRUPT_FORCE. Therefore, the value of forceType does not need to be checked. 1840 // 2. (Mandatory) The AudioRenderer then reads the value of interruptEvent.hintType and performs corresponding processing. 1841 if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 1842 // The audio focus event has been forcibly executed by the system. The application needs to update its status and displayed content. 1843 switch (interruptEvent.hintType) { 1844 case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 1845 // The audio stream has been paused and temporarily loses the focus. It will receive the interruptEvent corresponding to resume when it is able to regain the focus. 1846 console.info('Force paused. Update playing status and stop writing'); 1847 isPlaying = false; // A simplified processing indicating several operations for switching the application to the paused state. 1848 break; 1849 case audio.InterruptHint.INTERRUPT_HINT_STOP: 1850 // The audio stream has been stopped and permanently loses the focus. The user must manually trigger the operation to resume rendering. 1851 console.info('Force stopped. Update playing status and stop writing'); 1852 isPlaying = false; // A simplified processing indicating several operations for switching the application to the paused state. 1853 break; 1854 case audio.InterruptHint.INTERRUPT_HINT_DUCK: 1855 // The audio stream is rendered at a reduced volume. 1856 console.info('Force ducked. Update volume status'); 1857 isDucked = true; // A simplified processing indicating several operations for updating the volume status. 1858 break; 1859 case audio.InterruptHint.INTERRUPT_HINT_UNDUCK: 1860 // The audio stream is rendered at the normal volume. 1861 console.info('Force ducked. Update volume status'); 1862 isDucked = false; // A simplified processing indicating several operations for updating the volume status. 1863 break; 1864 default: 1865 console.info('Invalid interruptEvent'); 1866 break; 1867 } 1868 } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 1869 // The audio focus event needs to be operated by the application, which can choose the processing mode. It is recommended that the application process the event according to the value of InterruptHint. 1870 switch (interruptEvent.hintType) { 1871 case audio.InterruptHint.INTERRUPT_HINT_RESUME: 1872 // It is recommended that the application continue rendering. (The audio stream has been forcibly paused and temporarily lost the focus. It can resume rendering now.) 1873 // The INTERRUPT_HINT_RESUME operation must be proactively executed by the application and cannot be forcibly executed by the system. Therefore, the INTERRUPT_HINT_RESUME event must map to INTERRUPT_SHARE. 1874 console.info('Resume force paused renderer or ignore'); 1875 // To continue rendering, the application must perform the required operations. 1876 break; 1877 default: 1878 console.info('Invalid interruptEvent'); 1879 break; 1880 } 1881 } 1882}; 1883 1884audioRenderer.on('audioInterrupt', audioInterruptCallback); 1885 1886audioRenderer.off('audioInterrupt', audioInterruptCallback); 1887``` 1888 1889## on('markReach')<sup>8+</sup> 1890 1891on(type: 'markReach', frame: number, callback: Callback<number>): void 1892 1893Subscribes to the mark reached event, which is triggered (only once) when the number of frames rendered reaches the value of the **frame** parameter. This API uses an asynchronous callback to return the result. 1894 1895For example, if **frame** is set to **100**, the callback is invoked when the number of rendered frames reaches the 100th frame. 1896 1897**System capability**: SystemCapability.Multimedia.Audio.Renderer 1898 1899**Parameters** 1900 1901| Name | Type | Mandatory| Description | 1902| :------- | :----------------------- | :--- | :---------------------------------------- | 1903| type | string | Yes | Event type. The event **'markReach'** is triggered when the number of frames rendered reaches the value of the **frame** parameter.| 1904| frame | number | Yes | Number of frames to trigger the event. The value must be greater than **0**. | 1905| callback | Callback\<number> | Yes | Callback used to return the value of the **frame** parameter.| 1906 1907**Example** 1908 1909```ts 1910audioRenderer.on('markReach', 1000, (position: number) => { 1911 if (position == 1000) { 1912 console.info('ON Triggered successfully'); 1913 } 1914}); 1915``` 1916 1917 1918## off('markReach')<sup>8+</sup> 1919 1920off(type: 'markReach', callback?: Callback<number>): void 1921 1922Unsubscribes from the mark reached event. This API uses an asynchronous callback to return the result. 1923 1924**System capability**: SystemCapability.Multimedia.Audio.Renderer 1925 1926**Parameters** 1927 1928| Name| Type | Mandatory| Description | 1929| :----- | :----- | :--- | :------------------------------------------------ | 1930| type | string | Yes | Event type. The event **'markReach'** is triggered when the number of frames rendered reaches the value of the **frame** parameter.| 1931| callback<sup>18+</sup> | Callback\<number> | No | Callback used to return the value of the **frame** parameter.| 1932 1933**Example** 1934 1935```ts 1936// Cancel all subscriptions to the event. 1937audioRenderer.off('markReach'); 1938 1939// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter. 1940let markReachCallback = (position: number) => { 1941 if (position == 1000) { 1942 console.info('ON Triggered successfully'); 1943 } 1944}; 1945 1946audioRenderer.on('markReach', 1000, markReachCallback); 1947 1948audioRenderer.off('markReach', markReachCallback); 1949``` 1950 1951## on('periodReach')<sup>8+</sup> 1952 1953on(type: 'periodReach', frame: number, callback: Callback<number>): void 1954 1955Subscribes to the period reached event, which is triggered each time the number of frames rendered reaches the value of the **frame** parameter. In other words, the information is reported periodically. This API uses an asynchronous callback to return the result. 1956 1957For example, if **frame** is set to **10**, the callback is invoked each time 10 frames are rendered, for example, when the number of frames rendered reaches the 10th frame, 20th frame, and 30th frame. 1958 1959**System capability**: SystemCapability.Multimedia.Audio.Renderer 1960 1961**Parameters** 1962 1963| Name | Type | Mandatory| Description | 1964| :------- | :----------------------- | :--- | :------------------------------------------ | 1965| type | string | Yes | Event type. The event **'periodReach'** is triggered each time the number of frames rendered reaches the value of the **frame** parameter.| 1966| frame | number | Yes | Number of frames to trigger the event. The value must be greater than **0**. | 1967| callback | Callback\<number> | Yes | Callback used to return the value of the **frame** parameter.| 1968 1969**Example** 1970 1971```ts 1972audioRenderer.on('periodReach', 1000, (position: number) => { 1973 if (position == 1000) { 1974 console.info('ON Triggered successfully'); 1975 } 1976}); 1977``` 1978 1979## off('periodReach')<sup>8+</sup> 1980 1981off(type: 'periodReach', callback?: Callback<number>): void 1982 1983Unsubscribes from the period reached event. This API uses an asynchronous callback to return the result. 1984 1985**System capability**: SystemCapability.Multimedia.Audio.Renderer 1986 1987**Parameters** 1988 1989| Name| Type | Mandatory| Description | 1990| :----- | :----- | :--- | :-------------------------------------------------- | 1991| type | string | Yes | Event type. The event **'periodReach'** is triggered each time the number of frames rendered reaches the value of the **frame** parameter.| 1992| callback<sup>18+</sup> | Callback\<number> | No | Callback used to return the value of the **frame** parameter.| 1993 1994**Example** 1995 1996```ts 1997// Cancel all subscriptions to the event. 1998audioRenderer.off('periodReach'); 1999 2000// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter. 2001let periodReachCallback = (position: number) => { 2002 if (position == 1000) { 2003 console.info('ON Triggered successfully'); 2004 } 2005}; 2006 2007audioRenderer.on('periodReach', 1000, periodReachCallback); 2008 2009audioRenderer.off('periodReach', periodReachCallback); 2010``` 2011 2012## on('stateChange')<sup>8+</sup> 2013 2014on(type: 'stateChange', callback: Callback<AudioState\>): void 2015 2016Subscribes to the audio renderer state change event, which is triggered when the state of the audio renderer is changed. This API uses an asynchronous callback to return the result. 2017 2018**System capability**: SystemCapability.Multimedia.Audio.Renderer 2019 2020**Parameters** 2021 2022| Name | Type | Mandatory| Description | 2023| :------- | :------------------------- | :--- | :------------------------------------------ | 2024| type | string | Yes | Event type. The event **'stateChange'** is triggered when the state of the audio renderer is changed.| 2025| callback | Callback\<[AudioState](arkts-apis-audio-e.md#audiostate8)> | Yes | Callback used to return the audio status.| 2026 2027**Example** 2028 2029```ts 2030audioRenderer.on('stateChange', (state: audio.AudioState) => { 2031 if (state == 1) { 2032 console.info('audio renderer state is: STATE_PREPARED'); 2033 } 2034 if (state == 2) { 2035 console.info('audio renderer state is: STATE_RUNNING'); 2036 } 2037}); 2038``` 2039 2040## off('stateChange')<sup>18+</sup> 2041 2042off(type: 'stateChange', callback?: Callback<AudioState>): void 2043 2044Unsubscribes from the audio renderer state change event. This API uses an asynchronous callback to return the result. 2045 2046**System capability**: SystemCapability.Multimedia.Audio.Renderer 2047 2048**Parameters** 2049 2050| Name| Type | Mandatory| Description | 2051| :----- | :----- | :--- | :-------------------------------------------------- | 2052| type | string | Yes | Event type. The event **'stateChange'** is triggered when the state of the audio renderer is changed.| 2053| callback | Callback\<[AudioState](arkts-apis-audio-e.md#audiostate8)> | No| Callback used to return the audio status.| 2054 2055**Error codes** 2056 2057For details about the error codes, see [Audio Error Codes](errorcode-audio.md). 2058 2059| ID| Error Message| 2060| ------- | --------------------------------------------| 2061| 6800101 | Parameter verification failed. | 2062 2063**Example** 2064 2065```ts 2066// Cancel all subscriptions to the event. 2067audioRenderer.off('stateChange'); 2068 2069// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter. 2070let stateChangeCallback = (state: audio.AudioState) => { 2071 if (state == 1) { 2072 console.info('audio renderer state is: STATE_PREPARED'); 2073 } 2074 if (state == 2) { 2075 console.info('audio renderer state is: STATE_RUNNING'); 2076 } 2077}; 2078 2079audioRenderer.on('stateChange', stateChangeCallback); 2080 2081audioRenderer.off('stateChange', stateChangeCallback); 2082``` 2083 2084## on('outputDeviceChange')<sup>10+</sup> 2085 2086on(type: 'outputDeviceChange', callback: Callback\<AudioDeviceDescriptors>): void 2087 2088Subscribes to the audio output device change event, which is triggered when an audio output device is changed. This API uses an asynchronous callback to return the result. 2089 2090**System capability**: SystemCapability.Multimedia.Audio.Device 2091 2092**Parameters** 2093 2094| Name | Type | Mandatory| Description | 2095| :------- | :------------------------- | :--- | :------------------------------------------ | 2096| type | string | Yes | Event type. The event **'outputDeviceChange'** is triggered when an audio output device is changed.| 2097| callback | Callback\<[AudioDeviceDescriptors](arkts-apis-audio-t.md#audiodevicedescriptors)> | Yes | Callback used to return the output device descriptor of the current audio stream.| 2098 2099**Error codes** 2100 2101For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 2102 2103| ID| Error Message| 2104| ------- | --------------------------------------------| 2105| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2106| 6800101 | Parameter verification failed. | 2107 2108**Example** 2109 2110```ts 2111audioRenderer.on('outputDeviceChange', (deviceInfo: audio.AudioDeviceDescriptors) => { 2112 console.info(`DeviceInfo id: ${deviceInfo[0].id}`); 2113 console.info(`DeviceInfo name: ${deviceInfo[0].name}`); 2114 console.info(`DeviceInfo address: ${deviceInfo[0].address}`); 2115}); 2116``` 2117 2118## off('outputDeviceChange')<sup>10+</sup> 2119 2120off(type: 'outputDeviceChange', callback?: Callback\<AudioDeviceDescriptors>): void 2121 2122Unsubscribes from the audio output device change event. This API uses an asynchronous callback to return the result. 2123 2124**System capability**: SystemCapability.Multimedia.Audio.Device 2125 2126**Parameters** 2127 2128| Name | Type | Mandatory| Description | 2129| :------- | :------------------------- | :--- | :------------------------------------------ | 2130| type | string | Yes | Event type. The event **'outputDeviceChange'** is triggered when an audio output device is changed.| 2131| callback | Callback\<[AudioDeviceDescriptors](arkts-apis-audio-t.md#audiodevicedescriptors)> | No | Callback used to return the output device descriptor of the current audio stream.| 2132 2133**Error codes** 2134 2135For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 2136 2137| ID| Error Message| 2138| ------- | --------------------------------------------| 2139| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2140| 6800101 | Parameter verification failed. | 2141 2142**Example** 2143 2144```ts 2145// Cancel all subscriptions to the event. 2146audioRenderer.off('outputDeviceChange'); 2147 2148// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter. 2149let outputDeviceChangeCallback = (deviceInfo: audio.AudioDeviceDescriptors) => { 2150 console.info(`DeviceInfo id: ${deviceInfo[0].id}`); 2151 console.info(`DeviceInfo name: ${deviceInfo[0].name}`); 2152 console.info(`DeviceInfo address: ${deviceInfo[0].address}`); 2153}; 2154 2155audioRenderer.on('outputDeviceChange', outputDeviceChangeCallback); 2156 2157audioRenderer.off('outputDeviceChange', outputDeviceChangeCallback); 2158``` 2159 2160## on('outputDeviceChangeWithInfo')<sup>11+</sup> 2161 2162on(type: 'outputDeviceChangeWithInfo', callback: Callback\<AudioStreamDeviceChangeInfo>): void 2163 2164Subscribes to the change event of audio output devices and reasons, which is triggered when an audio output device is changed, and the change reason is reported. This API uses an asynchronous callback to return the result. 2165 2166**System capability**: SystemCapability.Multimedia.Audio.Device 2167 2168**Parameters** 2169 2170| Name | Type | Mandatory| Description | 2171| :------- |:-------------------------------------------------------------------------| :--- |:--------------------------------------------| 2172| type | string | Yes | Event type. The event **'outputDeviceChangeWithInfo'** is triggered when an audio output device is changed, and the change reason is reported.| 2173| callback | Callback\<[AudioStreamDeviceChangeInfo](arkts-apis-audio-i.md#audiostreamdevicechangeinfo11)> | Yes | Callback used to return the output device descriptor of the current audio stream and the change reason.| 2174 2175**Error codes** 2176 2177For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 2178 2179| ID| Error Message| 2180| ------- | --------------------------------------------| 2181| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2182| 6800101 | Parameter verification failed. | 2183 2184**Example** 2185 2186```ts 2187audioRenderer.on('outputDeviceChangeWithInfo', (deviceChangeInfo: audio.AudioStreamDeviceChangeInfo) => { 2188 console.info(`DeviceInfo id: ${deviceChangeInfo.devices[0].id}`); 2189 console.info(`DeviceInfo name: ${deviceChangeInfo.devices[0].name}`); 2190 console.info(`DeviceInfo address: ${deviceChangeInfo.devices[0].address}`); 2191 console.info(`Device change reason: ${deviceChangeInfo.changeReason}`); 2192}); 2193``` 2194 2195## off('outputDeviceChangeWithInfo')<sup>11+</sup> 2196 2197off(type: 'outputDeviceChangeWithInfo', callback?: Callback\<AudioStreamDeviceChangeInfo>): void 2198 2199Unsubscribes from the change event of audio output devices and reasons. This API uses an asynchronous callback to return the result. 2200 2201**System capability**: SystemCapability.Multimedia.Audio.Device 2202 2203**Parameters** 2204 2205| Name | Type | Mandatory| Description | 2206| :------- |:-------------------------------------------------------------------------| :--- |:--------------------------------------------| 2207| type | string | Yes | Event type. The event **'outputDeviceChangeWithInfo'** is triggered when an audio output device is changed, and the change reason is reported.| 2208| callback | Callback\<[AudioStreamDeviceChangeInfo](arkts-apis-audio-i.md#audiostreamdevicechangeinfo11)> | No | Callback used to return the output device descriptor of the current audio stream and the change reason.| 2209 2210**Error codes** 2211 2212For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 2213 2214| ID| Error Message| 2215| ------- | --------------------------------------------| 2216| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2217| 6800101 | Parameter verification failed. | 2218 2219**Example** 2220 2221```ts 2222// Cancel all subscriptions to the event. 2223audioRenderer.off('outputDeviceChangeWithInfo'); 2224 2225// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter. 2226let outputDeviceChangeWithInfoCallback = (deviceChangeInfo: audio.AudioStreamDeviceChangeInfo) => { 2227 console.info(`DeviceInfo id: ${deviceChangeInfo.devices[0].id}`); 2228 console.info(`DeviceInfo name: ${deviceChangeInfo.devices[0].name}`); 2229 console.info(`DeviceInfo address: ${deviceChangeInfo.devices[0].address}`); 2230 console.info(`Device change reason: ${deviceChangeInfo.changeReason}`); 2231}; 2232 2233audioRenderer.on('outputDeviceChangeWithInfo', outputDeviceChangeWithInfoCallback); 2234 2235audioRenderer.off('outputDeviceChangeWithInfo', outputDeviceChangeWithInfoCallback); 2236``` 2237 2238## on('writeData')<sup>11+</sup> 2239 2240on(type: 'writeData', callback: AudioRendererWriteDataCallback): void 2241 2242Subscribes to the audio data write event, which is triggered when audio data needs to be written. This API uses an asynchronous callback to return the result. 2243 2244The callback function is used only to write audio data. Do not call AudioRenderer APIs in it. 2245 2246**System capability**: SystemCapability.Multimedia.Audio.Renderer 2247 2248**Parameters** 2249 2250| Name | Type | Mandatory| Description | 2251| :------- |:--------------------------------| :--- |:--------------------------------------| 2252| type | string | Yes | Event type. The event **'writeData'** is triggered when audio data needs to be written.| 2253| callback | [AudioRendererWriteDataCallback](arkts-apis-audio-t.md#audiorendererwritedatacallback12) | Yes | Callback used to write the data to the buffer.<br>API version 11 does not support the return of the callback result. API version 12 and later support the return of the callback result [AudioDataCallbackResult](arkts-apis-audio-e.md#audiodatacallbackresult12). | 2254 2255**Error codes** 2256 2257For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 2258 2259| ID| Error Message| 2260| ------- | --------------------------------------------| 2261| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 2262| 6800101 | Parameter verification failed. | 2263 2264**Example** 2265 2266```ts 2267import { BusinessError } from '@kit.BasicServicesKit'; 2268import {fileIo as fs} from '@kit.CoreFileKit'; 2269import { common } from '@kit.AbilityKit'; 2270 2271class Options { 2272 offset?: number; 2273 length?: number; 2274} 2275 2276let bufferSize: number = 0; 2277// Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 2278let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 2279let path = context.cacheDir; 2280// Ensure that the resource exists in the path. 2281let filePath = path + '/StarWars10s-2C-48000-4SW.pcm'; 2282let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 2283let writeDataCallback = (buffer: ArrayBuffer) => { 2284 let options: Options = { 2285 offset: bufferSize, 2286 length: buffer.byteLength 2287 }; 2288 2289 try { 2290 fs.readSync(file.fd, buffer, options); 2291 bufferSize += buffer.byteLength; 2292 // This API does not return a callback result in API version 11, but does so in API version 12 and later versions. 2293 return audio.AudioDataCallbackResult.VALID; 2294 } catch (error) { 2295 console.error('Error reading file:', error); 2296 // This API does not return a callback result in API version 11, but does so in API version 12 and later versions. 2297 return audio.AudioDataCallbackResult.INVALID; 2298 } 2299}; 2300 2301audioRenderer.on('writeData', writeDataCallback); 2302audioRenderer.start().then(() => { 2303 console.info('Renderer started'); 2304}).catch((err: BusinessError) => { 2305 console.error(`ERROR: ${err}`); 2306}); 2307``` 2308 2309## off('writeData')<sup>11+</sup> 2310 2311off(type: 'writeData', callback?: AudioRendererWriteDataCallback): void 2312 2313Unsubscribes from the audio data write event. This API uses an asynchronous callback to return the result. 2314 2315**System capability**: SystemCapability.Multimedia.Audio.Renderer 2316 2317**Parameters** 2318 2319| Name | Type | Mandatory| Description | 2320| :------- |:--------------------------------| :--- |:--------------------------------------| 2321| type | string | Yes | Event type. The event **'writeData'** is triggered when audio data needs to be written.| 2322| callback | [AudioRendererWriteDataCallback](arkts-apis-audio-t.md#audiorendererwritedatacallback12) | No | Callback used to write the data to the buffer.<br>API version 11 does not support the return of the callback result. API version 12 and later support the return of the callback result [AudioDataCallbackResult](arkts-apis-audio-e.md#audiodatacallbackresult12).| 2323 2324**Error codes** 2325 2326For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Audio Error Codes](errorcode-audio.md). 2327 2328| ID| Error Message| 2329| ------- | --------------------------------------------| 2330| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2331| 6800101 | Parameter verification failed. | 2332 2333**Example** 2334 2335```ts 2336// Cancel all subscriptions to the event. 2337audioRenderer.off('writeData'); 2338 2339// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter. 2340let writeDataCallback = (data: ArrayBuffer) => { 2341 console.info(`write data: ${data}`); 2342}; 2343 2344audioRenderer.on('writeData', writeDataCallback); 2345 2346audioRenderer.off('writeData', writeDataCallback); 2347``` 2348## write<sup>(deprecated)</sup> 2349 2350write(buffer: ArrayBuffer, callback: AsyncCallback\<number>): void 2351 2352Writes the buffer. This API uses an asynchronous callback to return the result. 2353 2354> **NOTE** 2355> 2356> This API is supported since API version 8 and deprecated since API version 11. You are advised to use [on('writeData')](#onwritedata11) instead. 2357 2358**System capability**: SystemCapability.Multimedia.Audio.Renderer 2359 2360**Parameters** 2361 2362| Name | Type | Mandatory| Description | 2363| -------- | ---------------------- | ---- | --------------------------------------------------- | 2364| buffer | ArrayBuffer | Yes | Data to be written to the buffer. | 2365| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the number of bytes written; otherwise, **err** is an error object.| 2366 2367**Example** 2368 2369```ts 2370import { BusinessError } from '@kit.BasicServicesKit'; 2371import { fileIo as fs } from '@kit.CoreFileKit'; 2372import { common } from '@kit.AbilityKit'; 2373 2374let bufferSize: number; 2375class Options { 2376 offset?: number; 2377 length?: number; 2378} 2379audioRenderer.getBufferSize().then((data: number)=> { 2380 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 2381 bufferSize = data; 2382 console.info(`Buffer size: ${bufferSize}`); 2383 // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 2384 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 2385 let path = context.cacheDir; 2386 let filePath = path + '/StarWars10s-2C-48000-4SW.pcm'; 2387 let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 2388 fs.stat(filePath).then(async (stat: fs.Stat) => { 2389 let buf = new ArrayBuffer(bufferSize); 2390 let len = stat.size % bufferSize == 0 ? Math.floor(stat.size / bufferSize) : Math.floor(stat.size / bufferSize + 1); 2391 for (let i = 0;i < len; i++) { 2392 let options: Options = { 2393 offset: i * bufferSize, 2394 length: bufferSize 2395 }; 2396 let readSize: number = await fs.read(file.fd, buf, options); 2397 let writeSize: number = await new Promise((resolve,reject)=>{ 2398 audioRenderer.write(buf,(err: BusinessError, writeSize: number)=>{ 2399 if(err){ 2400 reject(err) 2401 }else{ 2402 resolve(writeSize) 2403 } 2404 }) 2405 }) 2406 } 2407 }); 2408 }).catch((err: BusinessError) => { 2409 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${err}`); 2410}); 2411``` 2412 2413## write<sup>(deprecated)</sup> 2414 2415write(buffer: ArrayBuffer): Promise\<number> 2416 2417Writes the buffer. This API uses a promise to return the result. 2418 2419> **NOTE** 2420> 2421> This API is supported since API version 8 and deprecated since API version 11. You are advised to use [on('writeData')](#onwritedata11) instead. 2422 2423**System capability**: SystemCapability.Multimedia.Audio.Renderer 2424 2425**Parameters** 2426 2427| Name | Type | Mandatory| Description | 2428| -------- | ---------------------- | ---- | --------------------------------------------------- | 2429| buffer | ArrayBuffer | Yes | Data to be written to the buffer. | 2430 2431**Return value** 2432 2433| Type | Description | 2434| ---------------- | ------------------------------------------------------------ | 2435| Promise\<number> | Promise used to return the number of written bytes.| 2436 2437**Example** 2438 2439```ts 2440import { BusinessError } from '@kit.BasicServicesKit'; 2441import { fileIo as fs } from '@kit.CoreFileKit'; 2442import { common } from '@kit.AbilityKit'; 2443 2444let bufferSize: number; 2445class Options { 2446 offset?: number; 2447 length?: number; 2448} 2449audioRenderer.getBufferSize().then((data: number) => { 2450 console.info(`AudioFrameworkRenderLog: getBufferSize: SUCCESS ${data}`); 2451 bufferSize = data; 2452 console.info(`BufferSize: ${bufferSize}`); 2453 // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 2454 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 2455 let path = context.cacheDir; 2456 let filePath = path + '/StarWars10s-2C-48000-4SW.pcm'; 2457 let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 2458 fs.stat(filePath).then(async (stat: fs.Stat) => { 2459 let buf = new ArrayBuffer(bufferSize); 2460 let len = stat.size % bufferSize == 0 ? Math.floor(stat.size / bufferSize) : Math.floor(stat.size / bufferSize + 1); 2461 for (let i = 0;i < len; i++) { 2462 let options: Options = { 2463 offset: i * bufferSize, 2464 length: bufferSize 2465 }; 2466 let readSize: number = await fs.read(file.fd, buf, options); 2467 try{ 2468 let writeSize: number = await audioRenderer.write(buf); 2469 } catch(err) { 2470 let error = err as BusinessError; 2471 console.error(`audioRenderer.write err: ${error}`); 2472 } 2473 } 2474 }); 2475}).catch((err: BusinessError) => { 2476 console.error(`AudioFrameworkRenderLog: getBufferSize: ERROR: ${err}`); 2477}); 2478``` 2479 2480## setRenderRate<sup>(deprecated)</sup> 2481 2482setRenderRate(rate: AudioRendererRate, callback: AsyncCallback\<void>): void 2483 2484Sets the render rate. This API uses an asynchronous callback to return the result. 2485 2486> **NOTE** 2487> 2488> This API is supported since API version 8 and deprecated since API version 11. You are advised to use [uninitialize][setSpeed](#setspeed11) instead. 2489 2490**System capability**: SystemCapability.Multimedia.Audio.Renderer 2491 2492**Parameters** 2493 2494| Name | Type | Mandatory| Description | 2495| -------- | ---------------------------------------- | ---- | ------------------------ | 2496| rate | [AudioRendererRate](arkts-apis-audio-e.md#audiorendererrate8) | Yes | Audio render rate. | 2497| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2498 2499**Example** 2500 2501```ts 2502import { BusinessError } from '@kit.BasicServicesKit'; 2503 2504audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL, (err: BusinessError) => { 2505 if (err) { 2506 console.error('Failed to set params'); 2507 } else { 2508 console.info('Callback invoked to indicate a successful render rate setting.'); 2509 } 2510}); 2511``` 2512 2513## setRenderRate<sup>(deprecated)</sup> 2514 2515setRenderRate(rate: AudioRendererRate): Promise\<void> 2516 2517Sets the render rate. This API uses a promise to return the result. 2518 2519> **NOTE** 2520> 2521> This API is supported since API version 8 and deprecated since API version 11. You are advised to use [uninitialize][setSpeed](#setspeed11) instead. 2522 2523**System capability**: SystemCapability.Multimedia.Audio.Renderer 2524 2525**Parameters** 2526 2527| Name| Type | Mandatory| Description | 2528| ------ | ---------------------------------------- | ---- | ------------ | 2529| rate | [AudioRendererRate](arkts-apis-audio-e.md#audiorendererrate8) | Yes | Audio render rate.| 2530 2531**Return value** 2532 2533| Type | Description | 2534| -------------- | ------------------------- | 2535| Promise\<void> | Promise that returns no value.| 2536 2537**Example** 2538 2539```ts 2540import { BusinessError } from '@kit.BasicServicesKit'; 2541 2542audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL).then(() => { 2543 console.info('setRenderRate SUCCESS'); 2544}).catch((err: BusinessError) => { 2545 console.error(`ERROR: ${err}`); 2546}); 2547``` 2548 2549## getRenderRate<sup>(deprecated)</sup> 2550 2551getRenderRate(callback: AsyncCallback\<AudioRendererRate>): void 2552 2553Obtains the audio renderer rate. This API uses an asynchronous callback to return the result. 2554 2555> **NOTE** 2556> 2557> This API is supported since API version 8 and deprecated since API version 11. You are advised to use [getSpeed](#getspeed11) instead. 2558 2559**System capability**: SystemCapability.Multimedia.Audio.Renderer 2560 2561**Parameters** 2562 2563| Name | Type | Mandatory| Description | 2564| -------- | ------------------------------------------------------- | ---- | ------------------ | 2565| callback | AsyncCallback<[AudioRendererRate](arkts-apis-audio-e.md#audiorendererrate8)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the render rate obtained; otherwise, **err** is an error object.| 2566 2567**Example** 2568 2569```ts 2570import { BusinessError } from '@kit.BasicServicesKit'; 2571 2572audioRenderer.getRenderRate((err: BusinessError, renderRate: audio.AudioRendererRate) => { 2573 console.info(`getRenderRate: ${renderRate}`); 2574}); 2575``` 2576 2577## getRenderRate<sup>(deprecated)</sup> 2578 2579getRenderRate(): Promise\<AudioRendererRate> 2580 2581Obtains the audio renderer rate. This API uses a promise to return the result. 2582 2583> **NOTE** 2584> 2585> This API is supported since API version 8 and deprecated since API version 11. You are advised to use [getSpeed](#getspeed11) instead. 2586 2587**System capability**: SystemCapability.Multimedia.Audio.Renderer 2588 2589**Return value** 2590 2591| Type | Description | 2592| ------------------------------------------------- | ------------------------- | 2593| Promise<[AudioRendererRate](arkts-apis-audio-e.md#audiorendererrate8)> | Promise used to return the render rate.| 2594 2595**Example** 2596 2597```ts 2598import { BusinessError } from '@kit.BasicServicesKit'; 2599 2600audioRenderer.getRenderRate().then((renderRate: audio.AudioRendererRate) => { 2601 console.info(`getRenderRate: ${renderRate}`); 2602}).catch((err: BusinessError) => { 2603 console.error(`ERROR: ${err}`); 2604}); 2605``` 2606 2607## getRenderRateSync<sup>(deprecated)</sup> 2608 2609getRenderRateSync(): AudioRendererRate 2610 2611Obtains the audio renderer rate. This API returns the result synchronously. 2612 2613> **NOTE** 2614> 2615> This API is supported since API version 10 and deprecated since API version 11. You are advised to use [getSpeed](#getspeed11) instead. 2616 2617**System capability**: SystemCapability.Multimedia.Audio.Renderer 2618 2619**Return value** 2620 2621| Type | Description | 2622| ------------------------------------------------- | ------------------------- | 2623| [AudioRendererRate](arkts-apis-audio-e.md#audiorendererrate8) | Audio render rate.| 2624 2625**Example** 2626 2627```ts 2628import { BusinessError } from '@kit.BasicServicesKit'; 2629 2630try { 2631 let renderRate: audio.AudioRendererRate = audioRenderer.getRenderRateSync(); 2632 console.info(`getRenderRate: ${renderRate}`); 2633} catch (err) { 2634 let error = err as BusinessError; 2635 console.error(`ERROR: ${error}`); 2636} 2637``` 2638 2639## setLoudnessGain<sup>20+</sup> 2640 2641setLoudnessGain(loudnessGain: number): Promise\<void> 2642 2643Sets the playback loudness. This API uses a promise to return the result. 2644 2645> **NOTE** 2646> 2647> - This API applies only to audio streams of the [STREAM_USAGE_MUSIC](../../reference/apis-audio-kit/arkts-apis-audio-e.md#streamusage), [STREAM_USAGE_MOVIE](../../reference/apis-audio-kit/arkts-apis-audio-e.md#streamusage), or [STREAM_USAGE_AUDIOBOOK](../../reference/apis-audio-kit/arkts-apis-audio-e.md#streamusage) type. 2648> - Loudness settings are not supported for high-definition channels. 2649> - Due to the buffer between the audio framework and hardware, there may be a delay in the actual effect of loudness adjustment. The delay duration depends on the buffer length. 2650> - You are advised to set the loudness before starting playback of different audio streams to achieve the optimal balance effect. 2651 2652**System capability**: SystemCapability.Multimedia.Audio.Renderer 2653 2654**Parameters** 2655 2656| Name | Type | Mandatory| Description | 2657| ------------ | -------| ---- |------------------------------------------ | 2658| loudnessGain | number | Yes | Loudness, in the range [-90.0, 24.0], in dB. The default value is 0.0 dB. | 2659 2660**Return value** 2661 2662| Type | Description | 2663| -------------- | ------------------------- | 2664| Promise\<void> | Promise that returns no value. | 2665 2666**Error codes** 2667 2668For details about the error codes, see [Audio Error Codes](errorcode-audio.md). 2669 2670| ID| Error Message| 2671| ------- | --------------------------------------------| 2672| 6800101 | Parameter verification failed. | 2673| 6800104 | Operation is not supported on this renderer, e.g. the stream usage of this renderer is not one of [STREAM_USAGE_MUSIC](../../reference/apis-audio-kit/arkts-apis-audio-e.md#streamusage), <br>[STREAM_USAGE_MOVIE](../../reference/apis-audio-kit/arkts-apis-audio-e.md#streamusage), or [STREAM_USAGE_AUDIOBOOK](../../reference/apis-audio-kit/arkts-apis-audio-e.md#streamusage), or this renderer is routed through the high-resolution playback path. | 2674 2675**Example** 2676 2677```ts 2678audioRenderer.setLoudnessGain(1.0); 2679``` 2680 2681## getLoudnessGain<sup>20+</sup> 2682 2683getLoudnessGain(): number 2684 2685Obtains the playback loudness. 2686 2687**System capability**: SystemCapability.Multimedia.Audio.Renderer 2688 2689**Return value** 2690 2691| Type | Description | 2692|------- |----------------- | 2693| number | Playback loudness.| 2694 2695**Example** 2696 2697```ts 2698let loudnessGain = audioRenderer.getLoudnessGain(); 2699``` 2700