1# SoundPool (Sound Pool) 2 3The **SoundPool** module provides APIs for loading, unloading, playing, and stopping playing system sounds, setting the volume, and setting the number of loops. 4 5Before using these APIs, you must call [media.createSoundPool](js-apis-media.md#mediacreatesoundpool10) to create a **SoundPool** instance. 6 7> **NOTE** 8> 9> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 10 11## Modules to Import 12 13```js 14import media from '@ohos.multimedia.media'; 15import audio from '@ohos.multimedia.audio'; 16``` 17 18## PlayParameters 19 20Describes the playback parameters of the sound pool. 21 22These parameters are used to control the playback volume, number of loops, and priority. 23 24**System capability**: SystemCapability.Multimedia.Media.SoundPool 25 26| Name | Type | Mandatory| Description | 27| --------------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | 28| loop | number | No | Number of loops. The value **0** means that the sound does not loop (the sound is played once), and **-1** means that the sound loops forever. | 29| rate | number | No | Playback rate. For details, see [AudioRendererRate](js-apis-audio.md#AudioRendererRate).| 30| leftVolume | number | No | Volume of the left channel. The value ranges from 0 to 1. | 31| rightVolume | number | No | Volume of the right channel. (Currently, the volume cannot be set separately for the left and right channels. The volume set for the left channel is used.)| 32| priority | number | No | Playback priority. The value **0** means the lowest priority. A larger value indicates a higher priority. | 33| parallelPlayFlag | boolean | No | Whether the sound can be played in parallel with other active audio streams. The value **true** means that the sound can be played in parallel with other active audio streams, without preempting the audio focus, and **false** means the opposite.| 34 35## SoundPool 36 37Implements a sound pool that provides APIs for loading, unloading, playing, and stopping playing system sounds, setting the volume, and setting the number of loops. Before using these APIs, you must call [createSoundPool](js-apis-media.md#mediacreatesoundpool10) to create a **SoundPool** instance. 38 39### load 40 41load(uri: string, callback: AsyncCallback\<number>): void 42 43Loads a sound. This API uses an asynchronous callback to obtain the sound ID. The input parameter **uri** is a string starting with fd://, which is generated based on the file descriptor (FD) obtained. 44 45**System capability**: SystemCapability.Multimedia.Media.SoundPool 46 47**Parameters** 48 49| Name | Type | Mandatory| Description | 50| -------- | -------------------------------------- | ---- | ------------------------------------- | 51| uri | string | Yes | URI of the audio file to load. Generally, the URI starts with fd://.| 52| callback | AsyncCallback\<number> | Yes | Callback used to return the ID of the sound loaded. A valid value must be greater than 0.| 53 54**Error codes** 55 56For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 57 58| ID| Error Message | 59| -------- | --------------------------------------- | 60| 5400102 | Operation not allowed. Return by callback.| 61| 5400103 | I/O error. Return by callback. | 62| 5400105 | Service died. Return by callback. | 63 64**Example** 65 66```js 67let uri:string = ""; 68 69// Obtain the URI starting with fd://. 70await fs.open('/test_01.mp3', fs.OpenMode.READ_ONLY).then((file: fs.File) => { 71 console.info("file fd: " + file.fd); 72 uri = 'fd://' + (file.fd).toString() 73}); // '/test_01.mp3' here is only an example. You need to pass in the actual URI. 74 75soundPool.load(uri, (error, soundId_: number) => { 76 if (error) { 77 console.info(`load soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 78 } else { 79 console.info(`load soundPool Success` + JSON.stringify(soundId_)) 80 } 81}) 82``` 83 84### load 85 86load(uri: string): Promise\<number> 87 88Loads a sound. This API uses a promise to obtain the sound ID. The input parameter **uri** is a starting with fd://, which is generated based on the FD obtained. 89 90**System capability**: SystemCapability.Multimedia.Media.SoundPool 91 92**Parameters** 93 94| Name| Type | Mandatory| Description | 95| ------ | -------------------------------------- | ---- | -------------------------- | 96| uri | string | Yes | URI of the audio file to load. Generally, the URI starts with fd://.| 97 98**Return value** 99 100| Type | Description | 101| -------------- | ------------------------------------------ | 102| Promise\<number> | Promise used to return the ID of the sound loaded. A valid value must be greater than 0.| 103 104**Example** 105 106```js 107let uri:string = ""; 108let soundID: number; 109 110// Obtain the URI starting with fd://. 111await fs.open('/test_01.mp3', fs.OpenMode.READ_ONLY).then((file: fs.File) => { 112 console.info("file fd: " + file.fd); 113 uri = 'fd://' + (file.fd).toString() 114}); // '/test_01.mp3' here is only an example. You need to pass in the actual URI. 115 116soundPool.load(uri).then((soundId: number) => { 117 console.info('soundPool load uri success'); 118 soundID = soundId; 119}).catch((err) => { 120 console.error('soundPool load failed and catch error is ' + err.message); 121}); 122 123``` 124 125### load 126 127load(fd: number, offset: number, length: number, callback: AsyncCallback\<number>): void 128 129Loads a sound. This API uses an asynchronous callback to obtain the sound ID. The input parameter **fd** can be manually input or automatically obtained by reading the embedded resource of the application. 130 131**System capability**: SystemCapability.Multimedia.Media.SoundPool 132 133**Parameters** 134 135| Name | Type | Mandatory| Description | 136| -------- | ---------------------- | ---- | --------------------------- | 137| fd | number | Yes | Resource handle, which is obtained by calling **resourceManager.getRawFileDescriptor**. | 138| offset | number | Yes | Resource offset, which needs to be entered based on the preset resource information. An invalid value causes a failure to parse audio and video resources.| 139| length | number | Yes | Resource length, which needs to be entered based on the preset resource information. An invalid value causes a failure to parse audio and video resources.| 140| callback | AsyncCallback\<number> | Yes | Callback used to return the sound ID. A valid value must be greater than 0.| 141 142**Error codes** 143 144For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 145 146| ID| Error Message | 147| -------- | --------------------------------------- | 148| 5400102 | Operation not allowed. Return by callback. | 149| 5400103 |I/O error. Return by callback. | 150| 5400105 | Service died. Return by callback. | 151 152**Example** 153 154```js 155let fd: number; 156let soundID: number; 157let fileSize: number; 158let maxOffset: number; 159 160// Obtain the FD. 161await fs.open('/test_01.mp3', fs.OpenMode.READ_ONLY).then((file: fs.File) => { 162 console.info("file fd: " + file.fd); 163 uri = 'fd://' + (file.fd).toString() 164}); // '/test_01.mp3' here is only an example. You need to pass in the actual URI. 165let stat: Stat = await fs.stat('/test_01.mp3'); 166fileSize = stat.size; 167maxOffset = stat.size; 168 169soundPool.load(fd, 0, fileSize, (error, soundId_: number) => { 170 if (error) { 171 console.info(`load soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 172 } else { 173 soundID = soundId_; 174 console.info('load success soundid:' + soundId_); 175 } 176}) 177 178``` 179 180### load 181 182load(fd: number, offset: number, length: number): Promise\<number> 183 184Loads a sound. This API uses a promise to obtain the sound ID. The input parameter **fd** can be manually input or automatically obtained by reading the embedded resource of the application. 185 186**System capability**: SystemCapability.Multimedia.Media.SoundPool 187 188**Parameters** 189 190| Name | Type | Mandatory| Description | 191| -------- | ---------------------- | ---- | --------------------------- | 192| fd | number | Yes | Resource handle, which is obtained by calling **resourceManager.getRawFileDescriptor**. | 193| offset | number | Yes | Resource offset, which needs to be entered based on the preset resource information. An invalid value causes a failure to parse audio and video resources.| 194| length | number | Yes | Resource length, which needs to be entered based on the preset resource information. An invalid value causes a failure to parse audio and video resources.| 195 196**Return value** 197 198| Type | Description | 199| ---------------- | -------------------------------- | 200| Promise\<number> | Promise used to return the sound ID. A valid value must be greater than 0.| 201 202**Example** 203 204```js 205let fd: number; 206let soundID: number; 207let fileSize: number; 208let maxOffset: number; 209 210// Obtain the FD. 211await fs.open('/test_01.mp3', fs.OpenMode.READ_ONLY).then((file: fs.File) => { 212 console.info("file fd: " + file.fd); 213 uri = 'fd://' + (file.fd).toString() 214}); // '/test_01.mp3' here is only an example. You need to pass in the actual URI. 215let stat = await fs.stat('/test_01.mp3'); 216fileSize = stat.size; 217maxOffset = stat.size; 218 219soundPool.load(fd, 0, fileSize).then((soundId: number) => { 220 console.info('load success'); 221 soundID = soundId; 222}).catch((err) => { 223 console.error('soundpool load failed and catch error is ' + err.message); 224}); 225``` 226 227### play 228 229play(soundID: number, params: PlayParameters, callback: AsyncCallback\<number>): void 230 231Plays a sound. This API uses an asynchronous callback to obtain the audio stream ID. 232 233**System capability**: SystemCapability.Multimedia.Media.SoundPool 234 235**Parameters** 236 237| Name | Type | Mandatory| Description | 238| -------- | ---------------------- | ---- | --------------------------- | 239| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 240| params | [PlayParameters](#playparameters) | Yes | Playback parameters.| 241| callback | AsyncCallback\<number> | Yes | Callback used to return the ID of the audio stream. A valid value must be greater than 0.| 242 243**Error codes** 244 245For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 246 247| ID| Error Message | 248| -------- | --------------------------------------- | 249| 5400105 | Service died. Return by callback. | 250 251**Example** 252 253```js 254let soundID: number; 255let streamID: number; 256let PlayParameters: media.PlayParameters = { 257 loop: number = 3, // The sound is played four times (three loops). 258 rate: audio.AudioRendererRate = audio.AudioRendererRate.RENDER_RATE_NORMAL, // The sound is played at the original frequency. 259 leftVolume: number = 0.5, // range = 0.0-1.0 260 rightVolume: number = 0.5, // range = 0.0-1.0 261 priority: number = 0 // The sound playback has the lowest priority. 262 parallelPlayFlag: boolean = false // The sound is not played in parallel with other active audio streams. 263 } 264soundPool.play(soundID, PlayParameters, (error, streamId: number) => { 265 if (error) { 266 console.info(`play sound Error: errCode is ${error.code}, errMessage is ${error.message}`) 267 } else { 268 streamID = streamId; 269 console.info('play success soundid:' + streamId); 270 } 271}) 272``` 273 274### play 275 276play(soundID: number, callback: AsyncCallback\<number>): void 277 278Plays a sound. This API uses an asynchronous callback to obtain the audio stream ID. 279 280**System capability**: SystemCapability.Multimedia.Media.SoundPool 281 282**Parameters** 283 284| Name | Type | Mandatory| Description | 285| -------- | ---------------------- | ---- | --------------------------- | 286| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 287| callback | AsyncCallback\<number> | Yes | Callback used to return the ID of the audio stream. A valid value must be greater than 0.| 288 289**Error codes** 290 291For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 292 293| ID| Error Message | 294| -------- | --------------------------------------- | 295| 5400105 | Service died. Return by callback. | 296 297**Example** 298 299```js 300let soundID: number; 301let streamID: number; 302soundPool.play(soundID, (error, streamId: number) => { 303 if (error) { 304 console.info(`play sound Error: errCode is ${error.code}, errMessage is ${error.message}`) 305 } else { 306 streamID = streamId; 307 console.info('play success soundid:' + streamId); 308 } 309}) 310``` 311 312### play 313 314play(soundID: number, params?: PlayParameters): Promise\<number> 315 316Plays a sound. This API uses a promise to obtain the audio stream ID. 317 318**System capability**: SystemCapability.Multimedia.Media.SoundPool 319 320**Parameters** 321 322| Name | Type | Mandatory| Description | 323| -------- | ---------------------- | ---- | --------------------------- | 324| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 325| params | [PlayParameters](#playparameters) | No | Playback parameters.| 326 327**Return value** 328 329| Type | Description | 330| ---------------- | -------------------------------- | 331| Promise\<number> | Promise used to return the ID of the audio stream. A valid value must be greater than 0.| 332 333**Example** 334 335```js 336let soundID: number; 337let streamID: number; 338let PlayParameters: media.PlayParameters = { 339 loop: number = 3, // The sound is played four times (three loops). 340 rate: audio.AudioRendererRate = audio.AudioRendererRate.RENDER_RATE_NORMAL, // The sound is played at the original frequency. 341 leftVolume: number = 0.5, // range = 0.0-1.0 342 rightVolume: number = 0.5, // range = 0.0-1.0 343 priority: number = 0 // The sound playback has the lowest priority. 344 parallelPlayFlag: boolean = false // The sound is not played in parallel with other active audio streams. 345 } 346 347soundPool.play(soundID, PlayParameters).then((streamId: number) => { 348 console.info('play success'); 349 streamID = streamId; 350}).catch((err) => { 351 console.error('soundpool play failed and catch error is ' + err.message); 352}); 353``` 354 355### stop 356 357stop(streamID: number, callback: AsyncCallback\<void>): void 358 359Stops playing a sound. This API uses an asynchronous callback to return the result. 360 361**System capability**: SystemCapability.Multimedia.Media.SoundPool 362 363**Parameters** 364 365| Name | Type | Mandatory| Description | 366| -------- | ---------------------- | ---- | --------------------------- | 367| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 368| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 369 370**Error codes** 371 372For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 373 374| ID| Error Message | 375| -------- | --------------------------------------- | 376| 5400105 | Service died. Return by callback. | 377 378**Example** 379 380```js 381let streamID: number; 382// Call play() to obtain the stream ID. 383soundPool.stop(streamID, (error) => { 384 if (error) { 385 console.info(`stop sound Error: errCode is ${error.code}, errMessage is ${error.message}`) 386 } else { 387 console.info('stop success'); 388 } 389}) 390 391``` 392 393### stop 394 395stop(streamID: number): Promise\<void> 396 397Stops playing a sound. This API uses a promise to return the result. 398 399**System capability**: SystemCapability.Multimedia.Media.SoundPool 400 401**Parameters** 402 403| Name | Type | Mandatory| Description | 404| -------- | ---------------------- | ---- | --------------------------- | 405| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 406 407**Return value** 408 409| Type | Description | 410| ---------------- | -------------------------------- | 411| Promise\<void> | Promise that returns no value.| 412 413**Example** 414 415```js 416let streamID: number; 417// Call play() to obtain the stream ID. 418 419soundPool.stop(streamID).then(() => { 420 console.info('stop success'); 421}).catch((err) => { 422 console.error('soundpool load stop and catch error is ' + err.message); 423}); 424``` 425 426### setLoop 427 428setLoop(streamID: number, loop: number, callback: AsyncCallback\<void>): void; 429 430Sets the loop mode for an audio stream. This API uses an asynchronous callback to return the result. 431 432**System capability**: SystemCapability.Multimedia.Media.SoundPool 433 434**Parameters** 435 436| Name | Type | Mandatory| Description | 437| -------- | ---------------------- | ---- | --------------------------- | 438| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 439| loop | number | Yes | Number of loops. The value **0** means that the sound does not loop (the sound is played once), and a value smaller than **0** means that the sound loops forever.| 440| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 441 442**Error codes** 443 444For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 445 446| ID| Error Message | 447| -------- | --------------------------------------- | 448| 5400102 | Operate not permit. Return by callback. | 449| 5400105 | Service died. Return by callback. | 450 451**Example** 452 453```js 454let streamID: number; 455// Call play() to obtain the stream ID. 456// Set the number of loops to 2. 457soundPool.setLoop(streamID, 2, (error) => { 458 if (error) { 459 console.info(`setLoop soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 460 } else { 461 console.info('setLoop success streamID:' + streamID); 462 } 463}) 464``` 465 466### setLoop 467 468setLoop(streamID: number, loop: number): Promise\<void> 469 470Sets the loop mode for an audio stream. This API uses a promise to return the result. 471 472**System capability**: SystemCapability.Multimedia.Media.SoundPool 473 474**Parameters** 475 476| Name | Type | Mandatory| Description | 477| -------- | ---------------------- | ---- | --------------------------- | 478| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 479| loop | number | Yes | Number of loops. The value **0** means that the sound does not loop (the sound is played once), and a value smaller than **0** means that the sound loops forever.| 480 481**Return value** 482 483| Type | Description | 484| ---------------- | -------------------------------- | 485| Promise\<void> | Promise that returns no value.| 486 487**Example** 488 489```js 490let streamID: number; 491// Call play() to obtain the stream ID. 492// Set the number of loops to 1. 493soundPool.setLoop(streamID, 1).then(() => { 494 console.info('setLoop success streamID:' + streamID); 495}).catch((err) => { 496 console.error('soundpool setLoop failed and catch error is ' + err.message); 497}); 498``` 499 500### setPriority 501 502setPriority(streamID: number, priority: number, callback: AsyncCallback\<void>): void 503 504Sets the priority for an audio stream. This API uses an asynchronous callback to return the result. 505 506**System capability**: SystemCapability.Multimedia.Media.SoundPool 507 508**Parameters** 509 510| Name | Type | Mandatory| Description | 511| -------- | ---------------------- | ---- | --------------------------- | 512| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 513| priority | number | Yes | Priority. The value **0** means the lowest priority.| 514| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 515 516**Error codes** 517 518For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 519 520| ID| Error Message | 521| -------- | --------------------------------------- | 522| 5400102 | Operate not permit. Return by callback. | 523| 5400105 | Service died. Return by callback. | 524 525**Example** 526 527```js 528let streamID: number; 529// Call play() to obtain the stream ID. 530// Set the priority to 1. 531soundPool.setPriority(streamID, 1, (error) => { 532 if (error) { 533 console.info(`setPriority soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 534 } else { 535 console.info('setPriority success streamID:' + streamID); 536 } 537}) 538 539``` 540 541### setPriority 542 543setPriority(streamID: number, priority: number): Promise\<void> 544 545Sets the priority for an audio stream. This API uses a promise to return the result. 546 547**System capability**: SystemCapability.Multimedia.Media.SoundPool 548 549**Parameters** 550 551| Name | Type | Mandatory| Description | 552| -------- | ---------------------- | ---- | --------------------------- | 553| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 554| priority | number | Yes | Priority. The value **0** means the lowest priority.| 555 556**Return value** 557 558| Type | Description | 559| ---------------- | -------------------------------- | 560| Promise\<void> | Promise that returns no value.| 561 562**Example** 563 564```js 565let streamID: number; 566// Call play() to obtain the stream ID. 567// Set the priority to 1. 568 569soundPool.setPriority(streamID, 1).then(() => { 570 console.info('setPriority success'); 571}).catch((err) => { 572 console.error('soundpool setPriority failed and catch error is ' + err.message); 573}); 574``` 575 576### setRate 577 578setRate(streamID: number, rate: audio.AudioRendererRate, callback: AsyncCallback\<void>): void 579 580Sets the playback rate for an audio stream. This API uses an asynchronous callback to return the result. 581 582**System capability**: SystemCapability.Multimedia.Media.SoundPool 583 584**Parameters** 585 586| Name | Type | Mandatory| Description | 587| -------- | ---------------------- | ---- | --------------------------- | 588| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 589| rate | [audio.AudioRendererRate](js-apis-audio.md#audiorendererrate8) | Yes | Playback rate.| 590| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 591 592**Error codes** 593 594For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 595 596| ID| Error Message | 597| -------- | --------------------------------------- | 598| 5400102 | Operate not permit. Return by callback. | 599| 5400105 | Service died. Return by callback. | 600 601**Example** 602 603```js 604let streamID: number; 605let selectedAudioRendererRate: audio.AudioRendererRate = audio.AudioRendererRate.RENDER_RATE_NORMAL; // The sound is played at the original frequency. 606// Call play() to obtain the stream ID. 607 608soundPool.setRate(streamID, selectedAudioRendererRate, (error) => { 609 if (error) { 610 console.info(`setRate soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 611 } else { 612 console.info('setRate success streamID:' + streamID); 613 } 614}) 615 616``` 617 618### setRate 619 620setRate(streamID: number, rate: audio.AudioRendererRate): Promise\<void> 621 622Sets the playback rate for an audio stream. This API uses a promise to return the result. 623 624**System capability**: SystemCapability.Multimedia.Media.SoundPool 625 626**Parameters** 627 628| Name | Type | Mandatory| Description | 629| -------- | ---------------------- | ---- | --------------------------- | 630| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 631| rate | [audio.AudioRendererRate](js-apis-audio.md#audiorendererrate8) | Yes | Playback rate.| 632 633**Return value** 634 635| Type | Description | 636| ---------------- | -------------------------------- | 637| Promise\<void> | Promise that returns no value.| 638 639**Example** 640 641```js 642let streamID: number; 643let selectedAudioRendererRate: audio.AudioRendererRate = audio.AudioRendererRate.RENDER_RATE_NORMAL; // The sound is played at the original frequency. 644// Call play() to obtain the stream ID. 645 646soundPool.setRate(streamID, selectedAudioRendererRate).then(() => { 647 console.info('setRate success'); 648}).catch((err) => { 649 console.error('soundpool setRate failed and catch error is ' + err.message); 650}); 651``` 652 653### setVolume 654 655setVolume(streamID: number, leftVolume: number, rightVolume: number, callback: AsyncCallback\<void>): void 656 657Sets the volume for an audio stream. This API uses an asynchronous callback to return the result. 658 659**System capability**: SystemCapability.Multimedia.Media.SoundPool 660 661**Parameters** 662 663| Name | Type | Mandatory| Description | 664| -------- | ---------------------- | ---- | --------------------------- | 665| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 666| leftVolume | number | Yes | Volume of the left channel. The value ranges from 0.0 to 1.0.| 667| rightVolume | number | Yes | Volume of the right channel. Currently, setting the volume for the right channel does not take effect. The volume set for the left channel is used.| 668| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 669 670**Error codes** 671 672For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 673 674| ID| Error Message | 675| -------- | --------------------------------------- | 676| 5400102 | Operate not permit. Return by callback. | 677| 5400105 | Service died. Return by callback. | 678 679**Example** 680 681```js 682let streamID: number; 683// Call play() to obtain the stream ID. 684// Set the volume to 0.5. 685soundPool.setVolume(streamID, 0.5, 0.5, (error) => { 686 if (error) { 687 console.info(`setVolume soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 688 } else { 689 console.info('setVolume success streamID:' + streamID); 690 } 691}) 692 693``` 694 695### setVolume 696 697setVolume(streamID: number, leftVolume: number, rightVolume: number): Promise<void> 698 699Sets the volume for an audio stream. This API uses a promise to return the result. 700 701**System capability**: SystemCapability.Multimedia.Media.SoundPool 702 703**Parameters** 704 705| Name | Type | Mandatory| Description | 706| -------- | ---------------------- | ---- | --------------------------- | 707| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 708| leftVolume | number | Yes | Volume of the left channel. The value ranges from 0.0 to 1.0.| 709| rightVolume | number | Yes | Volume of the right channel. Currently, setting the volume for the right channel does not take effect. The volume set for the left channel is used.| 710 711**Return value** 712 713| Type | Description | 714| ---------------- | -------------------------------- | 715| Promise\<void> | Promise that returns no value.| 716 717**Example** 718 719```js 720let streamID: number; 721selectedAudioRendererRate: number = 0; // The sound plays at the original frequency. 722// Call play() to obtain the stream ID. 723 724soundPool.setVolume(streamID, 0.5, 0.5).then(() => { 725 console.info('setVolume success'); 726}).catch((err) => { 727 console.error('soundpool setVolume failed and catch error is ' + err.message); 728}); 729``` 730 731### unload 732 733unload(soundID: number, callback: AsyncCallback\<void>): void 734 735Unloads a sound. This API uses an asynchronous callback to return the result. 736 737**System capability**: SystemCapability.Multimedia.Media.SoundPool 738 739**Parameters** 740 741| Name | Type | Mandatory| Description | 742| -------- | ---------------------- | ---- | --------------------------- | 743| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 744| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 745 746**Error codes** 747 748For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 749 750| ID| Error Message | 751| -------- | --------------------------------------- | 752| 5400102 | Operation not allowed. Return by callback. | 753| 5400103 | I/O error. Return by callback. | 754| 5400105 | Service died. Return by callback. | 755 756**Example** 757 758```js 759let soundID: number; 760// Call load() to obtain the sound ID. 761soundPool.unload(soundID, (error) => { 762 if (error) { 763 console.info(`unload soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 764 } else { 765 console.info('unload success:'); 766 } 767}) 768 769``` 770 771### unload 772 773unload(soundID: number): Promise\<void> 774 775Unloads a sound. This API uses a promise to return the result. 776 777**System capability**: SystemCapability.Multimedia.Media.SoundPool 778 779**Parameters** 780 781| Name | Type | Mandatory| Description | 782| -------- | ---------------------- | ---- | --------------------------- | 783| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 784 785**Return value** 786 787| Type | Description | 788| ---------------- | -------------------------------- | 789| Promise\<void> | Promise that returns no value.| 790 791**Example** 792 793```js 794let soundID: number; 795// Call load() to obtain the sound ID. 796 797soundPool.unload(soundID).then(() => { 798 console.info('unload success'); 799}).catch((err) => { 800 console.error('soundpool unload failed and catch error is ' + err.message); 801}); 802``` 803 804### release 805 806release(callback: AsyncCallback\<void>): void 807 808Releases this **SoundPool** instance. This API uses an asynchronous callback to return the result. 809 810**System capability**: SystemCapability.Multimedia.Media.SoundPool 811 812**Parameters** 813 814| Name | Type | Mandatory| Description | 815| -------- | ---------------------- | ---- | --------------------------- | 816| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 817 818**Error codes** 819 820For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 821 822| ID| Error Message | 823| -------- | --------------------------------------- | 824| 5400105 | Service died. Return by callback. | 825 826**Example** 827 828```js 829 830soundPool.release((error) => { 831 if (error) { 832 console.info(`release soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 833 } else { 834 console.info('release success'); 835 } 836}) 837 838``` 839 840### release 841 842release(): Promise\<void> 843 844Releases this **SoundPool** instance. This API uses a promise to return the result. 845 846**System capability**: SystemCapability.Multimedia.Media.SoundPool 847 848**Return value** 849 850| Type | Description | 851| ---------------- | -------------------------------- | 852| Promise\<void> | Promise that returns no value.| 853 854**Example** 855 856```js 857 858soundPool.release().then(() => { 859 console.info('release success'); 860}).catch((err) => { 861 console.error('soundpool release failed and catch error is ' + err.message); 862}); 863``` 864 865### on('loadComplete') 866 867on(type: 'loadComplete', callback: Callback<number>): void 868 869Subscribes to events indicating that a sound finishes loading. 870 871**System capability**: SystemCapability.Multimedia.Media.SoundPool 872 873**Parameters** 874 875| Name | Type | Mandatory| Description | 876| -------- | -------- | ---- | ------------------------------------------------------------ | 877| type | string | Yes | Event type, which is **'loadComplete'** in this case. This event is triggered when a sound is loaded.| 878| callback | Callback\<number> | Yes | ID of the sound that has been loaded. | 879 880**Error codes** 881 882For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 883 884| ID| Error Message | 885| -------- | --------------------------------------- | 886| 5400103 | IO error. Return by callback. | 887| 5400105 | Service died. Return by callback. | 888 889**Example** 890 891```js 892soundPool.on('loadComplete', (soundId: number) => { 893 console.info('loadComplete success, soundId: ' + soundId) 894}) 895``` 896 897### off('loadComplete') 898 899off(type: 'loadComplete'): void 900 901Unsubscribes from events indicating that a sound finishes loading. 902 903**System capability**: SystemCapability.Multimedia.Media.SoundPool 904 905**Parameters** 906 907| Name| Type | Mandatory| Description | 908| ------ | ------ | ---- | ------------------------------------------------------------ | 909| type | string | Yes | Event type. The value is fixed at **'loadComplete'**.| 910 911**Example** 912 913```js 914soundPool.off('loadComplete') 915``` 916 917### on('playFinished') 918 919on(type: 'playFinished', callback: Callback<void>): void 920 921Subscribes to events indicating that a sound finishes playing. 922 923**System capability**: SystemCapability.Multimedia.Media.SoundPool 924 925**Parameters** 926 927| Name | Type | Mandatory| Description | 928| -------- | -------- | ---- | ------------------------------------------------------------ | 929| type | string | Yes | Event type, which is **'playFinished'** in this case. This event is triggered when a sound finishes playing.| 930| callback | Callback\<void> | Yes | Callback used to return the result. | 931 932**Error codes** 933 934For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 935 936| ID| Error Message | 937| -------- | --------------------------------------- | 938| 5400103 | IO error. Return by callback. | 939| 5400105 | Service died. Return by callback. | 940 941**Example** 942 943```js 944soundPool.on('playFinished', () => { 945 console.info('playFinished success') 946}) 947``` 948 949### off('playFinished') 950 951off(type: 'playFinished'): void 952 953Unsubscribes from events indicating that a sound finishes playing. 954 955**System capability**: SystemCapability.Multimedia.Media.SoundPool 956 957**Parameters** 958 959| Name| Type | Mandatory| Description | 960| ------ | ------ | ---- | ------------------------------------------------------------ | 961| type | string | Yes | Event type. The value is fixed at **'playFinished'**.| 962 963**Example** 964 965```js 966soundPool.off('playFinished') 967``` 968 969### on('error') 970 971on(type: 'error', callback: ErrorCallback): void 972 973Subscribes to error events of this **SoundPool** instance. This event is used only for error prompt. 974 975**System capability**: SystemCapability.Multimedia.Media.SoundPool 976 977**Parameters** 978 979| Name | Type | Mandatory| Description | 980| -------- | -------- | ---- | ------------------------------------------------------------ | 981| type | string | Yes | Event type, which is **'error'** in this case. This event can be triggered by both user operations and the system.| 982| callback | function | Yes | Callback used to return the error code ID and error message.| 983 984The **SoundPool** class provides the following error types<a name = error_info></a>: 985 986| ID| Error Message | Description | 987| -------- | --------------------- | ------------------------------------------------------------ | 988| 401 | Invalid Parameter: | Incorrect input parameter, causing an invalid call. | 989| 801 | Unsupport Capability: | Unsupported API, causing an invalid call. | 990| 5400101 | No Memory: | Insufficient memory.| 991| 5400102 | Operation Not Allowed: | Unsupported operation in the current state, causing an invalid call. | 992| 5400103 | IO Error: | I/O exception.| 993| 5400105 | Service Died: | The playback process is dead, and the service on which the sound pool depends is abnormal.| 994 995**Example** 996 997```js 998soundPool.on('error', (error) => { 999 console.error('error happened,and error message is :' + error.message) 1000 console.error('error happened,and error code is :' + error.code) 1001}) 1002``` 1003 1004### off('error') 1005 1006off(type: 'error'): void 1007 1008Unsubscribes from error events of this **SoundPool** instance. 1009 1010**System capability**: SystemCapability.Multimedia.Media.SoundPool 1011 1012**Parameters** 1013 1014| Name| Type | Mandatory| Description | 1015| ------ | ------ | ---- | ----------------------------------------- | 1016| type | string | Yes | Event type, which is **'error'** in this case.| 1017 1018**Example** 1019 1020```js 1021soundPool.off('error') 1022``` 1023