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