1# SoundPool (Sound Pool) 2<!--Kit: Media Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @wang-haizhou6--> 5<!--Designer: @HmQQQ--> 6<!--Tester: @xchaosioda--> 7<!--Adviser: @zengyawen--> 8 9The module provides APIs for loading, unloading, playing, and stopping playing sounds, setting the volume, and setting the number of loops. 10 11Before using these APIs, you must call [media.createSoundPool](arkts-apis-media-f.md#mediacreatesoundpool10) to create a SoundPool instance. 12 13> **NOTE** 14> 15> 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. 16 17## Modules to Import 18 19```js 20import { media } from '@kit.MediaKit'; 21import { audio } from '@kit.AudioKit'; 22``` 23 24## PlayParameters 25 26Describes the playback parameters of the sound pool. 27 28These parameters are used to control the playback volume, number of loops, and priority. 29 30**System capability**: SystemCapability.Multimedia.Media.SoundPool 31 32| Name | Type | Mandatory| Description | 33| --------------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | 34| loop | number | No | Number of loops.<br>If this parameter is set to a value greater than or equal to 0, the number of times the content is actually played is the value of **loop** plus 1.<br> If this parameter is set to a value less than 0, the content is played repeatedly.<br>The default value is **0**, indicating that the content is played only once. | 35| rate | number | No | Playback rate. For details, see [AudioRendererRate](../apis-audio-kit/arkts-apis-audio-e.md#audiorendererrate8). Default value: **0**| 36| leftVolume | number | No | Volume of the left channel. The value ranges from 0.0 to 1.0. Default value: **1.0** | 37| rightVolume | number | No | Volume of the right channel. The value ranges from 0.0 to 1.0. (Currently, the volume cannot be set separately for the left and right channels. The volume set for the left channel is used.) Default value: **1.0**| 38| priority | number | No | Playback priority. The value **0** means the lowest priority. A larger value indicates a higher priority. The value is an integer greater than or equal to 0. Default value: **0** | 39 40## ErrorType<sup>20+</sup> 41 42Enumerates the error types (used to distinguish error stages). 43 44**System capability**: SystemCapability.Multimedia.Media.SoundPool 45 46| Name | Value | Description | 47| ----------------------------------- | ------- | --------------------------------- | 48| LOAD_ERROR | 1 | An error occurred during resource loading. | 49| PLAY_ERROR | 2 | An error occurred during resource playback. | 50 51## ErrorInfo<sup>20+</sup> 52 53Describes the error information. 54 55**System capability**: SystemCapability.Multimedia.Media.SoundPool 56 57| Name | Type | Read-Only| Optional| Description | 58| --------------- | ---------------------------------------- | ---- | ---- | ------------------------------------------------------------ | 59| errorCode | T | No | No | Error code. The type of **errorCode** is [BusinessError](../apis-basic-services-kit/js-apis-base.md#businesserror). | 60| errorType | [ErrorType](#errortype20) | No | Yes | Stage at which the error occurred.| 61| soundId | number | No | Yes | ID of the resource where the error occurred. It can be obtained by calling **load**. | 62| streamId | number | No | Yes | ID of the audio stream where the error occurred. It can be obtained by calling **play**.| 63 64## SoundPool 65 66Implements 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](arkts-apis-media-f.md#mediacreatesoundpool10) to create a SoundPool instance. 67 68> **NOTE** 69> 70> When using the SoundPool instance, you are advised to register the following callbacks to proactively obtain status changes: 71> - [on('loadComplete')](#onloadcomplete): listens for the event indicating that the resource loading is finished. 72> - [on('playFinishedWithStreamId')](#onplayfinishedwithstreamid18): listens for the event indicating that the playback is finished and returns the stream ID of the audio that finishes playing. 73> - [on('playFinished')](#onplayfinished): listens for the event indicating that the playback is finished. 74> - [on('error')](#onerror): listens for error events. 75> - [on('errorOccurred')](#onerroroccurred20): listens for error events and returns [errorInfo](#errorinfo20). 76 77### load 78 79load(uri: string, callback: AsyncCallback\<number>): void 80 81Loads 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. 82This API cannot be used to load resources in the **rawfile** directory. Instead, use [load(fd: number, offset: number, length: number, callback: AsyncCallback\<number>): void](#load-2) or [load(fd: number, offset: number, length: number): Promise\<number>](#load-3). 83 84>**NOTE** 85> 86> After the resource handle (in the form of an FD) or path description (in the form of a URI) is transferred to the player, do not use the resource handle or path description in read or write operations, including but not limited to transferring it to multiple players. 87> 88> Competition occurs when multiple players use the same resource handle or path description to read and write files at the same time, resulting in playback errors. 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| callback | AsyncCallback\<number> | Yes | Callback used to return the sound ID. A valid value must be greater than 0.| 98 99**Error codes** 100 101For details about the error codes, see [Media Error Codes](errorcode-media.md). 102 103| ID| Error Message | 104| -------- | --------------------------------------- | 105| 5400102 | Operation not allowed. Return by callback.| 106| 5400103 | I/O error. Return by callback. | 107| 5400105 | Service died. Return by callback. | 108 109**Example** 110 111```ts 112import { fileIo } from '@kit.CoreFileKit'; 113import { BusinessError } from '@kit.BasicServicesKit'; 114import { media } from '@kit.MediaKit'; 115import { audio } from '@kit.AudioKit'; 116 117// Create a SoundPool instance. 118let soundPool: media.SoundPool; 119let audioRendererInfo: audio.AudioRendererInfo = { 120 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 121 rendererFlags: 1 122} 123media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 124 if (error) { 125 console.error(`Failed to createSoundPool`); 126 return; 127 } else { 128 soundPool = soundPool_; 129 console.info(`Succeeded in createSoundPool`); 130 let uri:string = ""; 131 let file: fileIo.File; 132 // Obtain the URI starting with fd://. 133 fileIo.open('/test_01.mp3', fileIo.OpenMode.READ_ONLY).then((file_: fileIo.File) => { 134 file = file_; 135 console.info("file fd: " + file.fd); 136 uri = 'fd://' + (file.fd).toString(); 137 soundPool.load(uri, (error: BusinessError, soundId_: number) => { 138 if (error) { 139 console.error(`Failed to load soundPool: errCode is ${error.code}, errMessage is ${error.message}`); 140 } else { 141 console.info(`Succeeded in loading soundPool` + JSON.stringify(soundId_)); 142 } 143 }); 144 }); // '/test_01.mp3' here is only an example. You need to pass in the actual URI. 145 } 146}); 147``` 148 149### load 150 151load(uri: string): Promise\<number> 152 153Loads 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. 154This API cannot be used to load resources in the **rawfile** directory. Instead, use [load(fd: number, offset: number, length: number, callback: AsyncCallback\<number>): void](#load-2) or [load(fd: number, offset: number, length: number): Promise\<number>](#load-3). 155 156>**NOTE** 157> 158> After the resource handle (in the form of an FD) or path description (in the form of a URI) is transferred to the player, do not use the resource handle or path description in read or write operations, including but not limited to transferring it to multiple players. 159> 160>Competition occurs when multiple players use the same resource handle or path description to read and write files at the same time, resulting in playback errors. 161 162**System capability**: SystemCapability.Multimedia.Media.SoundPool 163 164**Parameters** 165 166| Name| Type | Mandatory| Description | 167| ------ | -------------------------------------- | ---- | -------------------------- | 168| uri | string | Yes | URI of the audio file to load. Generally, the URI starts with fd://.| 169 170**Return value** 171 172| Type | Description | 173| -------------- | ------------------------------------------ | 174| Promise\<number> | Promise used to return the sound ID. A valid value must be greater than 0.| 175 176**Error codes** 177 178For details about the error codes, see [Media Error Codes](errorcode-media.md). 179 180| ID| Error Message | 181| -------- | --------------------------------------- | 182| 5400102 | Operation not allowed. Return by promise.| 183| 5400103 | I/O error. Return by promise. | 184| 5400105 | Service died. Return by promise. | 185 186**Example** 187 188```ts 189import { fileIo } from '@kit.CoreFileKit'; 190import { BusinessError } from '@kit.BasicServicesKit'; 191import { media } from '@kit.MediaKit'; 192import { audio } from '@kit.AudioKit'; 193 194// Create a SoundPool instance. 195let soundPool: media.SoundPool; 196let audioRendererInfo: audio.AudioRendererInfo = { 197 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 198 rendererFlags: 1 199} 200media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 201 if (error) { 202 console.error(`Failed to createSoundPool`); 203 return; 204 } else { 205 soundPool = soundPool_; 206 console.info(`Succeeded in createSoundPool`); 207 let uri:string = ""; 208 let soundID: number = 0; 209 let file: fileIo.File; 210 // Obtain the URI starting with fd://. 211 fileIo.open('/test_01.mp3', fileIo.OpenMode.READ_ONLY).then((file_: fileIo.File) => { 212 file = file_; 213 console.info("file fd: " + file.fd); 214 uri = 'fd://' + (file.fd).toString(); 215 soundPool.load(uri).then((soundId: number) => { 216 console.info('Succeeded in loading uri'); 217 soundID = soundId; 218 }, (err: BusinessError) => { 219 console.error('Failed to load soundPool and catch error is ' + err.message); 220 }); 221 }); // '/test_01.mp3' here is only an example. You need to pass in the actual URI. 222 } 223}); 224 225``` 226 227### load 228 229load(fd: number, offset: number, length: number, callback: AsyncCallback\<number>): void 230 231Loads 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. 232 233>**NOTE** 234> 235> After the resource handle (in the form of an FD) or path description (in the form of a URI) is transferred to the player, do not use the resource handle or path description in read or write operations, including but not limited to transferring it to multiple players. 236> 237> Competition occurs when multiple players use the same resource handle or path description to read and write files at the same time, resulting in playback errors. 238 239**System capability**: SystemCapability.Multimedia.Media.SoundPool 240 241**Parameters** 242 243| Name | Type | Mandatory| Description | 244| -------- | ---------------------- | ---- | --------------------------- | 245| fd | number | Yes | Resource handle, which is obtained by calling [resourceManager.getRawFd](../apis-localization-kit/js-apis-resource-manager.md#getrawfd9). | 246| 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.| 247| 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.| 248| callback | AsyncCallback\<number> | Yes | Callback used to return the sound ID. A valid value must be greater than 0.| 249 250**Error codes** 251 252For details about the error codes, see [Media Error Codes](errorcode-media.md). 253 254| ID| Error Message | 255| -------- | --------------------------------------- | 256| 5400102 | Operation not allowed. Return by callback. | 257| 5400103 | I/O error. Return by callback. | 258| 5400105 | Service died. Return by callback. | 259 260**Example 1:** 261 262```ts 263import { fileIo } from '@kit.CoreFileKit'; 264import { BusinessError } from '@kit.BasicServicesKit'; 265import { media } from '@kit.MediaKit'; 266import { audio } from '@kit.AudioKit'; 267 268// Create a SoundPool instance. 269let soundPool: media.SoundPool; 270let audioRendererInfo: audio.AudioRendererInfo = { 271 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 272 rendererFlags: 1 273} 274media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 275 if (error) { 276 console.error(`Failed to createSoundPool`); 277 return; 278 } else { 279 soundPool = soundPool_; 280 console.info(`Succeeded in createSoundPool`); 281 let file: fileIo.File; 282 let soundID: number = 0; 283 let fileSize: number = 1; // Obtain the size through fileIo.stat(). 284 let uri: string = ""; 285 // Obtain the FD. The test_01.mp3 file is not an audio file in the rawfile directory. 286 fileIo.open('/test_01.mp3', fileIo.OpenMode.READ_ONLY).then((file_: fileIo.File) => { 287 file = file_; 288 console.info("file fd: " + file.fd); 289 uri = 'fd://' + (file.fd).toString(); 290 soundPool.load(file.fd, 0, fileSize, (error: BusinessError, soundId_: number) => { 291 if (error) { 292 console.error(`Failed to load soundPool: errCode is ${error.code}, errMessage is ${error.message}`); 293 } else { 294 soundID = soundId_; 295 console.info('Succeeded in loading soundId:' + soundId_); 296 } 297 }); 298 }); // '/test_01.mp3' here is only an example. You need to pass in the actual URI. 299 } 300}); 301 302``` 303 304**Example 2** 305 306```ts 307import { media } from '@kit.MediaKit'; 308import { audio } from '@kit.AudioKit'; 309import { BusinessError } from '@kit.BasicServicesKit'; 310 311function create(context: Context) { 312 // Create a SoundPool instance. 313 let soundPool: media.SoundPool; 314 let audioRendererInfo: audio.AudioRendererInfo = { 315 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 316 rendererFlags: 1 317 } 318 let soundID: number = 0; 319 media.createSoundPool(5, audioRendererInfo, async (error: BusinessError, soundPool_: media.SoundPool) => { 320 if (error) { 321 console.error(`Failed to createSoundPool`); 322 return; 323 } else { 324 soundPool = soundPool_; 325 console.info(`Succeeded in createSoundPool`); 326 // The test_01.mp3 file is an audio file in the rawfile directory. 327 let fileDescriptor = await context.resourceManager.getRawFd('test_01.mp3'); 328 soundPool.load(fileDescriptor.fd, fileDescriptor.offset, fileDescriptor.length, (error: BusinessError, soundId_: number) => { 329 if (error) { 330 console.error(`Failed to load soundPool: errCode is ${error.code}, errMessage is ${error.message}`); 331 } else { 332 soundID = soundId_; 333 console.info('Succeeded in loading soundId:' + soundId_); 334 } 335 }); 336 } 337 }); 338} 339 340``` 341 342### load 343 344load(fd: number, offset: number, length: number): Promise\<number> 345 346Loads 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. 347 348>**NOTE** 349> 350> After the resource handle (in the form of an FD) or path description (in the form of a URI) is transferred to the player, do not use the resource handle or path description in read or write operations, including but not limited to transferring it to multiple players. 351> 352> Competition occurs when multiple players use the same resource handle or path description to read and write files at the same time, resulting in playback errors. 353 354**System capability**: SystemCapability.Multimedia.Media.SoundPool 355 356**Parameters** 357 358| Name | Type | Mandatory| Description | 359| -------- | ---------------------- | ---- | --------------------------- | 360| fd | number | Yes | Resource handle, which is obtained by calling [resourceManager.getRawFd](../apis-localization-kit/js-apis-resource-manager.md#getrawfd9). | 361| 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.| 362| 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.| 363 364**Return value** 365 366| Type | Description | 367| ---------------- | -------------------------------- | 368| Promise\<number> | Promise used to return the sound ID. A valid value must be greater than 0.| 369 370**Error codes** 371 372For details about the error codes, see [Media Error Codes](errorcode-media.md). 373 374| ID| Error Message | 375| -------- | --------------------------------------- | 376| 5400102 | Operation not allowed. Return by promise.| 377| 5400103 | I/O error. Return by promise. | 378| 5400105 | Service died. Return by promise. | 379 380**Example 1:** 381 382```ts 383import { fileIo } from '@kit.CoreFileKit'; 384import { BusinessError } from '@kit.BasicServicesKit'; 385import { media } from '@kit.MediaKit'; 386import { audio } from '@kit.AudioKit'; 387 388// Create a SoundPool instance. 389let soundPool: media.SoundPool; 390let audioRendererInfo: audio.AudioRendererInfo = { 391 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 392 rendererFlags: 1 393} 394media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 395 if (error) { 396 console.error(`Failed to createSoundPool`); 397 return; 398 } else { 399 soundPool = soundPool_; 400 console.info(`Succeeded in createSoundPool`); 401 let file: fileIo.File; 402 let soundID: number = 0; 403 let fileSize: number = 1; // Obtain the size through fileIo.stat(). 404 let uri: string = ""; 405 // Obtain the FD. The test_01.mp3 file is not an audio file in the rawfile directory. 406 fileIo.open('/test_01.mp3', fileIo.OpenMode.READ_ONLY).then((file_: fileIo.File) => { 407 file = file_; 408 console.info("file fd: " + file.fd); 409 uri = 'fd://' + (file.fd).toString(); 410 soundPool.load(file.fd, 0, fileSize).then((soundId: number) => { 411 console.info('Succeeded in loading soundpool'); 412 soundID = soundId; 413 }, (err: BusinessError) => { 414 console.error('Failed to load soundpool and catch error is ' + err.message); 415 }); 416 }); 417 } 418}); 419 420``` 421 422**Example 2** 423 424```ts 425import { media } from '@kit.MediaKit'; 426import { audio } from '@kit.AudioKit'; 427import { BusinessError } from '@kit.BasicServicesKit'; 428 429function create(context: Context) { 430 // Create a SoundPool instance. 431 let soundPool: media.SoundPool; 432 let audioRendererInfo: audio.AudioRendererInfo = { 433 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 434 rendererFlags: 1 435 } 436 let soundID: number = 0; 437 media.createSoundPool(5, audioRendererInfo, async (error: BusinessError, soundPool_: media.SoundPool) => { 438 if (error) { 439 console.error(`Failed to createSoundPool`); 440 return; 441 } else { 442 soundPool = soundPool_; 443 console.info(`Succeeded in createSoundPool`); 444 // The test_01.mp3 file is an audio file in the rawfile directory. 445 let fileDescriptor = await context.resourceManager.getRawFd('test_01.mp3'); 446 soundPool.load(fileDescriptor.fd, fileDescriptor.offset, fileDescriptor.length).then((soundId: number) => { 447 console.info('Succeeded in loading soundpool'); 448 soundID = soundId; 449 }, (err: BusinessError) => { 450 console.error('Failed to load soundpool and catch error is ' + err.message); 451 }); 452 } 453 }); 454} 455 456``` 457 458### play 459 460play(soundID: number, params: PlayParameters, callback: AsyncCallback\<number>): void 461 462Plays a sound. This API uses an asynchronous callback to obtain the audio stream ID. 463 464**System capability**: SystemCapability.Multimedia.Media.SoundPool 465 466**Parameters** 467 468| Name | Type | Mandatory| Description | 469| -------- | ---------------------- | ---- | --------------------------- | 470| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 471| params | [PlayParameters](#playparameters) | Yes | Playback parameters.| 472| callback | AsyncCallback\<number> | Yes | Callback used to return the audio stream ID. A valid value must be greater than 0.| 473 474**Error codes** 475 476For details about the error codes, see [Media Error Codes](errorcode-media.md). 477 478| ID| Error Message | 479| -------- | --------------------------------------- | 480| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 481| 5400102 | Operation not allowed. Return by callback. | 482| 5400105 | Service died. Return by callback. | 483 484**Example** 485 486```js 487import { BusinessError } from '@kit.BasicServicesKit'; 488import { media } from '@kit.MediaKit'; 489import { audio } from '@kit.AudioKit'; 490 491// Create a SoundPool instance. 492let soundPool: media.SoundPool; 493let audioRendererInfo: audio.AudioRendererInfo = { 494 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 495 rendererFlags: 1 496} 497media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 498 if (error) { 499 console.error(`Failed to createSoundPool`); 500 return; 501 } else { 502 soundPool = soundPool_; 503 console.info(`Succeeded in createSoundPool`); 504 let soundID: number = 0; 505 let streamID: number = 0; 506 let playParameters: media.PlayParameters = { 507 loop: 3, // The sound is played four times (three loops). 508 rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // The sound is played at the original frequency. 509 leftVolume: 0.5, // range = 0.0-1.0 510 rightVolume: 0.5, // range = 0.0-1.0 511 priority: 0, // The sound playback has the lowest priority. 512 } 513 soundPool.play(soundID, playParameters, (error: BusinessError, streamId: number) => { 514 if (error) { 515 console.error(`Failed to play soundpool: errCode is ${error.code}, errMessage is ${error.message}`); 516 } else { 517 streamID = streamId; 518 console.info('Succeeded in playing soundpool, streamId:' + streamId); 519 } 520 }); 521 } 522}); 523 524``` 525 526### play 527 528play(soundID: number, callback: AsyncCallback\<number>): void 529 530Plays a sound using the default parameters. This API uses an asynchronous callback to obtain the audio stream ID. 531 532**System capability**: SystemCapability.Multimedia.Media.SoundPool 533 534**Parameters** 535 536| Name | Type | Mandatory| Description | 537| -------- | ---------------------- | ---- | --------------------------- | 538| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 539| callback | AsyncCallback\<number> | Yes | Callback used to return the audio stream ID. A valid value must be greater than 0.| 540 541**Error codes** 542 543For details about the error codes, see [Media Error Codes](errorcode-media.md). 544 545| ID| Error Message | 546| -------- | --------------------------------------- | 547| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 548| 5400102 | Operation not allowed. Return by callback. | 549| 5400105 | Service died. Return by callback. | 550 551**Example** 552 553```js 554import { BusinessError } from '@kit.BasicServicesKit'; 555import { media } from '@kit.MediaKit'; 556import { audio } from '@kit.AudioKit'; 557 558// Create a SoundPool instance. 559let soundPool: media.SoundPool; 560let audioRendererInfo: audio.AudioRendererInfo = { 561 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 562 rendererFlags: 1 563} 564media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 565 if (error) { 566 console.error(`Failed to createSoundPool`); 567 return; 568 } else { 569 soundPool = soundPool_; 570 console.info(`Succeeded in createSoundPool`); 571 let soundID: number = 0; 572 let streamID: number = 0; 573 soundPool.play(soundID, (error: BusinessError, streamId: number) => { 574 if (error) { 575 console.error(`Failed to play soundpool: errCode is ${error.code}, errMessage is ${error.message}`); 576 } else { 577 streamID = streamId; 578 console.info('Succeeded in playing soundpool, streamId:' + streamId); 579 } 580 }); 581 } 582}); 583 584``` 585 586### play 587 588play(soundID: number, params?: PlayParameters): Promise\<number> 589 590Plays a sound. This API uses a promise to obtain the audio stream ID. 591 592**System capability**: SystemCapability.Multimedia.Media.SoundPool 593 594**Parameters** 595 596| Name | Type | Mandatory| Description | 597| -------- | ---------------------- | ---- | --------------------------- | 598| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 599| params | [PlayParameters](#playparameters) | No | Playback parameters.| 600 601**Return value** 602 603| Type | Description | 604| ---------------- | -------------------------------- | 605| Promise\<number> | Promise used to return the audio stream ID. A valid value must be greater than 0.| 606 607**Error codes** 608 609For details about the error codes, see [Media Error Codes](errorcode-media.md). 610 611| ID| Error Message | 612| -------- | --------------------------------------- | 613| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. | 614| 5400102 | Operation not allowed. Return by promise. | 615| 5400105 | Service died. Return by promise. | 616 617**Example** 618 619```js 620import { BusinessError } from '@kit.BasicServicesKit'; 621import { media } from '@kit.MediaKit'; 622import { audio } from '@kit.AudioKit'; 623 624// Create a SoundPool instance. 625let soundPool: media.SoundPool; 626let audioRendererInfo: audio.AudioRendererInfo = { 627 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 628 rendererFlags: 1 629} 630media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 631 if (error) { 632 console.error(`Failed to createSoundPool`); 633 return; 634 } else { 635 soundPool = soundPool_; 636 console.info(`Succeeded in createSoundPool`); 637 let soundID: number = 0; 638 let streamID: number = 0; 639 let playParameters: media.PlayParameters = { 640 loop: 3, // The sound is played four times (three loops). 641 rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // The sound is played at the original frequency. 642 leftVolume: 0.5, // range = 0.0-1.0. 643 rightVolume: 0.5, // range = 0.0-1.0. 644 priority: 0, // The sound playback has the lowest priority. 645 } 646 647 soundPool.play(soundID, playParameters).then((streamId: number) => { 648 console.info('Succeeded in playing soundpool'); 649 streamID = streamId; 650 },(err: BusinessError) => { 651 console.error('Failed to play soundpool and catch error is ' + err.message); 652 }); 653 } 654}); 655 656``` 657 658### stop 659 660stop(streamID: number, callback: AsyncCallback\<void>): void 661 662Stops playing a sound. This API uses an asynchronous callback to return the result. 663 664**System capability**: SystemCapability.Multimedia.Media.SoundPool 665 666**Parameters** 667 668| Name | Type | Mandatory| Description | 669| -------- | ---------------------- | ---- | --------------------------- | 670| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 671| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 672 673**Error codes** 674 675For details about the error codes, see [Media Error Codes](errorcode-media.md). 676 677| ID| Error Message | 678| -------- | --------------------------------------- | 679| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 680| 5400102 | Operation not allowed. Return by callback. | 681| 5400105 | Service died. Return by callback. | 682 683**Example** 684 685```js 686import { BusinessError } from '@kit.BasicServicesKit'; 687import { media } from '@kit.MediaKit'; 688import { audio } from '@kit.AudioKit'; 689 690// Create a SoundPool instance. 691let soundPool: media.SoundPool; 692let audioRendererInfo: audio.AudioRendererInfo = { 693 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 694 rendererFlags: 1 695} 696media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 697 if (error) { 698 console.error(`Failed to createSoundPool`); 699 return; 700 } else { 701 soundPool = soundPool_; 702 console.info(`Succeeded in createSoundPool`); 703 let streamID: number = 0; 704 // Call play() to obtain the stream ID. 705 soundPool.stop(streamID, (error: BusinessError) => { 706 if (error) { 707 console.error(`Failed to stop soundpool: errCode is ${error.code}, errMessage is ${error.message}`); 708 } else { 709 console.info('Succeeded in stopping soundpool'); 710 } 711 }) 712 } 713}); 714 715``` 716 717### stop 718 719stop(streamID: number): Promise\<void> 720 721Stops playing a sound. This API uses a promise to return the result. 722 723**System capability**: SystemCapability.Multimedia.Media.SoundPool 724 725**Parameters** 726 727| Name | Type | Mandatory| Description | 728| -------- | ---------------------- | ---- | --------------------------- | 729| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 730 731**Return value** 732 733| Type | Description | 734| ---------------- | -------------------------------- | 735| Promise\<void> | Promise that returns no value.| 736 737**Error codes** 738 739For details about the error codes, see [Media Error Codes](errorcode-media.md). 740 741| ID| Error Message | 742| -------- | --------------------------------------- | 743| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. | 744| 5400102 | Operation not allowed. Return by promise. | 745| 5400105 | Service died. Return by promise. | 746 747**Example** 748 749```js 750import { BusinessError } from '@kit.BasicServicesKit'; 751import { media } from '@kit.MediaKit'; 752import { audio } from '@kit.AudioKit'; 753 754// Create a SoundPool instance. 755let soundPool: media.SoundPool; 756let audioRendererInfo: audio.AudioRendererInfo = { 757 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 758 rendererFlags: 1 759} 760media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 761 if (error) { 762 console.error(`Failed to createSoundPool`); 763 return; 764 } else { 765 soundPool = soundPool_; 766 console.info(`Succeeded in createSoundPool`); 767 let streamID: number = 0; 768 // Call play() to obtain the stream ID. 769 soundPool.stop(streamID).then(() => { 770 console.info('Succeeded in stopping soundpool'); 771 }, (err: BusinessError) => { 772 console.error('Failed to stop soundpool and catch error is ' + err.message); 773 }); 774 } 775}); 776``` 777 778### setLoop 779 780setLoop(streamID: number, loop: number, callback: AsyncCallback\<void>): void; 781 782Sets the loop mode for an audio stream. This API uses an asynchronous callback to return the result. 783 784**System capability**: SystemCapability.Multimedia.Media.SoundPool 785 786**Parameters** 787 788| Name | Type | Mandatory| Description | 789| -------- | ---------------------- | ---- | --------------------------- | 790| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 791| loop | number | Yes | Number of loops.<br>If this parameter is set to a value greater than or equal to 0, the number of times the content is actually played is the value of **loop** plus 1.<br> If this parameter is set to a value less than 0, the content is played repeatedly.| 792| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 793 794**Error codes** 795 796For details about the error codes, see [Media Error Codes](errorcode-media.md). 797 798| ID| Error Message | 799| -------- | --------------------------------------- | 800| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 801| 5400102 | Operation not allowed. Return by callback. | 802| 5400105 | Service died. Return by callback. | 803 804**Example** 805 806```js 807import { BusinessError } from '@kit.BasicServicesKit'; 808import { media } from '@kit.MediaKit'; 809import { audio } from '@kit.AudioKit'; 810 811// Create a SoundPool instance. 812let soundPool: media.SoundPool; 813let audioRendererInfo: audio.AudioRendererInfo = { 814 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 815 rendererFlags: 1 816} 817media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 818 if (error) { 819 console.error(`Failed to createSoundPool`); 820 return; 821 } else { 822 soundPool = soundPool_; 823 console.info(`Succeeded in createSoundPool`); 824 let streamID: number = 0; 825 // Call play() to obtain the stream ID. 826 // Set the number of loops to 2. 827 soundPool.setLoop(streamID, 2, (error: BusinessError) => { 828 if (error) { 829 console.error(`Failed to setLoop soundPool: errCode is ${error.code}, errMessage is ${error.message}`); 830 } else { 831 console.info('Succeeded in setLoopping soundpool, streamID:' + streamID); 832 } 833 }); 834 } 835}); 836 837``` 838 839### setLoop 840 841setLoop(streamID: number, loop: number): Promise\<void> 842 843Sets the loop mode for an audio stream. This API uses a promise to return the result. 844 845**System capability**: SystemCapability.Multimedia.Media.SoundPool 846 847**Parameters** 848 849| Name | Type | Mandatory| Description | 850| -------- | ---------------------- | ---- | --------------------------- | 851| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 852| loop | number | Yes | Number of loops.<br>If this parameter is set to a value greater than or equal to 0, the number of times the content is actually played is the value of **loop** plus 1.<br> If this parameter is set to a value less than 0, the content is played repeatedly.| 853 854**Return value** 855 856| Type | Description | 857| ---------------- | -------------------------------- | 858| Promise\<void> | Promise that returns no value.| 859 860**Error codes** 861 862For details about the error codes, see [Media Error Codes](errorcode-media.md). 863 864| ID| Error Message | 865| -------- | --------------------------------------- | 866| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. | 867| 5400102 | Operation not allowed. Return by promise. | 868| 5400105 | Service died. Return by promise. | 869 870**Example** 871 872```js 873import { BusinessError } from '@kit.BasicServicesKit'; 874import { media } from '@kit.MediaKit'; 875import { audio } from '@kit.AudioKit'; 876 877// Create a SoundPool instance. 878let soundPool: media.SoundPool; 879let audioRendererInfo: audio.AudioRendererInfo = { 880 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 881 rendererFlags: 1 882} 883media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 884 if (error) { 885 console.error(`Failed to createSoundPool`); 886 return; 887 } else { 888 soundPool = soundPool_; 889 console.info(`Succeeded in createSoundPool`); 890 let streamID: number = 0; 891 // Call play() to obtain the stream ID. 892 // Set the number of loops to 1. 893 soundPool.setLoop(streamID, 1).then(() => { 894 console.info('Succeeded in setLoopping soundpool, streamID:' + streamID); 895 }).catch((err: BusinessError) => { 896 console.error('Failed to setLoop soundPool and catch error is ' + err.message); 897 }); 898 } 899}); 900 901``` 902 903### setPriority 904 905setPriority(streamID: number, priority: number, callback: AsyncCallback\<void>): void 906 907Sets the priority for an audio stream. This API uses an asynchronous callback to return the result. 908 909**System capability**: SystemCapability.Multimedia.Media.SoundPool 910 911**Parameters** 912 913| Name | Type | Mandatory| Description | 914| -------- | ---------------------- | ---- | --------------------------- | 915| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 916| priority | number | Yes | Priority. The value **0** means the lowest priority. The value is an integer greater than or equal to 0.| 917| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 918 919**Error codes** 920 921For details about the error codes, see [Media Error Codes](errorcode-media.md). 922 923| ID| Error Message | 924| -------- | --------------------------------------- | 925| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 926| 5400102 | Operation not allowed. Return by callback. | 927| 5400105 | Service died. Return by callback. | 928 929**Example** 930 931```js 932import { BusinessError } from '@kit.BasicServicesKit'; 933import { media } from '@kit.MediaKit'; 934import { audio } from '@kit.AudioKit'; 935 936// Create a SoundPool instance. 937let soundPool: media.SoundPool; 938let audioRendererInfo: audio.AudioRendererInfo = { 939 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 940 rendererFlags: 1 941} 942media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 943 if (error) { 944 console.error(`Failed to createSoundPool`); 945 return; 946 } else { 947 soundPool = soundPool_; 948 console.info(`Succeeded in createSoundPool`); 949 let streamID: number = 0; 950 // Call play() to obtain the stream ID. 951 // Set the priority to 1. 952 soundPool.setPriority(streamID, 1, (error: BusinessError) => { 953 if (error) { 954 console.error(`Failed to setPriority soundPool: errCode is ${error.code}, errMessage is ${error.message}`); 955 } else { 956 console.info('Succeeded in setPriority soundpool, streamID:' + streamID); 957 } 958 }); 959 } 960}); 961 962``` 963 964### setPriority 965 966setPriority(streamID: number, priority: number): Promise\<void> 967 968Sets the priority for an audio stream. This API uses a promise to return the result. 969 970**System capability**: SystemCapability.Multimedia.Media.SoundPool 971 972**Parameters** 973 974| Name | Type | Mandatory| Description | 975| -------- | ---------------------- | ---- | --------------------------- | 976| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 977| priority | number | Yes | Priority. The value **0** means the lowest priority. The value is an integer greater than or equal to 0.| 978 979**Return value** 980 981| Type | Description | 982| ---------------- | -------------------------------- | 983| Promise\<void> | Promise that returns no value.| 984 985**Error codes** 986 987For details about the error codes, see [Media Error Codes](errorcode-media.md). 988 989| ID| Error Message | 990| -------- | --------------------------------------- | 991| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. | 992| 5400102 | Operation not allowed. Return by promise. | 993| 5400105 | Service died. Return by promise. | 994 995**Example** 996 997```js 998import { BusinessError } from '@kit.BasicServicesKit'; 999import { media } from '@kit.MediaKit'; 1000import { audio } from '@kit.AudioKit'; 1001 1002// Create a SoundPool instance. 1003let soundPool: media.SoundPool; 1004let audioRendererInfo: audio.AudioRendererInfo = { 1005 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1006 rendererFlags: 1 1007} 1008media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1009 if (error) { 1010 console.error(`Failed to createSoundPool`); 1011 return; 1012 } else { 1013 soundPool = soundPool_; 1014 console.info(`Succeeded in createSoundPool`); 1015 let streamID: number = 0; 1016 // Call play() to obtain the stream ID. 1017 // Set the priority to 1. 1018 1019 soundPool.setPriority(streamID, 1).then(() => { 1020 console.info('Succeeded in setPriority soundpool'); 1021 }, (err: BusinessError) => { 1022 console.error('Failed to setPriority soundPool and catch error is ' + err.message); 1023 }); 1024 } 1025}); 1026 1027``` 1028 1029### setRate 1030 1031setRate(streamID: number, rate: audio.AudioRendererRate, callback: AsyncCallback\<void>): void 1032 1033Sets the playback rate for an audio stream. This API uses an asynchronous callback to return the result. 1034 1035**System capability**: SystemCapability.Multimedia.Media.SoundPool 1036 1037**Parameters** 1038 1039| Name | Type | Mandatory| Description | 1040| -------- | ---------------------- | ---- | --------------------------- | 1041| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 1042| rate | [audio.AudioRendererRate](../apis-audio-kit/arkts-apis-audio-e.md#audiorendererrate8) | Yes | Playback rate.| 1043| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 1044 1045**Error codes** 1046 1047For details about the error codes, see [Media Error Codes](errorcode-media.md). 1048 1049| ID| Error Message | 1050| -------- | --------------------------------------- | 1051| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 1052| 5400102 | Operation not allowed. Return by callback. | 1053| 5400105 | Service died. Return by callback. | 1054 1055**Example** 1056 1057```js 1058import { BusinessError } from '@kit.BasicServicesKit'; 1059import { media } from '@kit.MediaKit'; 1060import { audio } from '@kit.AudioKit'; 1061 1062// Create a SoundPool instance. 1063let soundPool: media.SoundPool; 1064let audioRendererInfo: audio.AudioRendererInfo = { 1065 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1066 rendererFlags: 1 1067} 1068media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1069 if (error) { 1070 console.error(`Failed to createSoundPool`); 1071 return; 1072 } else { 1073 soundPool = soundPool_; 1074 console.info(`Succeeded in createSoundPool`); 1075 let streamID: number = 0; 1076 let selectedAudioRendererRate: audio.AudioRendererRate = audio.AudioRendererRate.RENDER_RATE_NORMAL; // The sound is played at the original frequency. 1077 // Call play() to obtain the stream ID. 1078 soundPool.setRate(streamID, selectedAudioRendererRate, (error: BusinessError) => { 1079 if (error) { 1080 console.error(`Failed to setRate soundPool: errCode is ${error.code}, errMessage is ${error.message}`); 1081 } else { 1082 console.info('Succeeded in setRate success, streamID:' + streamID); 1083 } 1084 }) 1085 } 1086}); 1087 1088``` 1089 1090### setRate 1091 1092setRate(streamID: number, rate: audio.AudioRendererRate): Promise\<void> 1093 1094Sets the playback rate for an audio stream. This API uses a promise to return the result. 1095 1096**System capability**: SystemCapability.Multimedia.Media.SoundPool 1097 1098**Parameters** 1099 1100| Name | Type | Mandatory| Description | 1101| -------- | ---------------------- | ---- | --------------------------- | 1102| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 1103| rate | [audio.AudioRendererRate](../apis-audio-kit/arkts-apis-audio-e.md#audiorendererrate8) | Yes | Playback rate.| 1104 1105**Return value** 1106 1107| Type | Description | 1108| ---------------- | -------------------------------- | 1109| Promise\<void> | Promise that returns no value.| 1110 1111**Error codes** 1112 1113For details about the error codes, see [Media Error Codes](errorcode-media.md). 1114 1115| ID| Error Message | 1116| -------- | --------------------------------------- | 1117| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. | 1118| 5400102 | Operation not allowed. Return by promise. | 1119| 5400105 | Service died. Return by promise. | 1120 1121**Example** 1122 1123```js 1124import { BusinessError } from '@kit.BasicServicesKit'; 1125import { media } from '@kit.MediaKit'; 1126import { audio } from '@kit.AudioKit'; 1127 1128// Create a SoundPool instance. 1129let soundPool: media.SoundPool; 1130let audioRendererInfo: audio.AudioRendererInfo = { 1131 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1132 rendererFlags: 1 1133} 1134media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1135 if (error) { 1136 console.error(`Failed to createSoundPool`); 1137 return; 1138 } else { 1139 soundPool = soundPool_; 1140 console.info(`Succeeded in createSoundPool`); 1141 let streamID: number = 0; 1142 let selectedAudioRendererRate: audio.AudioRendererRate = audio.AudioRendererRate.RENDER_RATE_NORMAL; // The sound is played at the original frequency. 1143 // Call play() to obtain the stream ID. 1144 soundPool.setRate(streamID, selectedAudioRendererRate).then(() => { 1145 console.info('Succeeded in setRate soundpool'); 1146 }, (err: BusinessError) => { 1147 console.error('Failed to setRate soundpool and catch error is ' + err.message); 1148 }); 1149 } 1150}); 1151 1152``` 1153 1154### setVolume 1155 1156setVolume(streamID: number, leftVolume: number, rightVolume: number, callback: AsyncCallback\<void>): void 1157 1158Sets the volume for an audio stream. This API uses an asynchronous callback to return the result. 1159 1160**System capability**: SystemCapability.Multimedia.Media.SoundPool 1161 1162**Parameters** 1163 1164| Name | Type | Mandatory| Description | 1165| -------- | ---------------------- | ---- | --------------------------- | 1166| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 1167| leftVolume | number | Yes | Volume of the left channel. The value ranges from 0.0 to 1.0.| 1168| rightVolume | number | Yes | Volume of the right channel. The value ranges from 0.0 to 1.0. Currently, setting the volume for the right channel does not take effect. The volume set for the left channel is used.| 1169| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 1170 1171**Error codes** 1172 1173For details about the error codes, see [Media Error Codes](errorcode-media.md). 1174 1175| ID| Error Message | 1176| -------- | --------------------------------------- | 1177| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 1178| 5400102 | Operation not allowed. Return by callback. | 1179| 5400105 | Service died. Return by callback. | 1180 1181**Example** 1182 1183```js 1184import { BusinessError } from '@kit.BasicServicesKit'; 1185import { media } from '@kit.MediaKit'; 1186import { audio } from '@kit.AudioKit'; 1187 1188// Create a SoundPool instance. 1189let soundPool: media.SoundPool; 1190let audioRendererInfo: audio.AudioRendererInfo = { 1191 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1192 rendererFlags: 1 1193} 1194media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1195 if (error) { 1196 console.error(`Failed to createSoundPool`); 1197 return; 1198 } else { 1199 soundPool = soundPool_; 1200 console.info(`Succeeded in createSoundPool`); 1201 let streamID: number = 0; 1202 // Call play() to obtain the stream ID. 1203 // Set the volume to 0.5. 1204 soundPool.setVolume(streamID, 0.5, 0.5, (error: BusinessError) => { 1205 if (error) { 1206 console.error(`Failed to setVolume soundPool: errCode is ${error.code}, errMessage is ${error.message}`); 1207 } else { 1208 console.info('Succeeded in setVolume soundpool, streamID:' + streamID); 1209 } 1210 }) 1211 } 1212}); 1213 1214``` 1215 1216### setVolume 1217 1218setVolume(streamID: number, leftVolume: number, rightVolume: number): Promise\<void> 1219 1220Sets the volume for an audio stream. This API uses a promise to return the result. 1221 1222**System capability**: SystemCapability.Multimedia.Media.SoundPool 1223 1224**Parameters** 1225 1226| Name | Type | Mandatory| Description | 1227| -------- | ---------------------- | ---- | --------------------------- | 1228| streamID | number | Yes | Audio stream ID, which is obtained by calling **play()**.| 1229| leftVolume | number | Yes | Volume of the left channel. The value ranges from 0.0 to 1.0.| 1230| rightVolume | number | Yes | Volume of the right channel. The value ranges from 0.0 to 1.0. Currently, setting the volume for the right channel does not take effect. The volume set for the left channel is used.| 1231 1232**Return value** 1233 1234| Type | Description | 1235| ---------------- | -------------------------------- | 1236| Promise\<void> | Promise that returns no value.| 1237 1238**Error codes** 1239 1240For details about the error codes, see [Media Error Codes](errorcode-media.md). 1241 1242| ID| Error Message | 1243| -------- | --------------------------------------- | 1244| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. | 1245| 5400102 | Operation not allowed. Return by promise. | 1246| 5400105 | Service died. Return by promise. | 1247 1248**Example** 1249 1250```js 1251import { BusinessError } from '@kit.BasicServicesKit'; 1252import { media } from '@kit.MediaKit'; 1253import { audio } from '@kit.AudioKit'; 1254 1255// Create a SoundPool instance. 1256let soundPool: media.SoundPool; 1257let audioRendererInfo: audio.AudioRendererInfo = { 1258 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1259 rendererFlags: 1 1260} 1261media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1262 if (error) { 1263 console.error(`Failed to createSoundPool`); 1264 return; 1265 } else { 1266 soundPool = soundPool_; 1267 console.info(`Succeeded in createSoundPool`); 1268 let streamID: number = 0; 1269 // Call play() to obtain the stream ID. 1270 1271 soundPool.setVolume(streamID, 0.5, 0.5).then(() => { 1272 console.info('Succeeded in setVolume soundpool'); 1273 }, (err: BusinessError) => { 1274 console.error('Failed to setVolume soundPool and catch error is ' + err.message); 1275 }); 1276 } 1277}); 1278 1279``` 1280 1281### unload 1282 1283unload(soundID: number, callback: AsyncCallback\<void>): void 1284 1285Unloads a sound. This API uses an asynchronous callback to return the result. 1286 1287**System capability**: SystemCapability.Multimedia.Media.SoundPool 1288 1289**Parameters** 1290 1291| Name | Type | Mandatory| Description | 1292| -------- | ---------------------- | ---- | --------------------------- | 1293| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 1294| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 1295 1296**Error codes** 1297 1298For details about the error codes, see [Media Error Codes](errorcode-media.md). 1299 1300| ID| Error Message | 1301| -------- | --------------------------------------- | 1302| 5400102 | Operation not allowed. Return by callback. | 1303| 5400103 | I/O error. Return by callback. | 1304| 5400105 | Service died. Return by callback. | 1305 1306**Example** 1307 1308```js 1309import { BusinessError } from '@kit.BasicServicesKit'; 1310import { media } from '@kit.MediaKit'; 1311import { audio } from '@kit.AudioKit'; 1312 1313// Create a SoundPool instance. 1314let soundPool: media.SoundPool; 1315let audioRendererInfo: audio.AudioRendererInfo = { 1316 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1317 rendererFlags: 1 1318} 1319media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1320 if (error) { 1321 console.error(`Failed to createSoundPool`); 1322 return; 1323 } else { 1324 soundPool = soundPool_; 1325 console.info(`Succeeded in createSoundPool`); 1326 let soundID: number = 0; 1327 // Call load() to obtain the sound ID. 1328 soundPool.unload(soundID, (error: BusinessError) => { 1329 if (error) { 1330 console.error(`Failed to unload soundPool: errCode is ${error.code}, errMessage is ${error.message}`); 1331 } else { 1332 console.info('Succceeded in unload soundPool'); 1333 } 1334 }) 1335 } 1336}); 1337 1338``` 1339 1340### unload 1341 1342unload(soundID: number): Promise\<void> 1343 1344Unloads a sound. This API uses a promise to return the result. 1345 1346**System capability**: SystemCapability.Multimedia.Media.SoundPool 1347 1348**Parameters** 1349 1350| Name | Type | Mandatory| Description | 1351| -------- | ---------------------- | ---- | --------------------------- | 1352| soundID | number | Yes | Sound ID, which is obtained by calling **load()**.| 1353 1354**Return value** 1355 1356| Type | Description | 1357| ---------------- | -------------------------------- | 1358| Promise\<void> | Promise that returns no value.| 1359 1360**Error codes** 1361 1362For details about the error codes, see [Media Error Codes](errorcode-media.md). 1363 1364| ID| Error Message | 1365| -------- | --------------------------------------- | 1366| 5400102 | Operation not allowed. Return by promise. | 1367| 5400103 | I/O error. Return by promise. | 1368| 5400105 | Service died. Return by promise. | 1369 1370**Example** 1371 1372```js 1373import { BusinessError } from '@kit.BasicServicesKit'; 1374import { media } from '@kit.MediaKit'; 1375import { audio } from '@kit.AudioKit'; 1376 1377// Create a SoundPool instance. 1378let soundPool: media.SoundPool; 1379let audioRendererInfo: audio.AudioRendererInfo = { 1380 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1381 rendererFlags: 1 1382} 1383media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1384 if (error) { 1385 console.error(`Failed to createSoundPool`); 1386 return; 1387 } else { 1388 soundPool = soundPool_; 1389 console.info(`Succeeded in createSoundPool`); 1390 let soundID: number = 0; 1391 // Call load() to obtain the sound ID. 1392 1393 soundPool.unload(soundID).then(() => { 1394 console.info('Succceeded in unload soundPool'); 1395 }, (err: BusinessError) => { 1396 console.error('Failed to unload soundPool and catch error is ' + err.message); 1397 }); 1398 } 1399}); 1400 1401``` 1402 1403### release 1404 1405release(callback: AsyncCallback\<void>): void 1406 1407Releases this SoundPool instance. This API uses an asynchronous callback to return the result. 1408 1409**System capability**: SystemCapability.Multimedia.Media.SoundPool 1410 1411**Parameters** 1412 1413| Name | Type | Mandatory| Description | 1414| -------- | ---------------------- | ---- | --------------------------- | 1415| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 1416 1417**Error codes** 1418 1419For details about the error codes, see [Media Error Codes](errorcode-media.md). 1420 1421| ID| Error Message | 1422| -------- | --------------------------------------- | 1423| 5400105 | Service died. Return by callback. | 1424 1425**Example** 1426 1427```js 1428import { BusinessError } from '@kit.BasicServicesKit'; 1429import { media } from '@kit.MediaKit'; 1430import { audio } from '@kit.AudioKit'; 1431 1432// Create a SoundPool instance. 1433let soundPool: media.SoundPool; 1434let audioRendererInfo: audio.AudioRendererInfo = { 1435 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1436 rendererFlags: 1 1437} 1438media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1439 if (error) { 1440 console.error(`Failed to createSoundPool`); 1441 return; 1442 } else { 1443 soundPool = soundPool_; 1444 console.info(`Succeeded in createSoundPool`); 1445 soundPool.release((error: BusinessError) => { 1446 if (error) { 1447 console.error(`Failed to release soundPool: errCode is ${error.code}, errMessage is ${error.message}`); 1448 } else { 1449 console.info('Succeeded in releasing soundPool'); 1450 } 1451 }) 1452 } 1453}); 1454 1455 1456``` 1457 1458### release 1459 1460release(): Promise\<void> 1461 1462Releases this SoundPool instance. This API uses a promise to return the result. 1463 1464**System capability**: SystemCapability.Multimedia.Media.SoundPool 1465 1466**Return value** 1467 1468| Type | Description | 1469| ---------------- | -------------------------------- | 1470| Promise\<void> | Promise that returns no value.| 1471 1472**Error codes** 1473 1474For details about the error codes, see [Media Error Codes](errorcode-media.md). 1475 1476| ID| Error Message | 1477| -------- | --------------------------------------- | 1478| 5400105 | Service died. Return by promise. | 1479 1480**Example** 1481 1482```js 1483import { BusinessError } from '@kit.BasicServicesKit'; 1484import { media } from '@kit.MediaKit'; 1485import { audio } from '@kit.AudioKit'; 1486 1487// Create a SoundPool instance. 1488let soundPool: media.SoundPool; 1489let audioRendererInfo: audio.AudioRendererInfo = { 1490 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1491 rendererFlags: 1 1492} 1493media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1494 if (error) { 1495 console.error(`Failed to createSoundPool`); 1496 return; 1497 } else { 1498 soundPool = soundPool_; 1499 console.info(`Succeeded in createSoundPool`); 1500 soundPool.release().then(() => { 1501 console.info('Succeeded in releasing soundPool'); 1502 }, (err: BusinessError) => { 1503 console.error('Failed to release soundPool and catch error is ' + err.message); 1504 }); 1505 } 1506}); 1507 1508``` 1509 1510### on('loadComplete') 1511 1512on(type: 'loadComplete', callback: Callback\<number>): void 1513 1514Subscribes to events indicating that a sound finishes loading. 1515 1516**System capability**: SystemCapability.Multimedia.Media.SoundPool 1517 1518**Parameters** 1519 1520| Name | Type | Mandatory| Description | 1521| -------- | -------- | ---- | ------------------------------------------------------------ | 1522| type | string | Yes | Event type, which is **'loadComplete'** in this case. This event is triggered when a sound is loaded.| 1523| callback | Callback\<number> | Yes | ID of the sound that has been loaded. | 1524 1525**Example** 1526 1527```js 1528import { BusinessError } from '@kit.BasicServicesKit'; 1529import { media } from '@kit.MediaKit'; 1530import { audio } from '@kit.AudioKit'; 1531 1532// Create a SoundPool instance. 1533let soundPool: media.SoundPool; 1534let audioRendererInfo: audio.AudioRendererInfo = { 1535 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1536 rendererFlags: 1 1537} 1538media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1539 if (error) { 1540 console.error(`Failed to createSoundPool`); 1541 return; 1542 } else { 1543 soundPool = soundPool_; 1544 console.info(`Succeeded in createSoundPool`); 1545 soundPool.on('loadComplete', (soundId: number) => { 1546 console.info('Succeeded in loadComplete, soundId: ' + soundId); 1547 }) 1548 } 1549}); 1550 1551``` 1552 1553### off('loadComplete') 1554 1555off(type: 'loadComplete'): void 1556 1557Unsubscribes from events indicating that a sound finishes loading. 1558 1559**System capability**: SystemCapability.Multimedia.Media.SoundPool 1560 1561**Parameters** 1562 1563| Name| Type | Mandatory| Description | 1564| ------ | ------ | ---- | ------------------------------------------------------------ | 1565| type | string | Yes | Event type. The value is fixed at **'loadComplete'**.| 1566 1567**Example** 1568 1569```js 1570import { BusinessError } from '@kit.BasicServicesKit'; 1571import { media } from '@kit.MediaKit'; 1572import { audio } from '@kit.AudioKit'; 1573 1574// Create a SoundPool instance. 1575let soundPool: media.SoundPool; 1576let audioRendererInfo: audio.AudioRendererInfo = { 1577 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1578 rendererFlags: 1 1579} 1580media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1581 if (error) { 1582 console.error(`Failed to createSoundPool`); 1583 return; 1584 } else { 1585 soundPool = soundPool_; 1586 console.info(`Succeeded in createSoundPool`); 1587 soundPool.off('loadComplete'); 1588 } 1589}); 1590 1591``` 1592 1593### on('playFinishedWithStreamId')<sup>18+</sup> 1594 1595on(type: 'playFinishedWithStreamId', callback: Callback\<number>): void 1596 1597Subscribes to events indicating the completion of audio playback and returns the stream ID of the audio that finishes playing. 1598 1599When only [on('playFinished')](#onplayfinished) or [on('playFinishedWithStreamId')](#onplayfinishedwithstreamid18) is subscribed to, the registered callback is triggered when the audio playback is complete. 1600 1601When both [on('playFinished')](#onplayfinished) and [on('playFinishedWithStreamId')](#onplayfinishedwithstreamid18) are subscribed to, the 'playFinishedWithStreamId' callback is triggered, but the 'playFinished' callback is not triggered, when the audio playback is complete. 1602 1603**System capability**: SystemCapability.Multimedia.Media.SoundPool 1604 1605**Parameters** 1606 1607| Name | Type | Mandatory| Description | 1608| -------- | -------- | ---- | ------------------------------------------------------------ | 1609| type | string | Yes | Event type, which is **'playFinishedWithStreamId'** in this case. This event is triggered when an audio stream finishes playing, and the stream ID is returned.| 1610| callback | Callback\<number> | Yes | Callback used to return the result. Stream ID of the audio that finishes playing. | 1611 1612**Example** 1613 1614```js 1615import { BusinessError } from '@kit.BasicServicesKit'; 1616import { media } from '@kit.MediaKit'; 1617import { audio } from '@kit.AudioKit'; 1618 1619// Create a SoundPool instance. 1620let soundPool_: media.SoundPool; 1621let audioRendererInfo: audio.AudioRendererInfo = { 1622 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1623 rendererFlags: 1 1624} 1625media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool: media.SoundPool) => { 1626 if (error) { 1627 console.error(`Failed to createSoundPool`); 1628 } else { 1629 soundPool_ = soundPool; 1630 console.info(`Succeeded in createSoundPool`); 1631 soundPool_.on('playFinishedWithStreamId', (streamId) => { 1632 console.info('The stream with streamId: ' + streamId + ' has finished playing.'); 1633 }); 1634 } 1635}); 1636 1637``` 1638 1639### off('playFinishedWithStreamId')<sup>18+</sup> 1640 1641off(type: 'playFinishedWithStreamId'): void 1642 1643Unsubscribes from events indicating that a sound finishes playing. 1644 1645**System capability**: SystemCapability.Multimedia.Media.SoundPool 1646 1647**Parameters** 1648 1649| Name| Type | Mandatory| Description | 1650| ------ | ------ | ---- | ------------------------------------------------------------ | 1651| type | string | Yes | Event type. The value is fixed at **'playFinishedWithStreamId'**.| 1652 1653**Example** 1654 1655```js 1656import { BusinessError } from '@kit.BasicServicesKit'; 1657import { media } from '@kit.MediaKit'; 1658import { audio } from '@kit.AudioKit'; 1659 1660// Create a SoundPool instance. 1661let soundPool_: media.SoundPool; 1662let audioRendererInfo: audio.AudioRendererInfo = { 1663 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1664 rendererFlags: 1 1665} 1666media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool: media.SoundPool) => { 1667 if (error) { 1668 console.error(`Failed to createSoundPool`); 1669 } else { 1670 soundPool_ = soundPool; 1671 console.info(`Succeeded in createSoundPool`); 1672 soundPool_.off('playFinishedWithStreamId'); 1673 } 1674}); 1675 1676``` 1677 1678### on('playFinished') 1679 1680on(type: 'playFinished', callback: Callback\<void>): void 1681 1682Subscribes to events indicating that a sound finishes playing. 1683 1684**System capability**: SystemCapability.Multimedia.Media.SoundPool 1685 1686**Parameters** 1687 1688| Name | Type | Mandatory| Description | 1689| -------- | -------- | ---- | ------------------------------------------------------------ | 1690| type | string | Yes | Event type, which is **'playFinished'** in this case. This event is triggered when a sound finishes playing.| 1691| callback | Callback\<void> | Yes | Callback used to return the result. | 1692 1693**Example** 1694 1695```js 1696import { BusinessError } from '@kit.BasicServicesKit'; 1697import { media } from '@kit.MediaKit'; 1698import { audio } from '@kit.AudioKit'; 1699 1700// Create a SoundPool instance. 1701let soundPool: media.SoundPool; 1702let audioRendererInfo: audio.AudioRendererInfo = { 1703 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1704 rendererFlags: 1 1705} 1706media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1707 if (error) { 1708 console.error(`Failed to createSoundPool`); 1709 return; 1710 } else { 1711 soundPool = soundPool_; 1712 console.info(`Succeeded in createSoundPool`); 1713 soundPool.on('playFinished', () => { 1714 console.info('Succeeded in playFinished'); 1715 }); 1716 } 1717}); 1718 1719``` 1720 1721### off('playFinished') 1722 1723off(type: 'playFinished'): void 1724 1725Unsubscribes from events indicating that a sound finishes playing. 1726 1727**System capability**: SystemCapability.Multimedia.Media.SoundPool 1728 1729**Parameters** 1730 1731| Name| Type | Mandatory| Description | 1732| ------ | ------ | ---- | ------------------------------------------------------------ | 1733| type | string | Yes | Event type. The value is fixed at **'playFinished'**.| 1734 1735**Example** 1736 1737```js 1738import { BusinessError } from '@kit.BasicServicesKit'; 1739import { media } from '@kit.MediaKit'; 1740import { audio } from '@kit.AudioKit'; 1741 1742// Create a SoundPool instance. 1743let soundPool: media.SoundPool; 1744let audioRendererInfo: audio.AudioRendererInfo = { 1745 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1746 rendererFlags: 1 1747} 1748media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1749 if (error) { 1750 console.error(`Failed to createSoundPool`); 1751 return; 1752 } else { 1753 soundPool = soundPool_; 1754 console.info(`Succeeded in createSoundPool`); 1755 soundPool.off('playFinished'); 1756 } 1757}); 1758 1759``` 1760 1761### on('error') 1762 1763on(type: 'error', callback: ErrorCallback): void 1764 1765Subscribes to error events of this SoundPool instance. This event is used only for error prompt. 1766 1767**System capability**: SystemCapability.Multimedia.Media.SoundPool 1768 1769**Parameters** 1770 1771| Name | Type | Mandatory| Description | 1772| -------- | -------- | ---- | ------------------------------------------------------------ | 1773| type | string | Yes | Event type, which is **'error'** in this case. This event can be triggered by both user operations and the system.| 1774| callback | ErrorCallback | Yes | Callback used to return the error code ID and error message.| 1775 1776**Example** 1777 1778```js 1779import { BusinessError } from '@kit.BasicServicesKit'; 1780import { media } from '@kit.MediaKit'; 1781import { audio } from '@kit.AudioKit'; 1782 1783// Create a SoundPool instance. 1784let soundPool: media.SoundPool; 1785let audioRendererInfo: audio.AudioRendererInfo = { 1786 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1787 rendererFlags: 1 1788} 1789media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1790 if (error) { 1791 console.error(`Failed to createSoundPool`); 1792 return; 1793 } else { 1794 soundPool = soundPool_; 1795 console.info(`Succeeded in createSoundPool`); 1796 soundPool.on('error', (error: BusinessError) => { 1797 console.error('error happened,and error message is :' + error.message); 1798 console.error('error happened,and error code is :' + error.code); 1799 }) 1800 } 1801}); 1802 1803``` 1804 1805### off('error') 1806 1807off(type: 'error'): void 1808 1809Unsubscribes from error events of this SoundPool instance. 1810 1811**System capability**: SystemCapability.Multimedia.Media.SoundPool 1812 1813**Parameters** 1814 1815| Name| Type | Mandatory| Description | 1816| ------ | ------ | ---- | ----------------------------------------- | 1817| type | string | Yes | Event type, which is **'error'** in this case.| 1818 1819**Example** 1820 1821```js 1822import { BusinessError } from '@kit.BasicServicesKit'; 1823import { media } from '@kit.MediaKit'; 1824import { audio } from '@kit.AudioKit'; 1825 1826// Create a SoundPool instance. 1827let soundPool: media.SoundPool; 1828let audioRendererInfo: audio.AudioRendererInfo = { 1829 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1830 rendererFlags: 1 1831} 1832media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1833 if (error) { 1834 console.error(`Failed to createSoundPool`); 1835 return; 1836 } else { 1837 soundPool = soundPool_; 1838 console.info(`Succeeded in createSoundPool`); 1839 soundPool.off('error'); 1840 } 1841}); 1842``` 1843 1844### on('errorOccurred')<sup>20+</sup> 1845 1846on(type: 'errorOccurred', callback: Callback\<ErrorInfo>): void 1847 1848Subscribes to error events of this [SoundPool](#soundpool) instance and returns [ErrorInfo](#errorinfo20) that contains the error code, error stage, resource ID, and audio stream ID. 1849 1850**System capability**: SystemCapability.Multimedia.Media.SoundPool 1851 1852**Parameters** 1853 1854| Name | Type | Mandatory| Description | 1855| -------- | -------- | ---- | ------------------------------------------------------------ | 1856| type | string | Yes | Event type, which is **'errorOccurred'** in this case. This event can be triggered by both user operations and the system.| 1857| callback | Callback\<[ErrorInfo](#errorinfo20)> | Yes | Callback used to return [ErrorInfo](#errorinfo20).| 1858 1859**Example** 1860 1861```js 1862import { BusinessError } from '@kit.BasicServicesKit'; 1863import { media } from '@kit.MediaKit'; 1864import { audio } from '@kit.AudioKit'; 1865 1866// Create a SoundPool instance. 1867let soundPool: media.SoundPool; 1868let audioRendererInfo: audio.AudioRendererInfo = { 1869 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1870 rendererFlags: 1 1871} 1872media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1873 if (error) { 1874 console.error(`Failed to createSoundPool`); 1875 return; 1876 } else { 1877 soundPool = soundPool_; 1878 console.info(`Succeeded in createSoundPool`); 1879 soundPool.on('errorOccurred', (errorInfo) => { 1880 console.error('error happened,and error message is :' + errorInfo.errorCode.message); 1881 console.error('error happened,and error code is :' + errorInfo.errorCode.code); 1882 console.error('error happened,and errorType is :' + errorInfo.errorType); 1883 console.error('error happened,and soundId is :' + errorInfo.soundId); 1884 console.error('error happened,and streamId is :' + errorInfo.streamId); 1885 }) 1886 } 1887}); 1888 1889``` 1890 1891### off('errorOccurred')<sup>20+</sup> 1892 1893 off(type: 'errorOccurred', callback?: Callback\<ErrorInfo>): void 1894 1895Unsubscribes from error events of this SoundPool instance. 1896 1897**System capability**: SystemCapability.Multimedia.Media.SoundPool 1898 1899**Parameters** 1900 1901| Name| Type | Mandatory| Description | 1902| ------ | ------ | ---- | ----------------------------------------- | 1903| type | string | Yes | Event type, which is **'errorOccurred'** in this case.| 1904| callback | Callback\<[ErrorInfo](#errorinfo20)> | No | Callback used to return [ErrorInfo](#errorinfo20).| 1905 1906**Example** 1907 1908```js 1909import { BusinessError } from '@kit.BasicServicesKit'; 1910import { media } from '@kit.MediaKit'; 1911import { audio } from '@kit.AudioKit'; 1912 1913// Create a SoundPool instance. 1914let soundPool: media.SoundPool; 1915let audioRendererInfo: audio.AudioRendererInfo = { 1916 usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1917 rendererFlags: 1 1918} 1919media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1920 if (error) { 1921 console.error(`Failed to createSoundPool`); 1922 return; 1923 } else { 1924 soundPool = soundPool_; 1925 console.info(`Succeeded in createSoundPool`); 1926 soundPool.off('errorOccurred'); 1927 } 1928}); 1929``` 1930