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.<br>This is a system API.| 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```ts 67import fs from '@ohos.file.fs'; 68import { BusinessError } from '@ohos.base'; 69 70// Create a SoundPool instance. 71let soundPool: media.SoundPool; 72let audioRendererInfo: audio.AudioRendererInfo = { 73 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 74 rendererFlags: 1 75} 76media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 77 if (error) { 78 console.error(`createSoundPool failed`) 79 return; 80 } else { 81 soundPool = soundPool_; 82 console.info(`createSoundPool success`) 83 let uri:string = ""; 84 let file: fs.File; 85 // Obtain the URI starting with fd://. 86 fs.open('/test_01.mp3', fs.OpenMode.READ_ONLY).then((file_: fs.File) => { 87 file = file_; 88 console.info("file fd: " + file.fd); 89 uri = 'fd://' + (file.fd).toString() 90 soundPool.load(uri, (error: BusinessError, soundId_: number) => { 91 if (error) { 92 console.error(`load soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 93 } else { 94 console.info(`load soundPool Success` + JSON.stringify(soundId_)) 95 } 96 }); 97 }); // '/test_01.mp3' here is only an example. You need to pass in the actual URI. 98 } 99}); 100``` 101 102### load 103 104load(uri: string): Promise\<number> 105 106Loads 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. 107 108**System capability**: SystemCapability.Multimedia.Media.SoundPool 109 110**Parameters** 111 112| Name| Type | Mandatory| Description | 113| ------ | -------------------------------------- | ---- | -------------------------- | 114| uri | string | Yes | URI of the audio file to load. Generally, the URI starts with fd://.| 115 116**Return value** 117 118| Type | Description | 119| -------------- | ------------------------------------------ | 120| Promise\<number> | Promise used to return the ID of the sound loaded. A valid value must be greater than 0.| 121 122**Error codes** 123 124For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 125 126| ID| Error Message | 127| -------- | --------------------------------------- | 128| 5400102 | Operation not allowed. Return by promise.| 129| 5400103 | I/O error. Return by promise. | 130| 5400105 | Service died. Return by promise. | 131 132**Example** 133 134```ts 135import fs from '@ohos.file.fs'; 136import { BusinessError } from '@ohos.base'; 137 138// Create a SoundPool instance. 139let soundPool: media.SoundPool; 140let audioRendererInfo: audio.AudioRendererInfo = { 141 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 142 rendererFlags: 1 143} 144media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 145 if (error) { 146 console.error(`createSoundPool failed`) 147 return; 148 } else { 149 soundPool = soundPool_; 150 console.info(`createSoundPool success`) 151 let uri:string = ""; 152 let soundID: number = 0; 153 let file: fs.File; 154 // Obtain the URI starting with fd://. 155 fs.open('/test_01.mp3', fs.OpenMode.READ_ONLY).then((file_: fs.File) => { 156 file = file_; 157 console.info("file fd: " + file.fd); 158 uri = 'fd://' + (file.fd).toString() 159 soundPool.load(uri).then((soundId: number) => { 160 console.info('soundPool load uri success'); 161 soundID = soundId; 162 }, (err: BusinessError) => { 163 console.error('soundPool load failed and catch error is ' + err.message); 164 }); 165 }); // '/test_01.mp3' here is only an example. You need to pass in the actual URI. 166 } 167}); 168 169``` 170 171### load 172 173load(fd: number, offset: number, length: number, callback: AsyncCallback\<number>): void 174 175Loads 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. 176 177**System capability**: SystemCapability.Multimedia.Media.SoundPool 178 179**Parameters** 180 181| Name | Type | Mandatory| Description | 182| -------- | ---------------------- | ---- | --------------------------- | 183| fd | number | Yes | Resource handle, which is obtained by calling **resourceManager.getRawFileDescriptor**. | 184| 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.| 185| 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.| 186| callback | AsyncCallback\<number> | Yes | Callback used to return the sound ID. A valid value must be greater than 0.| 187 188**Error codes** 189 190For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 191 192| ID| Error Message | 193| -------- | --------------------------------------- | 194| 5400102 | Operation not allowed. Return by callback. | 195| 5400103 | I/O error. Return by callback. | 196| 5400105 | Service died. Return by callback. | 197 198**Example** 199 200```ts 201import fs from '@ohos.file.fs'; 202import { BusinessError } from '@ohos.base'; 203 204// Create a SoundPool instance. 205let soundPool: media.SoundPool; 206let audioRendererInfo: audio.AudioRendererInfo = { 207 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 208 rendererFlags: 1 209} 210media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 211 if (error) { 212 console.error(`createSoundPool failed`) 213 return; 214 } else { 215 soundPool = soundPool_; 216 console.info(`createSoundPool success`) 217 let file: fs.File; 218 let soundID: number = 0; 219 let fileSize: number = 1; // Obtain the size through fs.stat(). 220 let uri: string = ""; 221 // Obtain the FD. 222 fs.open('/test_01.mp3', fs.OpenMode.READ_ONLY).then((file_: fs.File) => { 223 file = file_; 224 console.info("file fd: " + file.fd); 225 uri = 'fd://' + (file.fd).toString() 226 soundPool.load(file.fd, 0, fileSize, (error: BusinessError, soundId_: number) => { 227 if (error) { 228 console.error(`load soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 229 } else { 230 soundID = soundId_; 231 console.info('load success soundid:' + soundId_); 232 } 233 }); 234 }); // '/test_01.mp3' here is only an example. You need to pass in the actual URI. 235 } 236}); 237 238``` 239 240### load 241 242load(fd: number, offset: number, length: number): Promise\<number> 243 244Loads 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. 245 246**System capability**: SystemCapability.Multimedia.Media.SoundPool 247 248**Parameters** 249 250| Name | Type | Mandatory| Description | 251| -------- | ---------------------- | ---- | --------------------------- | 252| fd | number | Yes | Resource handle, which is obtained by calling **resourceManager.getRawFileDescriptor**. | 253| 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.| 254| 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.| 255 256**Return value** 257 258| Type | Description | 259| ---------------- | -------------------------------- | 260| Promise\<number> | Promise used to return the sound ID. A valid value must be greater than 0.| 261 262**Error codes** 263 264For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 265 266| ID| Error Message | 267| -------- | --------------------------------------- | 268| 5400102 | Operation not allowed. Return by promise.| 269| 5400103 | I/O error. Return by promise. | 270| 5400105 | Service died. Return by promise. | 271 272**Example** 273 274```ts 275import fs from '@ohos.file.fs'; 276import { BusinessError } from '@ohos.base'; 277 278// Create a SoundPool instance. 279let soundPool: media.SoundPool; 280let audioRendererInfo: audio.AudioRendererInfo = { 281 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 282 rendererFlags: 1 283} 284media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 285 if (error) { 286 console.error(`createSoundPool failed`) 287 return; 288 } else { 289 soundPool = soundPool_; 290 console.info(`createSoundPool success`) 291 let file: fs.File; 292 let soundID: number = 0; 293 let fileSize: number = 1; // Obtain the size through fs.stat(). 294 let uri: string = ""; 295 // Obtain the FD. 296 fs.open('/test_01.mp3', fs.OpenMode.READ_ONLY).then((file_: fs.File) => { 297 file = file_; 298 console.info("file fd: " + file.fd); 299 uri = 'fd://' + (file.fd).toString() 300 soundPool.load(file.fd, 0, fileSize).then((soundId: number) => { 301 console.info('load success'); 302 soundID = soundId; 303 }, (err: BusinessError) => { 304 console.error('soundpool load failed and catch error is ' + err.message); 305 }); 306 }); 307 } 308}); 309 310``` 311 312### play 313 314play(soundID: number, params: PlayParameters, callback: AsyncCallback\<number>): void 315 316Plays a sound. This API uses an asynchronous callback 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) | Yes | Playback parameters.| 326| callback | AsyncCallback\<number> | Yes | Callback used to return the ID of the audio stream. A valid value must be greater than 0.| 327 328**Error codes** 329 330For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 331 332| ID| Error Message | 333| -------- | --------------------------------------- | 334| 5400102 | Operation not allowed. Return by callback. | 335| 5400105 | Service died. Return by callback. | 336 337**Example** 338 339```js 340import { BusinessError } from '@ohos.base'; 341 342// Create a SoundPool instance. 343let soundPool: media.SoundPool; 344let audioRendererInfo: audio.AudioRendererInfo = { 345 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 346 rendererFlags: 1 347} 348media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 349 if (error) { 350 console.error(`createSoundPool failed`) 351 return; 352 } else { 353 soundPool = soundPool_; 354 console.info(`createSoundPool success`) 355 let soundID: number = 0; 356 let streamID: number = 0; 357 let playParameters: media.PlayParameters = { 358 loop: 3, // The sound loops three times. 359 rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // The sound is played at the original frequency. 360 leftVolume: 0.5, // range = 0.0-1.0 361 rightVolume: 0.5, // range = 0.0-1.0 362 priority: 0, // The sound playback has the lowest priority. 363 } 364 soundPool.play(soundID, playParameters, (error: BusinessError, streamId: number) => { 365 if (error) { 366 console.error(`play sound Error: errCode is ${error.code}, errMessage is ${error.message}`) 367 } else { 368 streamID = streamId; 369 console.info('play success soundid:' + streamId); 370 } 371 }); 372 } 373}); 374 375``` 376 377### play 378 379play(soundID: number, callback: AsyncCallback\<number>): void 380 381Plays a sound. This API uses an asynchronous callback to obtain the audio stream ID. 382 383**System capability**: SystemCapability.Multimedia.Media.SoundPool 384 385**Parameters** 386 387| Name | Type | Mandatory| Description | 388| -------- | ---------------------- | ---- | --------------------------- | 389| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 390| callback | AsyncCallback\<number> | Yes | Callback used to return the ID of the audio stream. A valid value must be greater than 0.| 391 392**Error codes** 393 394For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 395 396| ID| Error Message | 397| -------- | --------------------------------------- | 398| 5400102 | Operation not allowed. Return by callback. | 399| 5400105 | Service died. Return by callback. | 400 401**Example** 402 403```js 404import { BusinessError } from '@ohos.base'; 405 406// Create a SoundPool instance. 407let soundPool: media.SoundPool; 408let audioRendererInfo: audio.AudioRendererInfo = { 409 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 410 rendererFlags: 1 411} 412media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 413 if (error) { 414 console.error(`createSoundPool failed`) 415 return; 416 } else { 417 soundPool = soundPool_; 418 console.info(`createSoundPool success`) 419 let soundID: number = 0; 420 let streamID: number = 0; 421 soundPool.play(soundID, (error: BusinessError, streamId: number) => { 422 if (error) { 423 console.error(`play sound Error: errCode is ${error.code}, errMessage is ${error.message}`) 424 } else { 425 streamID = streamId; 426 console.info('play success soundid:' + streamId); 427 } 428 }); 429 } 430}); 431 432``` 433 434### play 435 436play(soundID: number, params?: PlayParameters): Promise\<number> 437 438Plays a sound. This API uses a promise to obtain the audio stream ID. 439 440**System capability**: SystemCapability.Multimedia.Media.SoundPool 441 442**Parameters** 443 444| Name | Type | Mandatory| Description | 445| -------- | ---------------------- | ---- | --------------------------- | 446| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 447| params | [PlayParameters](#playparameters) | No | Playback parameters.| 448 449**Return value** 450 451| Type | Description | 452| ---------------- | -------------------------------- | 453| Promise\<number> | Promise used to return the ID of the audio stream. A valid value must be greater than 0.| 454 455**Error codes** 456 457For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 458 459| ID| Error Message | 460| -------- | --------------------------------------- | 461| 5400102 | Operation not allowed. Return by promise. | 462| 5400105 | Service died. Return by promise. | 463 464**Example** 465 466```js 467import { BusinessError } from '@ohos.base'; 468 469// Create a SoundPool instance. 470let soundPool: media.SoundPool; 471let audioRendererInfo: audio.AudioRendererInfo = { 472 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 473 rendererFlags: 1 474} 475media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 476 if (error) { 477 console.error(`createSoundPool failed`) 478 return; 479 } else { 480 soundPool = soundPool_; 481 console.info(`createSoundPool success`) 482 let soundID: number = 0; 483 let streamID: number = 0; 484 let playParameters: media.PlayParameters = { 485 loop: 3, // The sound is played four times (three loops). 486 rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // The sound is played at the original frequency. 487 leftVolume: 0.5, // range = 0.0-1.0 488 rightVolume: 0.5, // range = 0.0-1.0 489 priority: 0, // The sound playback has the lowest priority. 490 } 491 492 soundPool.play(soundID, playParameters).then((streamId: number) => { 493 console.info('play success'); 494 streamID = streamId; 495 },(err: BusinessError) => { 496 console.error('soundpool play failed and catch error is ' + err.message); 497 }); 498 } 499}); 500 501``` 502 503### stop 504 505stop(streamID: number, callback: AsyncCallback\<void>): void 506 507Stops playing a sound. This API uses an asynchronous callback to return the result. 508 509**System capability**: SystemCapability.Multimedia.Media.SoundPool 510 511**Parameters** 512 513| Name | Type | Mandatory| Description | 514| -------- | ---------------------- | ---- | --------------------------- | 515| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 516| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 517 518**Error codes** 519 520For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 521 522| ID| Error Message | 523| -------- | --------------------------------------- | 524| 5400102 | Operation not allowed. Return by callback. | 525| 5400105 | Service died. Return by callback. | 526 527**Example** 528 529```js 530import { BusinessError } from '@ohos.base'; 531 532// Create a SoundPool instance. 533let soundPool: media.SoundPool; 534let audioRendererInfo: audio.AudioRendererInfo = { 535 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 536 rendererFlags: 1 537} 538media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 539 if (error) { 540 console.error(`createSoundPool failed`) 541 return; 542 } else { 543 soundPool = soundPool_; 544 console.info(`createSoundPool success`) 545 let streamID: number = 0; 546 // Call play() to obtain the stream ID. 547 soundPool.stop(streamID, (error: BusinessError) => { 548 if (error) { 549 console.error(`stop sound Error: errCode is ${error.code}, errMessage is ${error.message}`) 550 } else { 551 console.info('stop success'); 552 } 553 }) 554 } 555}); 556 557``` 558 559### stop 560 561stop(streamID: number): Promise\<void> 562 563Stops playing a sound. This API uses a promise to return the result. 564 565**System capability**: SystemCapability.Multimedia.Media.SoundPool 566 567**Parameters** 568 569| Name | Type | Mandatory| Description | 570| -------- | ---------------------- | ---- | --------------------------- | 571| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 572 573**Return value** 574 575| Type | Description | 576| ---------------- | -------------------------------- | 577| Promise\<void> | Promise that returns no value.| 578 579**Error codes** 580 581For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 582 583| ID| Error Message | 584| -------- | --------------------------------------- | 585| 5400102 | Operation not allowed. Return by promise. | 586| 5400105 | Service died. Return by promise. | 587 588**Example** 589 590```js 591import { BusinessError } from '@ohos.base'; 592 593// Create a SoundPool instance. 594let soundPool: media.SoundPool; 595let audioRendererInfo: audio.AudioRendererInfo = { 596 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 597 rendererFlags: 1 598} 599media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 600 if (error) { 601 console.error(`createSoundPool failed`) 602 return; 603 } else { 604 soundPool = soundPool_; 605 console.info(`createSoundPool success`) 606 let streamID: number = 0; 607 // Call play() to obtain the stream ID. 608 soundPool.stop(streamID).then(() => { 609 console.info('stop success'); 610 }, (err: BusinessError) => { 611 console.error('soundpool load stop and catch error is ' + err.message); 612 }); 613 } 614}); 615``` 616 617### setLoop 618 619setLoop(streamID: number, loop: number, callback: AsyncCallback\<void>): void; 620 621Sets the loop mode for an audio stream. This API uses an asynchronous callback to return the result. 622 623**System capability**: SystemCapability.Multimedia.Media.SoundPool 624 625**Parameters** 626 627| Name | Type | Mandatory| Description | 628| -------- | ---------------------- | ---- | --------------------------- | 629| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 630| 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.| 631| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 632 633**Error codes** 634 635For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 636 637| ID| Error Message | 638| -------- | --------------------------------------- | 639| 5400102 | Operation not allowed. Return by callback. | 640| 5400105 | Service died. Return by callback. | 641 642**Example** 643 644```js 645import { BusinessError } from '@ohos.base'; 646 647// Create a SoundPool instance. 648let soundPool: media.SoundPool; 649let audioRendererInfo: audio.AudioRendererInfo = { 650 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 651 rendererFlags: 1 652} 653media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 654 if (error) { 655 console.error(`createSoundPool failed`) 656 return; 657 } else { 658 soundPool = soundPool_; 659 console.info(`createSoundPool success`) 660 let streamID: number = 0; 661 // Call play() to obtain the stream ID. 662 // Set the number of loops to 2. 663 soundPool.setLoop(streamID, 2, (error: BusinessError) => { 664 if (error) { 665 console.error(`setLoop soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 666 } else { 667 console.info('setLoop success streamID:' + streamID); 668 } 669 }); 670 } 671}); 672 673``` 674 675### setLoop 676 677setLoop(streamID: number, loop: number): Promise\<void> 678 679Sets the loop mode for an audio stream. This API uses a promise to return the result. 680 681**System capability**: SystemCapability.Multimedia.Media.SoundPool 682 683**Parameters** 684 685| Name | Type | Mandatory| Description | 686| -------- | ---------------------- | ---- | --------------------------- | 687| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 688| 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.| 689 690**Return value** 691 692| Type | Description | 693| ---------------- | -------------------------------- | 694| Promise\<void> | Promise that returns no value.| 695 696**Error codes** 697 698For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 699 700| ID| Error Message | 701| -------- | --------------------------------------- | 702| 5400102 | Operation not allowed. Return by promise. | 703| 5400105 | Service died. Return by promise. | 704 705**Example** 706 707```js 708import { BusinessError } from '@ohos.base'; 709 710// Create a SoundPool instance. 711let soundPool: media.SoundPool; 712let audioRendererInfo: audio.AudioRendererInfo = { 713 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 714 rendererFlags: 1 715} 716media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 717 if (error) { 718 console.error(`createSoundPool failed`) 719 return; 720 } else { 721 soundPool = soundPool_; 722 console.info(`createSoundPool success`) 723 let streamID: number = 0; 724 // Call play() to obtain the stream ID. 725 // Set the number of loops to 1. 726 soundPool.setLoop(streamID, 1).then(() => { 727 console.info('setLoop success streamID:' + streamID); 728 }).catch((err: BusinessError) => { 729 console.error('soundpool setLoop failed and catch error is ' + err.message); 730 }); 731 } 732}); 733 734``` 735 736### setPriority 737 738setPriority(streamID: number, priority: number, callback: AsyncCallback\<void>): void 739 740Sets the priority for an audio stream. This API uses an asynchronous callback to return the result. 741 742**System capability**: SystemCapability.Multimedia.Media.SoundPool 743 744**Parameters** 745 746| Name | Type | Mandatory| Description | 747| -------- | ---------------------- | ---- | --------------------------- | 748| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 749| priority | number | Yes | Priority. The value **0** means the lowest priority.| 750| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 751 752**Error codes** 753 754For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 755 756| ID| Error Message | 757| -------- | --------------------------------------- | 758| 5400102 | Operation not allowed. Return by callback. | 759| 5400105 | Service died. Return by callback. | 760 761**Example** 762 763```js 764import { BusinessError } from '@ohos.base'; 765 766// Create a SoundPool instance. 767let soundPool: media.SoundPool; 768let audioRendererInfo: audio.AudioRendererInfo = { 769 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 770 rendererFlags: 1 771} 772media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 773 if (error) { 774 console.error(`createSoundPool failed`) 775 return; 776 } else { 777 soundPool = soundPool_; 778 console.info(`createSoundPool success`) 779 let streamID: number = 0; 780 // Call play() to obtain the stream ID. 781 // Set the priority to 1. 782 soundPool.setPriority(streamID, 1, (error: BusinessError) => { 783 if (error) { 784 console.error(`setPriority soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 785 } else { 786 console.info('setPriority success streamID:' + streamID); 787 } 788 }); 789 } 790}); 791 792``` 793 794### setPriority 795 796setPriority(streamID: number, priority: number): Promise\<void> 797 798Sets the priority for an audio stream. This API uses a promise to return the result. 799 800**System capability**: SystemCapability.Multimedia.Media.SoundPool 801 802**Parameters** 803 804| Name | Type | Mandatory| Description | 805| -------- | ---------------------- | ---- | --------------------------- | 806| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 807| priority | number | Yes | Priority. The value **0** means the lowest priority.| 808 809**Return value** 810 811| Type | Description | 812| ---------------- | -------------------------------- | 813| Promise\<void> | Promise that returns no value.| 814 815**Error codes** 816 817For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 818 819| ID| Error Message | 820| -------- | --------------------------------------- | 821| 5400102 | Operation not allowed. Return by promise. | 822| 5400105 | Service died. Return by promise. | 823 824**Example** 825 826```js 827import { BusinessError } from '@ohos.base'; 828 829// Create a SoundPool instance. 830let soundPool: media.SoundPool; 831let audioRendererInfo: audio.AudioRendererInfo = { 832 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 833 rendererFlags: 1 834} 835media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 836 if (error) { 837 console.error(`createSoundPool failed`) 838 return; 839 } else { 840 soundPool = soundPool_; 841 console.info(`createSoundPool success`) 842 let streamID: number = 0; 843 // Call play() to obtain the stream ID. 844 // Set the priority to 1. 845 846 soundPool.setPriority(streamID, 1).then(() => { 847 console.info('setPriority success'); 848 }, (err: BusinessError) => { 849 console.error('soundpool setPriority failed and catch error is ' + err.message); 850 }); 851 } 852}); 853 854``` 855 856### setRate 857 858setRate(streamID: number, rate: audio.AudioRendererRate, callback: AsyncCallback\<void>): void 859 860Sets the playback rate for an audio stream. This API uses an asynchronous callback to return the result. 861 862> **NOTE** 863> This API is available in OpenHarmony 4.0. The function implementation depends on the device. Currently, only the RK3568 development board supports the playback rate setting. 864 865**System capability**: SystemCapability.Multimedia.Media.SoundPool 866 867**Parameters** 868 869| Name | Type | Mandatory| Description | 870| -------- | ---------------------- | ---- | --------------------------- | 871| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 872| rate | [audio.AudioRendererRate](js-apis-audio.md#audiorendererrate8) | Yes | Playback rate.| 873| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 874 875**Error codes** 876 877For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 878 879| ID| Error Message | 880| -------- | --------------------------------------- | 881| 5400102 | Operation not allowed. Return by callback. | 882| 5400105 | Service died. Return by callback. | 883 884**Example** 885 886```js 887import { BusinessError } from '@ohos.base'; 888 889// Create a SoundPool instance. 890let soundPool: media.SoundPool; 891let audioRendererInfo: audio.AudioRendererInfo = { 892 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 893 rendererFlags: 1 894} 895media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 896 if (error) { 897 console.error(`createSoundPool failed`) 898 return; 899 } else { 900 soundPool = soundPool_; 901 console.info(`createSoundPool success`) 902 let streamID: number = 0; 903 let selectedAudioRendererRate: audio.AudioRendererRate = audio.AudioRendererRate.RENDER_RATE_NORMAL; // The sound is played at the original frequency. 904 // Call play() to obtain the stream ID. 905 soundPool.setRate(streamID, selectedAudioRendererRate, (error: BusinessError) => { 906 if (error) { 907 console.error(`setRate soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 908 } else { 909 console.info('setRate success streamID:' + streamID); 910 } 911 }) 912 } 913}); 914 915``` 916 917### setRate 918 919setRate(streamID: number, rate: audio.AudioRendererRate): Promise\<void> 920 921Sets the playback rate for an audio stream. This API uses a promise to return the result. 922 923> **NOTE** 924> This API is available in OpenHarmony 4.0. The function implementation depends on the device. Currently, only the RK3568 development board supports the playback rate setting. 925 926**System capability**: SystemCapability.Multimedia.Media.SoundPool 927 928**Parameters** 929 930| Name | Type | Mandatory| Description | 931| -------- | ---------------------- | ---- | --------------------------- | 932| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 933| rate | [audio.AudioRendererRate](js-apis-audio.md#audiorendererrate8) | Yes | Playback rate.| 934 935**Return value** 936 937| Type | Description | 938| ---------------- | -------------------------------- | 939| Promise\<void> | Promise that returns no value.| 940 941**Error codes** 942 943For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 944 945| ID| Error Message | 946| -------- | --------------------------------------- | 947| 5400102 | Operation not allowed. Return by promise. | 948| 5400105 | Service died. Return by promise. | 949 950**Example** 951 952```js 953import { BusinessError } from '@ohos.base'; 954 955// Create a SoundPool instance. 956let soundPool: media.SoundPool; 957let audioRendererInfo: audio.AudioRendererInfo = { 958 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 959 rendererFlags: 1 960} 961media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 962 if (error) { 963 console.error(`createSoundPool failed`) 964 return; 965 } else { 966 soundPool = soundPool_; 967 console.info(`createSoundPool success`) 968 let streamID: number = 0; 969 let selectedAudioRendererRate: audio.AudioRendererRate = audio.AudioRendererRate.RENDER_RATE_NORMAL; // The sound is played at the original frequency. 970 // Call play() to obtain the stream ID. 971 soundPool.setRate(streamID, selectedAudioRendererRate).then(() => { 972 console.info('setRate success'); 973 }, (err: BusinessError) => { 974 console.error('soundpool setRate failed and catch error is ' + err.message); 975 }); 976 } 977}); 978 979``` 980 981### setVolume 982 983setVolume(streamID: number, leftVolume: number, rightVolume: number, callback: AsyncCallback\<void>): void 984 985Sets the volume for an audio stream. This API uses an asynchronous callback to return the result. 986 987**System capability**: SystemCapability.Multimedia.Media.SoundPool 988 989**Parameters** 990 991| Name | Type | Mandatory| Description | 992| -------- | ---------------------- | ---- | --------------------------- | 993| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 994| leftVolume | number | Yes | Volume of the left channel. The value ranges from 0.0 to 1.0.| 995| 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.| 996| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 997 998**Error codes** 999 1000For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1001 1002| ID| Error Message | 1003| -------- | --------------------------------------- | 1004| 5400102 | Operation not allowed. Return by callback. | 1005| 5400105 | Service died. Return by callback. | 1006 1007**Example** 1008 1009```js 1010import { BusinessError } from '@ohos.base'; 1011 1012// Create a SoundPool instance. 1013let soundPool: media.SoundPool; 1014let audioRendererInfo: audio.AudioRendererInfo = { 1015 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1016 rendererFlags: 1 1017} 1018media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1019 if (error) { 1020 console.error(`createSoundPool failed`) 1021 return; 1022 } else { 1023 soundPool = soundPool_; 1024 console.info(`createSoundPool success`) 1025 let streamID: number = 0; 1026 // Call play() to obtain the stream ID. 1027 // Set the volume to 0.5. 1028 soundPool.setVolume(streamID, 0.5, 0.5, (error: BusinessError) => { 1029 if (error) { 1030 console.error(`setVolume soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 1031 } else { 1032 console.info('setVolume success streamID:' + streamID); 1033 } 1034 }) 1035 } 1036}); 1037 1038``` 1039 1040### setVolume 1041 1042setVolume(streamID: number, leftVolume: number, rightVolume: number): Promise\<void> 1043 1044Sets the volume for an audio stream. This API uses a promise to return the result. 1045 1046**System capability**: SystemCapability.Multimedia.Media.SoundPool 1047 1048**Parameters** 1049 1050| Name | Type | Mandatory| Description | 1051| -------- | ---------------------- | ---- | --------------------------- | 1052| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 1053| leftVolume | number | Yes | Volume of the left channel. The value ranges from 0.0 to 1.0.| 1054| 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.| 1055 1056**Return value** 1057 1058| Type | Description | 1059| ---------------- | -------------------------------- | 1060| Promise\<void> | Promise that returns no value.| 1061 1062**Error codes** 1063 1064For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1065 1066| ID| Error Message | 1067| -------- | --------------------------------------- | 1068| 5400102 | Operation not allowed. Return by promise. | 1069| 5400105 | Service died. Return by promise. | 1070 1071**Example** 1072 1073```js 1074import { BusinessError } from '@ohos.base'; 1075 1076// Create a SoundPool instance. 1077let soundPool: media.SoundPool; 1078let audioRendererInfo: audio.AudioRendererInfo = { 1079 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1080 rendererFlags: 1 1081} 1082media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1083 if (error) { 1084 console.error(`createSoundPool failed`) 1085 return; 1086 } else { 1087 soundPool = soundPool_; 1088 console.info(`createSoundPool success`) 1089 let streamID: number = 0; 1090 // Call play() to obtain the stream ID. 1091 1092 soundPool.setVolume(streamID, 0.5, 0.5).then(() => { 1093 console.info('setVolume success'); 1094 }, (err: BusinessError) => { 1095 console.error('soundpool setVolume failed and catch error is ' + err.message); 1096 }); 1097 } 1098}); 1099 1100``` 1101 1102### unload 1103 1104unload(soundID: number, callback: AsyncCallback\<void>): void 1105 1106Unloads a sound. This API uses an asynchronous callback to return the result. 1107 1108**System capability**: SystemCapability.Multimedia.Media.SoundPool 1109 1110**Parameters** 1111 1112| Name | Type | Mandatory| Description | 1113| -------- | ---------------------- | ---- | --------------------------- | 1114| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 1115| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 1116 1117**Error codes** 1118 1119For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1120 1121| ID| Error Message | 1122| -------- | --------------------------------------- | 1123| 5400102 | Operation not allowed. Return by callback. | 1124| 5400103 | I/O error. Return by callback. | 1125| 5400105 | Service died. Return by callback. | 1126 1127**Example** 1128 1129```js 1130import { BusinessError } from '@ohos.base'; 1131 1132// Create a SoundPool instance. 1133let soundPool: media.SoundPool; 1134let audioRendererInfo: audio.AudioRendererInfo = { 1135 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1136 rendererFlags: 1 1137} 1138media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1139 if (error) { 1140 console.error(`createSoundPool failed`) 1141 return; 1142 } else { 1143 soundPool = soundPool_; 1144 console.info(`createSoundPool success`) 1145 let soundID: number = 0; 1146 // Call load() to obtain the sound ID. 1147 soundPool.unload(soundID, (error: BusinessError) => { 1148 if (error) { 1149 console.error(`unload soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 1150 } else { 1151 console.info('unload success:'); 1152 } 1153 }) 1154 } 1155}); 1156 1157``` 1158 1159### unload 1160 1161unload(soundID: number): Promise\<void> 1162 1163Unloads a sound. This API uses a promise to return the result. 1164 1165**System capability**: SystemCapability.Multimedia.Media.SoundPool 1166 1167**Parameters** 1168 1169| Name | Type | Mandatory| Description | 1170| -------- | ---------------------- | ---- | --------------------------- | 1171| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 1172 1173**Return value** 1174 1175| Type | Description | 1176| ---------------- | -------------------------------- | 1177| Promise\<void> | Promise that returns no value.| 1178 1179**Error codes** 1180 1181For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1182 1183| ID| Error Message | 1184| -------- | --------------------------------------- | 1185| 5400102 | Operation not allowed. Return by promise. | 1186| 5400103 | I/O error. Return by promise. | 1187| 5400105 | Service died. Return by promise. | 1188 1189**Example** 1190 1191```js 1192import { BusinessError } from '@ohos.base'; 1193 1194// Create a SoundPool instance. 1195let soundPool: media.SoundPool; 1196let audioRendererInfo: audio.AudioRendererInfo = { 1197 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1198 rendererFlags: 1 1199} 1200media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1201 if (error) { 1202 console.error(`createSoundPool failed`) 1203 return; 1204 } else { 1205 soundPool = soundPool_; 1206 console.info(`createSoundPool success`) 1207 let soundID: number = 0; 1208 // Call load() to obtain the sound ID. 1209 1210 soundPool.unload(soundID).then(() => { 1211 console.info('unload success'); 1212 }, (err: BusinessError) => { 1213 console.error('soundpool unload failed and catch error is ' + err.message); 1214 }); 1215 } 1216}); 1217 1218``` 1219 1220### release 1221 1222release(callback: AsyncCallback\<void>): void 1223 1224Releases this **SoundPool** instance. This API uses an asynchronous callback to return the result. 1225 1226**System capability**: SystemCapability.Multimedia.Media.SoundPool 1227 1228**Parameters** 1229 1230| Name | Type | Mandatory| Description | 1231| -------- | ---------------------- | ---- | --------------------------- | 1232| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 1233 1234**Error codes** 1235 1236For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1237 1238| ID| Error Message | 1239| -------- | --------------------------------------- | 1240| 5400105 | Service died. Return by callback. | 1241 1242**Example** 1243 1244```js 1245import { BusinessError } from '@ohos.base'; 1246 1247// Create a SoundPool instance. 1248let soundPool: media.SoundPool; 1249let audioRendererInfo: audio.AudioRendererInfo = { 1250 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1251 rendererFlags: 1 1252} 1253media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1254 if (error) { 1255 console.error(`createSoundPool failed`) 1256 return; 1257 } else { 1258 soundPool = soundPool_; 1259 console.info(`createSoundPool success`) 1260 soundPool.release((error: BusinessError) => { 1261 if (error) { 1262 console.error(`release soundPool Error: errCode is ${error.code}, errMessage is ${error.message}`) 1263 } else { 1264 console.info('release success'); 1265 } 1266 }) 1267 } 1268}); 1269 1270 1271``` 1272 1273### release 1274 1275release(): Promise\<void> 1276 1277Releases this **SoundPool** instance. This API uses a promise to return the result. 1278 1279**System capability**: SystemCapability.Multimedia.Media.SoundPool 1280 1281**Return value** 1282 1283| Type | Description | 1284| ---------------- | -------------------------------- | 1285| Promise\<void> | Promise that returns no value.| 1286 1287**Error codes** 1288 1289For details about the error codes, see [Media Error Codes](../errorcodes/errorcode-media.md). 1290 1291| ID| Error Message | 1292| -------- | --------------------------------------- | 1293| 5400105 | Service died. Return by promise. | 1294 1295**Example** 1296 1297```js 1298import { BusinessError } from '@ohos.base'; 1299 1300// Create a SoundPool instance. 1301let soundPool: media.SoundPool; 1302let audioRendererInfo: audio.AudioRendererInfo = { 1303 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1304 rendererFlags: 1 1305} 1306media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1307 if (error) { 1308 console.error(`createSoundPool failed`) 1309 return; 1310 } else { 1311 soundPool = soundPool_; 1312 console.info(`createSoundPool success`) 1313 soundPool.release().then(() => { 1314 console.info('release success'); 1315 }, (err: BusinessError) => { 1316 console.error('soundpool release failed and catch error is ' + err.message); 1317 }); 1318 } 1319}); 1320 1321``` 1322 1323### on('loadComplete') 1324 1325on(type: 'loadComplete', callback: Callback\<number>): void 1326 1327Subscribes to events indicating that a sound finishes loading. 1328 1329**System capability**: SystemCapability.Multimedia.Media.SoundPool 1330 1331**Parameters** 1332 1333| Name | Type | Mandatory| Description | 1334| -------- | -------- | ---- | ------------------------------------------------------------ | 1335| type | string | Yes | Event type, which is **'loadComplete'** in this case. This event is triggered when a sound is loaded.| 1336| callback | Callback\<number> | Yes | ID of the sound that has been loaded. | 1337 1338**Example** 1339 1340```js 1341import { BusinessError } from '@ohos.base'; 1342 1343// Create a SoundPool instance. 1344let soundPool: media.SoundPool; 1345let audioRendererInfo: audio.AudioRendererInfo = { 1346 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1347 rendererFlags: 1 1348} 1349media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1350 if (error) { 1351 console.error(`createSoundPool failed`) 1352 return; 1353 } else { 1354 soundPool = soundPool_; 1355 console.info(`createSoundPool success`) 1356 soundPool.on('loadComplete', (soundId: number) => { 1357 console.info('loadComplete success, soundId: ' + soundId) 1358 }) 1359 } 1360}); 1361 1362``` 1363 1364### off('loadComplete') 1365 1366off(type: 'loadComplete'): void 1367 1368Unsubscribes from events indicating that a sound finishes loading. 1369 1370**System capability**: SystemCapability.Multimedia.Media.SoundPool 1371 1372**Parameters** 1373 1374| Name| Type | Mandatory| Description | 1375| ------ | ------ | ---- | ------------------------------------------------------------ | 1376| type | string | Yes | Event type. The value is fixed at **'loadComplete'**.| 1377 1378**Example** 1379 1380```js 1381import { BusinessError } from '@ohos.base'; 1382 1383// Create a SoundPool instance. 1384let soundPool: media.SoundPool; 1385let audioRendererInfo: audio.AudioRendererInfo = { 1386 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1387 rendererFlags: 1 1388} 1389media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1390 if (error) { 1391 console.error(`createSoundPool failed`) 1392 return; 1393 } else { 1394 soundPool = soundPool_; 1395 console.info(`createSoundPool success`) 1396 soundPool.off('loadComplete') 1397 } 1398}); 1399 1400``` 1401 1402### on('playFinished') 1403 1404on(type: 'playFinished', callback: Callback\<void>): void 1405 1406Subscribes to events indicating that a sound finishes playing. 1407 1408**System capability**: SystemCapability.Multimedia.Media.SoundPool 1409 1410**Parameters** 1411 1412| Name | Type | Mandatory| Description | 1413| -------- | -------- | ---- | ------------------------------------------------------------ | 1414| type | string | Yes | Event type, which is **'playFinished'** in this case. This event is triggered when a sound finishes playing.| 1415| callback | Callback\<void> | Yes | Callback used to return the result. | 1416 1417**Example** 1418 1419```js 1420import { BusinessError } from '@ohos.base'; 1421 1422// Create a SoundPool instance. 1423let soundPool: media.SoundPool; 1424let audioRendererInfo: audio.AudioRendererInfo = { 1425 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1426 rendererFlags: 1 1427} 1428media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1429 if (error) { 1430 console.error(`createSoundPool failed`) 1431 return; 1432 } else { 1433 soundPool = soundPool_; 1434 console.info(`createSoundPool success`) 1435 soundPool.on('playFinished', () => { 1436 console.info('playFinished success') 1437 }); 1438 } 1439}); 1440 1441``` 1442 1443### off('playFinished') 1444 1445off(type: 'playFinished'): void 1446 1447Unsubscribes from events indicating that a sound finishes playing. 1448 1449**System capability**: SystemCapability.Multimedia.Media.SoundPool 1450 1451**Parameters** 1452 1453| Name| Type | Mandatory| Description | 1454| ------ | ------ | ---- | ------------------------------------------------------------ | 1455| type | string | Yes | Event type. The value is fixed at **'playFinished'**.| 1456 1457**Example** 1458 1459```js 1460import { BusinessError } from '@ohos.base'; 1461 1462// Create a SoundPool instance. 1463let soundPool: media.SoundPool; 1464let audioRendererInfo: audio.AudioRendererInfo = { 1465 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1466 rendererFlags: 1 1467} 1468media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1469 if (error) { 1470 console.error(`createSoundPool failed`) 1471 return; 1472 } else { 1473 soundPool = soundPool_; 1474 console.info(`createSoundPool success`) 1475 soundPool.off('playFinished') 1476 } 1477}); 1478 1479``` 1480 1481### on('error') 1482 1483on(type: 'error', callback: ErrorCallback): void 1484 1485Subscribes to error events of this **SoundPool** instance. This event is used only for error prompt. 1486 1487**System capability**: SystemCapability.Multimedia.Media.SoundPool 1488 1489**Parameters** 1490 1491| Name | Type | Mandatory| Description | 1492| -------- | -------- | ---- | ------------------------------------------------------------ | 1493| type | string | Yes | Event type, which is **'error'** in this case. This event can be triggered by both user operations and the system.| 1494| callback | ErrorCallback | Yes | Callback used to return the error code ID and error message.| 1495 1496The **SoundPool** class provides the following error types<a name = error_info></a>: 1497 1498| ID| Error Message | Description | 1499| -------- | --------------------- | ------------------------------------------------------------ | 1500| 401 | Invalid Parameter: | Incorrect input parameter, causing an invalid call. | 1501| 801 | Unsupport Capability: | Unsupported API, causing an invalid call. | 1502| 5400101 | No Memory: | Insufficient memory.| 1503| 5400102 | Operation Not Allowed: | Unsupported operation in the current state, causing an invalid call. | 1504| 5400103 | IO Error: | I/O exception.| 1505| 5400105 | Service Died: | The playback process is dead, and the service on which the sound pool depends is abnormal.| 1506 1507**Example** 1508 1509```js 1510import { BusinessError } from '@ohos.base'; 1511 1512// Create a SoundPool instance. 1513let soundPool: media.SoundPool; 1514let audioRendererInfo: audio.AudioRendererInfo = { 1515 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1516 rendererFlags: 1 1517} 1518media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1519 if (error) { 1520 console.error(`createSoundPool failed`) 1521 return; 1522 } else { 1523 soundPool = soundPool_; 1524 console.info(`createSoundPool success`) 1525 soundPool.on('error', (error: BusinessError) => { 1526 console.info('error happened,and error message is :' + error.message) 1527 console.info('error happened,and error code is :' + error.code) 1528 }) 1529 } 1530}); 1531 1532``` 1533 1534### off('error') 1535 1536off(type: 'error'): void 1537 1538Unsubscribes from error events of this **SoundPool** instance. 1539 1540**System capability**: SystemCapability.Multimedia.Media.SoundPool 1541 1542**Parameters** 1543 1544| Name| Type | Mandatory| Description | 1545| ------ | ------ | ---- | ----------------------------------------- | 1546| type | string | Yes | Event type, which is **'error'** in this case.| 1547 1548**Example** 1549 1550```js 1551import { BusinessError } from '@ohos.base'; 1552 1553// Create a SoundPool instance. 1554let soundPool: media.SoundPool; 1555let audioRendererInfo: audio.AudioRendererInfo = { 1556 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1557 rendererFlags: 1 1558} 1559media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1560 if (error) { 1561 console.error(`createSoundPool failed`) 1562 return; 1563 } else { 1564 soundPool = soundPool_; 1565 console.info(`createSoundPool success`) 1566 soundPool.off('error') 1567 } 1568}); 1569``` 1570