1e41f4b71Sopenharmony_ci# SoundPool (音频池) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci音频池提供了短音频的加载、播放、音量设置、循环设置、停止播放、资源卸载等功能。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciSoundPool需要和@ohos.multimedia.media配合使用,需要先通过[media.createSoundPool](js-apis-media.md#mediacreatesoundpool10)完成音频池实例的创建。 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci> **说明:** 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## 导入模块 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```js 14e41f4b71Sopenharmony_ciimport { media } from '@kit.MediaKit'; 15e41f4b71Sopenharmony_ciimport { audio } from '@kit.AudioKit'; 16e41f4b71Sopenharmony_ci``` 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## PlayParameters 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci表示音频池播放参数设置。 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci通过设置播放相关参数,来控制播放的音量,循环次数,播放优先级等参数。 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 27e41f4b71Sopenharmony_ci| --------------- | ---------------------------------------- | ---- | ------------------------------------------------------------ | 28e41f4b71Sopenharmony_ci| loop | number | 否 | 设置循环参数,0为循环一次,-1表示一直循环。默认值:0。 | 29e41f4b71Sopenharmony_ci| rate | number | 否 | 设置音频播放的倍速,具体倍速范围参照[AudioRendererRate](../apis-audio-kit/js-apis-audio.md#audiorendererrate8)。默认值:0。 | 30e41f4b71Sopenharmony_ci| leftVolume | number | 否 | 设置左声道音量,设置范围(0.0~1.0)。默认值:1.0。 | 31e41f4b71Sopenharmony_ci| rightVolume | number | 否 | 设置右声道音量。(当前不支持左右分别设置,将以左声道音量为准)。默认值:1.0。 | 32e41f4b71Sopenharmony_ci| priority | number | 否 | 音频流播放的优先级,0为最低优先级,数值越大优先级越高,通过相互比较大小确定播放优先级。默认值:0。 | 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci## SoundPool 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci音频池提供了系统声音的加载、播放、音量设置、循环设置、停止播放、资源卸载等功能, 在调用SoundPool的接口前,需要先通过[createSoundPool](js-apis-media.md#mediacreatesoundpool10)创建实例。 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci> **说明:** 39e41f4b71Sopenharmony_ci> 40e41f4b71Sopenharmony_ci> 在使用SoundPool实例的方法时,建议开发者注册相关回调,主动获取当前状态变化。 41e41f4b71Sopenharmony_ci> - [on('loadComplete')](#onloadcomplete):监听资源加载完成。 42e41f4b71Sopenharmony_ci> - [on('playFinished')](#onplayfinished):监听播放完成。 43e41f4b71Sopenharmony_ci> - [on('error')](#onerror):监听错误事件。 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci### load 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ciload(uri: string, callback: AsyncCallback\<number>): void 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci加载音频资源。使用callback方式异步获取资源ID,入参uri通过获取文件fd生成以"fd://"开头的文件描述字符串。 50e41f4b71Sopenharmony_ci该方法不支持加载rawfile目录资源,需要通过[load(fd: number, offset: number, length: number, callback: AsyncCallback\<number>): void](#load-2)或者[load(fd: number, offset: number, length: number): Promise\<number>](#load-3)实现。 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci>**说明:** 53e41f4b71Sopenharmony_ci> 54e41f4b71Sopenharmony_ci>将资源句柄(fd)或加载路径描述(uri)传递给音频池播放器之后,请不要通过该资源句柄或加载路径描述做其他读写操作,包括但不限于将同一个资源句柄或加载路径描述传递给多个音频池播放器。 55e41f4b71Sopenharmony_ci>同一时间通过同一个资源句柄或加载路径描述读写文件时存在竞争关系,将导致播放异常。 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci**参数:** 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 62e41f4b71Sopenharmony_ci| -------- | -------------------------------------- | ---- | ------------------------------------- | 63e41f4b71Sopenharmony_ci| uri | string | 是 | 音频文件的加载路径描述,一般以"fd://"开头的文件描述。 | 64e41f4b71Sopenharmony_ci| callback | AsyncCallback\<number> | 是 | 异步音频资源加载返回的资源id,有效值大于0。 | 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**错误码:** 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 71e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 72e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by callback.| 73e41f4b71Sopenharmony_ci| 5400103 | I/O error. Return by callback. | 74e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by callback. | 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci**示例:** 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci```ts 79e41f4b71Sopenharmony_ciimport { fileIo } from '@kit.CoreFileKit'; 80e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci//创建soundPool实例 83e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 84e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 85e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 86e41f4b71Sopenharmony_ci rendererFlags: 1 87e41f4b71Sopenharmony_ci} 88e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 89e41f4b71Sopenharmony_ci if (error) { 90e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 91e41f4b71Sopenharmony_ci return; 92e41f4b71Sopenharmony_ci } else { 93e41f4b71Sopenharmony_ci soundPool = soundPool_; 94e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 95e41f4b71Sopenharmony_ci let uri:string = ""; 96e41f4b71Sopenharmony_ci let file: fileIo.File; 97e41f4b71Sopenharmony_ci //获取fd的uri路径 98e41f4b71Sopenharmony_ci fileIo.open('/test_01.mp3', fileIo.OpenMode.READ_ONLY).then((file_: fileIo.File) => { 99e41f4b71Sopenharmony_ci file = file_; 100e41f4b71Sopenharmony_ci console.info("file fd: " + file.fd); 101e41f4b71Sopenharmony_ci uri = 'fd://' + (file.fd).toString() 102e41f4b71Sopenharmony_ci soundPool.load(uri, (error: BusinessError, soundId_: number) => { 103e41f4b71Sopenharmony_ci if (error) { 104e41f4b71Sopenharmony_ci console.error(`Failed to load soundPool: errCode is ${error.code}, errMessage is ${error.message}`) 105e41f4b71Sopenharmony_ci } else { 106e41f4b71Sopenharmony_ci console.info(`Succeeded in loading soundPool` + JSON.stringify(soundId_)) 107e41f4b71Sopenharmony_ci } 108e41f4b71Sopenharmony_ci }); 109e41f4b71Sopenharmony_ci }); // '/test_01.mp3' 作为样例,使用时需要传入文件对应路径。 110e41f4b71Sopenharmony_ci } 111e41f4b71Sopenharmony_ci}); 112e41f4b71Sopenharmony_ci``` 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci### load 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ciload(uri: string): Promise\<number> 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci加载音频资源。使用Promise方式异步获取资源ID,入参uri通过获取文件fd生成以"fd://"开头的文件描述字符串。 119e41f4b71Sopenharmony_ci该方法不支持加载rawfile目录资源,需要通过[load(fd: number, offset: number, length: number, callback: AsyncCallback\<number>): void](#load-2)或者[load(fd: number, offset: number, length: number): Promise\<number>](#load-3)实现。 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci>**说明:** 122e41f4b71Sopenharmony_ci> 123e41f4b71Sopenharmony_ci>将资源句柄(fd)或加载路径描述(uri)传递给音频池播放器之后,请不要通过该资源句柄或加载路径描述做其他读写操作,包括但不限于将同一个资源句柄或加载路径描述传递给多个音频池播放器。 124e41f4b71Sopenharmony_ci>同一时间通过同一个资源句柄或加载路径描述读写文件时存在竞争关系,将导致播放异常。 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci**参数:** 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 131e41f4b71Sopenharmony_ci| ------ | -------------------------------------- | ---- | -------------------------- | 132e41f4b71Sopenharmony_ci| uri | string | 是 | 音频文件的加载路径描述,一般以"fd://"开头的文件描述。 | 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**返回值:** 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci| 类型 | 说明 | 137e41f4b71Sopenharmony_ci| -------------- | ------------------------------------------ | 138e41f4b71Sopenharmony_ci| Promise\<number> | 以Promise方式异步加载音频池资源,返回资源的id,有效值大于0。 | 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci**错误码:** 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 145e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 146e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by promise.| 147e41f4b71Sopenharmony_ci| 5400103 | I/O error. Return by promise. | 148e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by promise. | 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci**示例:** 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci```ts 153e41f4b71Sopenharmony_ciimport { fileIo } from '@kit.CoreFileKit'; 154e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci//创建soundPool实例 157e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 158e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 159e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 160e41f4b71Sopenharmony_ci rendererFlags: 1 161e41f4b71Sopenharmony_ci} 162e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 163e41f4b71Sopenharmony_ci if (error) { 164e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 165e41f4b71Sopenharmony_ci return; 166e41f4b71Sopenharmony_ci } else { 167e41f4b71Sopenharmony_ci soundPool = soundPool_; 168e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 169e41f4b71Sopenharmony_ci let uri:string = ""; 170e41f4b71Sopenharmony_ci let soundID: number = 0; 171e41f4b71Sopenharmony_ci let file: fileIo.File; 172e41f4b71Sopenharmony_ci //获取fd的uri路径 173e41f4b71Sopenharmony_ci fileIo.open('/test_01.mp3', fileIo.OpenMode.READ_ONLY).then((file_: fileIo.File) => { 174e41f4b71Sopenharmony_ci file = file_; 175e41f4b71Sopenharmony_ci console.info("file fd: " + file.fd); 176e41f4b71Sopenharmony_ci uri = 'fd://' + (file.fd).toString() 177e41f4b71Sopenharmony_ci soundPool.load(uri).then((soundId: number) => { 178e41f4b71Sopenharmony_ci console.info('Succeeded in loading uri'); 179e41f4b71Sopenharmony_ci soundID = soundId; 180e41f4b71Sopenharmony_ci }, (err: BusinessError) => { 181e41f4b71Sopenharmony_ci console.error('Failed to load soundPool and catch error is ' + err.message); 182e41f4b71Sopenharmony_ci }); 183e41f4b71Sopenharmony_ci }); // '/test_01.mp3' 作为样例,使用时需要传入文件对应路径。 184e41f4b71Sopenharmony_ci } 185e41f4b71Sopenharmony_ci}); 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci``` 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci### load 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ciload(fd: number, offset: number, length: number, callback: AsyncCallback\<number>): void 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci加载音频资源。使用callback方式异步获取资源ID,入参可手动传入资源信息或通过读取应用内置资源自动获取。 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci>**说明:** 196e41f4b71Sopenharmony_ci> 197e41f4b71Sopenharmony_ci>将资源句柄(fd)或加载路径描述(uri)传递给音频池播放器之后,请不要通过该资源句柄或加载路径描述做其他读写操作,包括但不限于将同一个资源句柄或加载路径描述传递给多个音频池播放器。 198e41f4b71Sopenharmony_ci>同一时间通过同一个资源句柄或加载路径描述读写文件时存在竞争关系,将导致播放异常。 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci**参数:** 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 205e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 206e41f4b71Sopenharmony_ci| fd | number | 是 | 资源句柄,通过resourceManager.getRawFileDescriptor获取。 | 207e41f4b71Sopenharmony_ci| offset | number | 是 | 资源偏移量,需要基于预置资源的信息输入,非法值会造成音视频资源解析错误。 | 208e41f4b71Sopenharmony_ci| length | number | 是 | 资源长度,需要基于预置资源的信息输入,非法值会造成音视频资源解析错误。 | 209e41f4b71Sopenharmony_ci| callback | AsyncCallback\<number> | 是 | 获取回调的soundID,有效值大于0。 | 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci**错误码:** 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 216e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 217e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by callback. | 218e41f4b71Sopenharmony_ci| 5400103 | I/O error. Return by callback. | 219e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by callback. | 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci**示例1:** 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci```ts 224e41f4b71Sopenharmony_ciimport { fileIo } from '@kit.CoreFileKit'; 225e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci//创建soundPool实例 228e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 229e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 230e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 231e41f4b71Sopenharmony_ci rendererFlags: 1 232e41f4b71Sopenharmony_ci} 233e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 234e41f4b71Sopenharmony_ci if (error) { 235e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 236e41f4b71Sopenharmony_ci return; 237e41f4b71Sopenharmony_ci } else { 238e41f4b71Sopenharmony_ci soundPool = soundPool_; 239e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 240e41f4b71Sopenharmony_ci let file: fileIo.File; 241e41f4b71Sopenharmony_ci let soundID: number = 0; 242e41f4b71Sopenharmony_ci let fileSize: number = 1; //通过fileIo.stat()获取size值 243e41f4b71Sopenharmony_ci let uri: string = ""; 244e41f4b71Sopenharmony_ci //获取fd的描述信息,test_01.mp3不是rawfile目录资源下面的音频 245e41f4b71Sopenharmony_ci fileIo.open('/test_01.mp3', fileIo.OpenMode.READ_ONLY).then((file_: fileIo.File) => { 246e41f4b71Sopenharmony_ci file = file_; 247e41f4b71Sopenharmony_ci console.info("file fd: " + file.fd); 248e41f4b71Sopenharmony_ci uri = 'fd://' + (file.fd).toString() 249e41f4b71Sopenharmony_ci soundPool.load(file.fd, 0, fileSize, (error: BusinessError, soundId_: number) => { 250e41f4b71Sopenharmony_ci if (error) { 251e41f4b71Sopenharmony_ci console.error(`Failed to load soundPool: errCode is ${error.code}, errMessage is ${error.message}`) 252e41f4b71Sopenharmony_ci } else { 253e41f4b71Sopenharmony_ci soundID = soundId_; 254e41f4b71Sopenharmony_ci console.info('Succeeded in loading soundId:' + soundId_); 255e41f4b71Sopenharmony_ci } 256e41f4b71Sopenharmony_ci }); 257e41f4b71Sopenharmony_ci }); // '/test_01.mp3' 作为样例,使用时需要传入文件对应路径。 258e41f4b71Sopenharmony_ci } 259e41f4b71Sopenharmony_ci}); 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci``` 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci**示例2:** 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci```ts 266e41f4b71Sopenharmony_ciimport { media } from '@kit.MediaKit'; 267e41f4b71Sopenharmony_ciimport { audio } from '@kit.AudioKit'; 268e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci//创建soundPool实例 271e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 272e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 273e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 274e41f4b71Sopenharmony_ci rendererFlags: 1 275e41f4b71Sopenharmony_ci} 276e41f4b71Sopenharmony_cilet soundID: number = 0; 277e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 278e41f4b71Sopenharmony_ci if (error) { 279e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 280e41f4b71Sopenharmony_ci return; 281e41f4b71Sopenharmony_ci } else { 282e41f4b71Sopenharmony_ci soundPool = soundPool_; 283e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 284e41f4b71Sopenharmony_ci //test_01.mp3为rawfile目录资源下面的音频 285e41f4b71Sopenharmony_ci let fileDescriptor = await getContext().resourceManager.getRawFd('test_01.mp3'); 286e41f4b71Sopenharmony_ci soundPool.load(fileDescriptor.fd, fileDescriptor.offset, fileDescriptor.length, (error: BusinessError, soundId_: number) => { 287e41f4b71Sopenharmony_ci if (error) { 288e41f4b71Sopenharmony_ci console.error(`Failed to load soundPool: errCode is ${error.code}, errMessage is ${error.message}`) 289e41f4b71Sopenharmony_ci } else { 290e41f4b71Sopenharmony_ci soundID = soundId_; 291e41f4b71Sopenharmony_ci console.info('Succeeded in loading soundId:' + soundId_); 292e41f4b71Sopenharmony_ci } 293e41f4b71Sopenharmony_ci }); 294e41f4b71Sopenharmony_ci } 295e41f4b71Sopenharmony_ci}); 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci``` 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci### load 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ciload(fd: number, offset: number, length: number): Promise\<number> 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ci加载音频资源。使用Promise方式异步获取资源ID,入参可手动传入资源信息或通过读取应用内置资源自动获取。 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci>**说明:** 306e41f4b71Sopenharmony_ci> 307e41f4b71Sopenharmony_ci>将资源句柄(fd)或加载路径描述(uri)传递给音频池播放器之后,请不要通过该资源句柄或加载路径描述做其他读写操作,包括但不限于将同一个资源句柄或加载路径描述传递给多个音频池播放器。 308e41f4b71Sopenharmony_ci>同一时间通过同一个资源句柄或加载路径描述读写文件时存在竞争关系,将导致播放异常。 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci**参数:** 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 315e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 316e41f4b71Sopenharmony_ci| fd | number | 是 | 资源句柄,通过resourceManager.getRawFileDescriptor获取。 | 317e41f4b71Sopenharmony_ci| offset | number | 是 | 资源偏移量,需要基于预置资源的信息输入,非法值会造成音视频资源解析错误。 | 318e41f4b71Sopenharmony_ci| length | number | 是 | 资源长度,需要基于预置资源的信息输入,非法值会造成音视频资源解析错误。 | 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_ci**返回值:** 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci| 类型 | 说明 | 323e41f4b71Sopenharmony_ci| ---------------- | -------------------------------- | 324e41f4b71Sopenharmony_ci| Promise\<number> | 以Promise方式获取返回的soundID,有效值大于0。 | 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci**错误码:** 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 331e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 332e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by promise.| 333e41f4b71Sopenharmony_ci| 5400103 | I/O error. Return by promise. | 334e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by promise. | 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci**示例1:** 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci```ts 339e41f4b71Sopenharmony_ciimport { fileIo } from '@kit.CoreFileKit'; 340e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci//创建soundPool实例 343e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 344e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 345e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 346e41f4b71Sopenharmony_ci rendererFlags: 1 347e41f4b71Sopenharmony_ci} 348e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 349e41f4b71Sopenharmony_ci if (error) { 350e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 351e41f4b71Sopenharmony_ci return; 352e41f4b71Sopenharmony_ci } else { 353e41f4b71Sopenharmony_ci soundPool = soundPool_; 354e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 355e41f4b71Sopenharmony_ci let file: fileIo.File; 356e41f4b71Sopenharmony_ci let soundID: number = 0; 357e41f4b71Sopenharmony_ci let fileSize: number = 1; //通过fileIo.stat()获取size值 358e41f4b71Sopenharmony_ci let uri: string = ""; 359e41f4b71Sopenharmony_ci //获取fd的描述信息,test_01.mp3不是rawfile目录资源下面的音频 360e41f4b71Sopenharmony_ci fileIo.open('/test_01.mp3', fileIo.OpenMode.READ_ONLY).then((file_: fileIo.File) => { 361e41f4b71Sopenharmony_ci file = file_; 362e41f4b71Sopenharmony_ci console.info("file fd: " + file.fd); 363e41f4b71Sopenharmony_ci uri = 'fd://' + (file.fd).toString() 364e41f4b71Sopenharmony_ci soundPool.load(file.fd, 0, fileSize).then((soundId: number) => { 365e41f4b71Sopenharmony_ci console.info('Succeeded in loading soundpool'); 366e41f4b71Sopenharmony_ci soundID = soundId; 367e41f4b71Sopenharmony_ci }, (err: BusinessError) => { 368e41f4b71Sopenharmony_ci console.error('Failed to load soundpool and catch error is ' + err.message); 369e41f4b71Sopenharmony_ci }); 370e41f4b71Sopenharmony_ci }); 371e41f4b71Sopenharmony_ci } 372e41f4b71Sopenharmony_ci}); 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci``` 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci**示例2:** 377e41f4b71Sopenharmony_ci 378e41f4b71Sopenharmony_ci```ts 379e41f4b71Sopenharmony_ciimport { media } from '@kit.MediaKit'; 380e41f4b71Sopenharmony_ciimport { audio } from '@kit.AudioKit'; 381e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci//创建soundPool实例 384e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 385e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 386e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 387e41f4b71Sopenharmony_ci rendererFlags: 1 388e41f4b71Sopenharmony_ci} 389e41f4b71Sopenharmony_cilet soundID: number = 0; 390e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 391e41f4b71Sopenharmony_ci if (error) { 392e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 393e41f4b71Sopenharmony_ci return; 394e41f4b71Sopenharmony_ci } else { 395e41f4b71Sopenharmony_ci soundPool = soundPool_; 396e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 397e41f4b71Sopenharmony_ci //test_01.mp3为rawfile目录资源下面的音频 398e41f4b71Sopenharmony_ci let fileDescriptor = await getContext().resourceManager.getRawFd('test_01.mp3'); 399e41f4b71Sopenharmony_ci soundPool.load(fileDescriptor.fd, fileDescriptor.offset, fileDescriptor.length).then((soundId: number) => { 400e41f4b71Sopenharmony_ci console.info('Succeeded in loading soundpool'); 401e41f4b71Sopenharmony_ci soundID = soundId; 402e41f4b71Sopenharmony_ci }, (err: BusinessError) => { 403e41f4b71Sopenharmony_ci console.error('Failed to load soundpool and catch error is ' + err.message); 404e41f4b71Sopenharmony_ci }); 405e41f4b71Sopenharmony_ci } 406e41f4b71Sopenharmony_ci}); 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci``` 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci### play 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ciplay(soundID: number, params: PlayParameters, callback: AsyncCallback\<number>): void 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci播放音频资源。使用callback方式异步获取音频流streamID。 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci**参数:** 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 421e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 422e41f4b71Sopenharmony_ci| soundID | number | 是 | 资源ID,通过load方法获取。 | 423e41f4b71Sopenharmony_ci| params | [PlayParameters](#playparameters) | 是 | play播放相关参数的设置。 | 424e41f4b71Sopenharmony_ci| callback | AsyncCallback\<number> | 是 | 获取回调的音频流ID,有效值大于0。 | 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ci**错误码:** 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 431e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 432e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 433e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by callback. | 434e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by callback. | 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci**示例:** 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci```js 439e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ci//创建soundPool实例 442e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 443e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 444e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 445e41f4b71Sopenharmony_ci rendererFlags: 1 446e41f4b71Sopenharmony_ci} 447e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 448e41f4b71Sopenharmony_ci if (error) { 449e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 450e41f4b71Sopenharmony_ci return; 451e41f4b71Sopenharmony_ci } else { 452e41f4b71Sopenharmony_ci soundPool = soundPool_; 453e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 454e41f4b71Sopenharmony_ci let soundID: number = 0; 455e41f4b71Sopenharmony_ci let streamID: number = 0; 456e41f4b71Sopenharmony_ci let playParameters: media.PlayParameters = { 457e41f4b71Sopenharmony_ci loop: 3, // 循环3次 458e41f4b71Sopenharmony_ci rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // 正常倍速 459e41f4b71Sopenharmony_ci leftVolume: 0.5, // range = 0.0-1.0 460e41f4b71Sopenharmony_ci rightVolume: 0.5, // range = 0.0-1.0 461e41f4b71Sopenharmony_ci priority: 0, // 最低优先级 462e41f4b71Sopenharmony_ci } 463e41f4b71Sopenharmony_ci soundPool.play(soundID, playParameters, (error: BusinessError, streamId: number) => { 464e41f4b71Sopenharmony_ci if (error) { 465e41f4b71Sopenharmony_ci console.error(`Failed to play soundpool: errCode is ${error.code}, errMessage is ${error.message}`) 466e41f4b71Sopenharmony_ci } else { 467e41f4b71Sopenharmony_ci streamID = streamId; 468e41f4b71Sopenharmony_ci console.info('Succeeded in playing soundpool, streamId:' + streamId); 469e41f4b71Sopenharmony_ci } 470e41f4b71Sopenharmony_ci }); 471e41f4b71Sopenharmony_ci } 472e41f4b71Sopenharmony_ci}); 473e41f4b71Sopenharmony_ci 474e41f4b71Sopenharmony_ci``` 475e41f4b71Sopenharmony_ci 476e41f4b71Sopenharmony_ci### play 477e41f4b71Sopenharmony_ci 478e41f4b71Sopenharmony_ciplay(soundID: number, callback: AsyncCallback\<number>): void 479e41f4b71Sopenharmony_ci 480e41f4b71Sopenharmony_ci播放音频资源。使用callback方式异步获取音频流streamID。 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ci**参数:** 485e41f4b71Sopenharmony_ci 486e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 487e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 488e41f4b71Sopenharmony_ci| soundID | number | 是 | 资源ID,通过load方法获取。 | 489e41f4b71Sopenharmony_ci| callback | AsyncCallback\<number> | 是 | 获取回调的音频流ID,有效值大于0。 | 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ci**错误码:** 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 496e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 497e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 498e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by callback. | 499e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by callback. | 500e41f4b71Sopenharmony_ci 501e41f4b71Sopenharmony_ci**示例:** 502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci```js 504e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ci//创建soundPool实例 507e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 508e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 509e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 510e41f4b71Sopenharmony_ci rendererFlags: 1 511e41f4b71Sopenharmony_ci} 512e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 513e41f4b71Sopenharmony_ci if (error) { 514e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 515e41f4b71Sopenharmony_ci return; 516e41f4b71Sopenharmony_ci } else { 517e41f4b71Sopenharmony_ci soundPool = soundPool_; 518e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 519e41f4b71Sopenharmony_ci let soundID: number = 0; 520e41f4b71Sopenharmony_ci let streamID: number = 0; 521e41f4b71Sopenharmony_ci soundPool.play(soundID, (error: BusinessError, streamId: number) => { 522e41f4b71Sopenharmony_ci if (error) { 523e41f4b71Sopenharmony_ci console.error(`Failed to play soundpool: errCode is ${error.code}, errMessage is ${error.message}`) 524e41f4b71Sopenharmony_ci } else { 525e41f4b71Sopenharmony_ci streamID = streamId; 526e41f4b71Sopenharmony_ci console.info('Succeeded in playing soundpool, streamId:' + streamId); 527e41f4b71Sopenharmony_ci } 528e41f4b71Sopenharmony_ci }); 529e41f4b71Sopenharmony_ci } 530e41f4b71Sopenharmony_ci}); 531e41f4b71Sopenharmony_ci 532e41f4b71Sopenharmony_ci``` 533e41f4b71Sopenharmony_ci 534e41f4b71Sopenharmony_ci### play 535e41f4b71Sopenharmony_ci 536e41f4b71Sopenharmony_ciplay(soundID: number, params?: PlayParameters): Promise\<number> 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_ci播放音频资源。使用Promise方式异步获取音频流streamID。 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 541e41f4b71Sopenharmony_ci 542e41f4b71Sopenharmony_ci**参数:** 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 545e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 546e41f4b71Sopenharmony_ci| soundID | number | 是 | 资源ID,通过load方法获取。 | 547e41f4b71Sopenharmony_ci| params | [PlayParameters](#playparameters) | 否 | play播放相关参数的设置。 | 548e41f4b71Sopenharmony_ci 549e41f4b71Sopenharmony_ci**返回值:** 550e41f4b71Sopenharmony_ci 551e41f4b71Sopenharmony_ci| 类型 | 说明 | 552e41f4b71Sopenharmony_ci| ---------------- | -------------------------------- | 553e41f4b71Sopenharmony_ci| Promise\<number> | 以Promise方式获取返回的音频流ID,有效值大于0。 | 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ci**错误码:** 556e41f4b71Sopenharmony_ci 557e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 558e41f4b71Sopenharmony_ci 559e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 560e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 561e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. | 562e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by promise. | 563e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by promise. | 564e41f4b71Sopenharmony_ci 565e41f4b71Sopenharmony_ci**示例:** 566e41f4b71Sopenharmony_ci 567e41f4b71Sopenharmony_ci```js 568e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 569e41f4b71Sopenharmony_ci 570e41f4b71Sopenharmony_ci//创建soundPool实例 571e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 572e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 573e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 574e41f4b71Sopenharmony_ci rendererFlags: 1 575e41f4b71Sopenharmony_ci} 576e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 577e41f4b71Sopenharmony_ci if (error) { 578e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 579e41f4b71Sopenharmony_ci return; 580e41f4b71Sopenharmony_ci } else { 581e41f4b71Sopenharmony_ci soundPool = soundPool_; 582e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 583e41f4b71Sopenharmony_ci let soundID: number = 0; 584e41f4b71Sopenharmony_ci let streamID: number = 0; 585e41f4b71Sopenharmony_ci let playParameters: media.PlayParameters = { 586e41f4b71Sopenharmony_ci loop: 3, // 循环4次 587e41f4b71Sopenharmony_ci rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // 正常倍速 588e41f4b71Sopenharmony_ci leftVolume: 0.5, // range = 0.0-1.0 589e41f4b71Sopenharmony_ci rightVolume: 0.5, // range = 0.0-1.0 590e41f4b71Sopenharmony_ci priority: 0, // 最低优先级 591e41f4b71Sopenharmony_ci } 592e41f4b71Sopenharmony_ci 593e41f4b71Sopenharmony_ci soundPool.play(soundID, playParameters).then((streamId: number) => { 594e41f4b71Sopenharmony_ci console.info('Succeeded in playing soundpool'); 595e41f4b71Sopenharmony_ci streamID = streamId; 596e41f4b71Sopenharmony_ci },(err: BusinessError) => { 597e41f4b71Sopenharmony_ci console.error('Failed to play soundpool and catch error is ' + err.message); 598e41f4b71Sopenharmony_ci }); 599e41f4b71Sopenharmony_ci } 600e41f4b71Sopenharmony_ci}); 601e41f4b71Sopenharmony_ci 602e41f4b71Sopenharmony_ci``` 603e41f4b71Sopenharmony_ci 604e41f4b71Sopenharmony_ci### stop 605e41f4b71Sopenharmony_ci 606e41f4b71Sopenharmony_cistop(streamID: number, callback: AsyncCallback\<void>): void 607e41f4b71Sopenharmony_ci 608e41f4b71Sopenharmony_ci停止播放音频资源。使用callback方式异步获取返回值。 609e41f4b71Sopenharmony_ci 610e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 611e41f4b71Sopenharmony_ci 612e41f4b71Sopenharmony_ci**参数:** 613e41f4b71Sopenharmony_ci 614e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 615e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 616e41f4b71Sopenharmony_ci| streamID | number | 是 | 音频流ID,通过play方法获取。 | 617e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 异步音频池stop的回调方法。 | 618e41f4b71Sopenharmony_ci 619e41f4b71Sopenharmony_ci**错误码:** 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 624e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 625e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 626e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by callback. | 627e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by callback. | 628e41f4b71Sopenharmony_ci 629e41f4b71Sopenharmony_ci**示例:** 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ci```js 632e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 633e41f4b71Sopenharmony_ci 634e41f4b71Sopenharmony_ci//创建soundPool实例 635e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 636e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 637e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 638e41f4b71Sopenharmony_ci rendererFlags: 1 639e41f4b71Sopenharmony_ci} 640e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 641e41f4b71Sopenharmony_ci if (error) { 642e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 643e41f4b71Sopenharmony_ci return; 644e41f4b71Sopenharmony_ci } else { 645e41f4b71Sopenharmony_ci soundPool = soundPool_; 646e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 647e41f4b71Sopenharmony_ci let streamID: number = 0; 648e41f4b71Sopenharmony_ci //先调用play方法给拿到对应的streamID 649e41f4b71Sopenharmony_ci soundPool.stop(streamID, (error: BusinessError) => { 650e41f4b71Sopenharmony_ci if (error) { 651e41f4b71Sopenharmony_ci console.error(`Failed to stop soundpool: errCode is ${error.code}, errMessage is ${error.message}`) 652e41f4b71Sopenharmony_ci } else { 653e41f4b71Sopenharmony_ci console.info('Succeeded in stopping soundpool'); 654e41f4b71Sopenharmony_ci } 655e41f4b71Sopenharmony_ci }) 656e41f4b71Sopenharmony_ci } 657e41f4b71Sopenharmony_ci}); 658e41f4b71Sopenharmony_ci 659e41f4b71Sopenharmony_ci``` 660e41f4b71Sopenharmony_ci 661e41f4b71Sopenharmony_ci### stop 662e41f4b71Sopenharmony_ci 663e41f4b71Sopenharmony_cistop(streamID: number): Promise\<void> 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_ci停止streamID对应的音频播放。使用Promise方式异步获取返回值。 666e41f4b71Sopenharmony_ci 667e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 668e41f4b71Sopenharmony_ci 669e41f4b71Sopenharmony_ci**参数:** 670e41f4b71Sopenharmony_ci 671e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 672e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 673e41f4b71Sopenharmony_ci| streamID | number | 是 | 音频流ID,通过play方法获取。 | 674e41f4b71Sopenharmony_ci 675e41f4b71Sopenharmony_ci**返回值:** 676e41f4b71Sopenharmony_ci 677e41f4b71Sopenharmony_ci| 类型 | 说明 | 678e41f4b71Sopenharmony_ci| ---------------- | -------------------------------- | 679e41f4b71Sopenharmony_ci| Promise\<void> | 以Promise方式返回,无返回值。 | 680e41f4b71Sopenharmony_ci 681e41f4b71Sopenharmony_ci**错误码:** 682e41f4b71Sopenharmony_ci 683e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 684e41f4b71Sopenharmony_ci 685e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 686e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 687e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. | 688e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by promise. | 689e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by promise. | 690e41f4b71Sopenharmony_ci 691e41f4b71Sopenharmony_ci**示例:** 692e41f4b71Sopenharmony_ci 693e41f4b71Sopenharmony_ci```js 694e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 695e41f4b71Sopenharmony_ci 696e41f4b71Sopenharmony_ci//创建soundPool实例 697e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 698e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 699e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 700e41f4b71Sopenharmony_ci rendererFlags: 1 701e41f4b71Sopenharmony_ci} 702e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 703e41f4b71Sopenharmony_ci if (error) { 704e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 705e41f4b71Sopenharmony_ci return; 706e41f4b71Sopenharmony_ci } else { 707e41f4b71Sopenharmony_ci soundPool = soundPool_; 708e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 709e41f4b71Sopenharmony_ci let streamID: number = 0; 710e41f4b71Sopenharmony_ci //先调用play方法给拿到对应的streamID 711e41f4b71Sopenharmony_ci soundPool.stop(streamID).then(() => { 712e41f4b71Sopenharmony_ci console.info('Succeeded in stopping soundpool'); 713e41f4b71Sopenharmony_ci }, (err: BusinessError) => { 714e41f4b71Sopenharmony_ci console.error('Failed to stop soundpool and catch error is ' + err.message); 715e41f4b71Sopenharmony_ci }); 716e41f4b71Sopenharmony_ci } 717e41f4b71Sopenharmony_ci}); 718e41f4b71Sopenharmony_ci``` 719e41f4b71Sopenharmony_ci 720e41f4b71Sopenharmony_ci### setLoop 721e41f4b71Sopenharmony_ci 722e41f4b71Sopenharmony_cisetLoop(streamID: number, loop: number, callback: AsyncCallback\<void>): void; 723e41f4b71Sopenharmony_ci 724e41f4b71Sopenharmony_ci设置循环模式。使用callback方式异步获取返回值。 725e41f4b71Sopenharmony_ci 726e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 727e41f4b71Sopenharmony_ci 728e41f4b71Sopenharmony_ci**参数:** 729e41f4b71Sopenharmony_ci 730e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 731e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 732e41f4b71Sopenharmony_ci| streamID | number | 是 | 音频流ID,通过play方法获取。 | 733e41f4b71Sopenharmony_ci| loop | number | 是 | 设置循环的次数,0为默认1次,-1为一直循环。 | 734e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 异步setLoop的回调方法。 | 735e41f4b71Sopenharmony_ci 736e41f4b71Sopenharmony_ci**错误码:** 737e41f4b71Sopenharmony_ci 738e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 739e41f4b71Sopenharmony_ci 740e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 741e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 742e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 743e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by callback. | 744e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by callback. | 745e41f4b71Sopenharmony_ci 746e41f4b71Sopenharmony_ci**示例:** 747e41f4b71Sopenharmony_ci 748e41f4b71Sopenharmony_ci```js 749e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 750e41f4b71Sopenharmony_ci 751e41f4b71Sopenharmony_ci//创建soundPool实例 752e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 753e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 754e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 755e41f4b71Sopenharmony_ci rendererFlags: 1 756e41f4b71Sopenharmony_ci} 757e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 758e41f4b71Sopenharmony_ci if (error) { 759e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 760e41f4b71Sopenharmony_ci return; 761e41f4b71Sopenharmony_ci } else { 762e41f4b71Sopenharmony_ci soundPool = soundPool_; 763e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 764e41f4b71Sopenharmony_ci let streamID: number = 0; 765e41f4b71Sopenharmony_ci //先通过调用play方法获取到对应的streamID 766e41f4b71Sopenharmony_ci //设置循环2次 767e41f4b71Sopenharmony_ci soundPool.setLoop(streamID, 2, (error: BusinessError) => { 768e41f4b71Sopenharmony_ci if (error) { 769e41f4b71Sopenharmony_ci console.error(`Failed to setLoop soundPool: errCode is ${error.code}, errMessage is ${error.message}`) 770e41f4b71Sopenharmony_ci } else { 771e41f4b71Sopenharmony_ci console.info('Succeeded in setLoopping soundpool, streamID:' + streamID); 772e41f4b71Sopenharmony_ci } 773e41f4b71Sopenharmony_ci }); 774e41f4b71Sopenharmony_ci } 775e41f4b71Sopenharmony_ci}); 776e41f4b71Sopenharmony_ci 777e41f4b71Sopenharmony_ci``` 778e41f4b71Sopenharmony_ci 779e41f4b71Sopenharmony_ci### setLoop 780e41f4b71Sopenharmony_ci 781e41f4b71Sopenharmony_cisetLoop(streamID: number, loop: number): Promise\<void> 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ci设置循环模式。使用Promise方式异步获取返回值。 784e41f4b71Sopenharmony_ci 785e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 786e41f4b71Sopenharmony_ci 787e41f4b71Sopenharmony_ci**参数:** 788e41f4b71Sopenharmony_ci 789e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 790e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 791e41f4b71Sopenharmony_ci| streamID | number | 是 | 音频流ID,通过play方法获取。 | 792e41f4b71Sopenharmony_ci| loop | number | 是 | 设置循环的次数,0为默认1次,-1为一直循环。| 793e41f4b71Sopenharmony_ci 794e41f4b71Sopenharmony_ci**返回值:** 795e41f4b71Sopenharmony_ci 796e41f4b71Sopenharmony_ci| 类型 | 说明 | 797e41f4b71Sopenharmony_ci| ---------------- | -------------------------------- | 798e41f4b71Sopenharmony_ci| Promise\<void> | 异步音频池setLoop方法的Promise返回值。 | 799e41f4b71Sopenharmony_ci 800e41f4b71Sopenharmony_ci**错误码:** 801e41f4b71Sopenharmony_ci 802e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 803e41f4b71Sopenharmony_ci 804e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 805e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 806e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. | 807e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by promise. | 808e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by promise. | 809e41f4b71Sopenharmony_ci 810e41f4b71Sopenharmony_ci**示例:** 811e41f4b71Sopenharmony_ci 812e41f4b71Sopenharmony_ci```js 813e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 814e41f4b71Sopenharmony_ci 815e41f4b71Sopenharmony_ci//创建soundPool实例 816e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 817e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 818e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 819e41f4b71Sopenharmony_ci rendererFlags: 1 820e41f4b71Sopenharmony_ci} 821e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 822e41f4b71Sopenharmony_ci if (error) { 823e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 824e41f4b71Sopenharmony_ci return; 825e41f4b71Sopenharmony_ci } else { 826e41f4b71Sopenharmony_ci soundPool = soundPool_; 827e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 828e41f4b71Sopenharmony_ci let streamID: number = 0; 829e41f4b71Sopenharmony_ci //先通过调用play方法获取到对应的streamID 830e41f4b71Sopenharmony_ci //设置循环1次 831e41f4b71Sopenharmony_ci soundPool.setLoop(streamID, 1).then(() => { 832e41f4b71Sopenharmony_ci console.info('Succeeded in setLoopping soundpool, streamID:' + streamID); 833e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 834e41f4b71Sopenharmony_ci console.error('Failed to setLoop soundPool and catch error is ' + err.message); 835e41f4b71Sopenharmony_ci }); 836e41f4b71Sopenharmony_ci } 837e41f4b71Sopenharmony_ci}); 838e41f4b71Sopenharmony_ci 839e41f4b71Sopenharmony_ci``` 840e41f4b71Sopenharmony_ci 841e41f4b71Sopenharmony_ci### setPriority 842e41f4b71Sopenharmony_ci 843e41f4b71Sopenharmony_cisetPriority(streamID: number, priority: number, callback: AsyncCallback\<void>): void 844e41f4b71Sopenharmony_ci 845e41f4b71Sopenharmony_ci设置音频流播放的优先级。使用callback方式异步获取返回值。 846e41f4b71Sopenharmony_ci 847e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 848e41f4b71Sopenharmony_ci 849e41f4b71Sopenharmony_ci**参数:** 850e41f4b71Sopenharmony_ci 851e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 852e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 853e41f4b71Sopenharmony_ci| streamID | number | 是 | 音频流ID,通过play方法获取。 | 854e41f4b71Sopenharmony_ci| priority | number | 是 | 优先级,0表示最低优先级。 | 855e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 异步音频池setPriority方法的回调方法。 | 856e41f4b71Sopenharmony_ci 857e41f4b71Sopenharmony_ci**错误码:** 858e41f4b71Sopenharmony_ci 859e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 860e41f4b71Sopenharmony_ci 861e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 862e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 863e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 864e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by callback. | 865e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by callback. | 866e41f4b71Sopenharmony_ci 867e41f4b71Sopenharmony_ci**示例:** 868e41f4b71Sopenharmony_ci 869e41f4b71Sopenharmony_ci```js 870e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 871e41f4b71Sopenharmony_ci 872e41f4b71Sopenharmony_ci//创建soundPool实例 873e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 874e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 875e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 876e41f4b71Sopenharmony_ci rendererFlags: 1 877e41f4b71Sopenharmony_ci} 878e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 879e41f4b71Sopenharmony_ci if (error) { 880e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 881e41f4b71Sopenharmony_ci return; 882e41f4b71Sopenharmony_ci } else { 883e41f4b71Sopenharmony_ci soundPool = soundPool_; 884e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 885e41f4b71Sopenharmony_ci let streamID: number = 0; 886e41f4b71Sopenharmony_ci // 先调用play方法获取到对应资源的streamID 887e41f4b71Sopenharmony_ci // 给对应的streamID资源设置优先级为1 888e41f4b71Sopenharmony_ci soundPool.setPriority(streamID, 1, (error: BusinessError) => { 889e41f4b71Sopenharmony_ci if (error) { 890e41f4b71Sopenharmony_ci console.error(`Failed to setPriority soundPool: errCode is ${error.code}, errMessage is ${error.message}`) 891e41f4b71Sopenharmony_ci } else { 892e41f4b71Sopenharmony_ci console.info('Succeeded in setPriority soundpool, streamID:' + streamID); 893e41f4b71Sopenharmony_ci } 894e41f4b71Sopenharmony_ci }); 895e41f4b71Sopenharmony_ci } 896e41f4b71Sopenharmony_ci}); 897e41f4b71Sopenharmony_ci 898e41f4b71Sopenharmony_ci``` 899e41f4b71Sopenharmony_ci 900e41f4b71Sopenharmony_ci### setPriority 901e41f4b71Sopenharmony_ci 902e41f4b71Sopenharmony_cisetPriority(streamID: number, priority: number): Promise\<void> 903e41f4b71Sopenharmony_ci 904e41f4b71Sopenharmony_ci设置音频流优先级。使用Promise方式异步获取返回值。 905e41f4b71Sopenharmony_ci 906e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 907e41f4b71Sopenharmony_ci 908e41f4b71Sopenharmony_ci**参数:** 909e41f4b71Sopenharmony_ci 910e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 911e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 912e41f4b71Sopenharmony_ci| streamID | number | 是 | 音频流ID,通过play方法获取。 | 913e41f4b71Sopenharmony_ci| priority | number | 是 | 优先级,0表示最低优先级。 | 914e41f4b71Sopenharmony_ci 915e41f4b71Sopenharmony_ci**返回值:** 916e41f4b71Sopenharmony_ci 917e41f4b71Sopenharmony_ci| 类型 | 说明 | 918e41f4b71Sopenharmony_ci| ---------------- | -------------------------------- | 919e41f4b71Sopenharmony_ci| Promise\<void> | 异步音频池setPriority的Promise返回值。 | 920e41f4b71Sopenharmony_ci 921e41f4b71Sopenharmony_ci**错误码:** 922e41f4b71Sopenharmony_ci 923e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 924e41f4b71Sopenharmony_ci 925e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 926e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 927e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. | 928e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by promise. | 929e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by promise. | 930e41f4b71Sopenharmony_ci 931e41f4b71Sopenharmony_ci**示例:** 932e41f4b71Sopenharmony_ci 933e41f4b71Sopenharmony_ci```js 934e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 935e41f4b71Sopenharmony_ci 936e41f4b71Sopenharmony_ci//创建soundPool实例 937e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 938e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 939e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 940e41f4b71Sopenharmony_ci rendererFlags: 1 941e41f4b71Sopenharmony_ci} 942e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 943e41f4b71Sopenharmony_ci if (error) { 944e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 945e41f4b71Sopenharmony_ci return; 946e41f4b71Sopenharmony_ci } else { 947e41f4b71Sopenharmony_ci soundPool = soundPool_; 948e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 949e41f4b71Sopenharmony_ci let streamID: number = 0; 950e41f4b71Sopenharmony_ci // 先调用play方法获取到对应资源的streamID 951e41f4b71Sopenharmony_ci // 给对应的streamID资源设置优先级为1 952e41f4b71Sopenharmony_ci 953e41f4b71Sopenharmony_ci soundPool.setPriority(streamID, 1).then(() => { 954e41f4b71Sopenharmony_ci console.info('Succeeded in setPriority soundpool'); 955e41f4b71Sopenharmony_ci }, (err: BusinessError) => { 956e41f4b71Sopenharmony_ci console.error('Failed to setPriority soundPool and catch error is ' + err.message); 957e41f4b71Sopenharmony_ci }); 958e41f4b71Sopenharmony_ci } 959e41f4b71Sopenharmony_ci}); 960e41f4b71Sopenharmony_ci 961e41f4b71Sopenharmony_ci``` 962e41f4b71Sopenharmony_ci 963e41f4b71Sopenharmony_ci### setRate 964e41f4b71Sopenharmony_ci 965e41f4b71Sopenharmony_cisetRate(streamID: number, rate: audio.AudioRendererRate, callback: AsyncCallback\<void>): void 966e41f4b71Sopenharmony_ci 967e41f4b71Sopenharmony_ci设置音频流播放速率。使用callback方式异步获取返回值。 968e41f4b71Sopenharmony_ci 969e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 970e41f4b71Sopenharmony_ci 971e41f4b71Sopenharmony_ci**参数:** 972e41f4b71Sopenharmony_ci 973e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 974e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 975e41f4b71Sopenharmony_ci| streamID | number | 是 | 音频流ID,通过play方法获取。 | 976e41f4b71Sopenharmony_ci| rate | [audio.AudioRendererRate](../apis-audio-kit/js-apis-audio.md#audiorendererrate8) | 是 | 音频rate相关参数。 | 977e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 异步音频池setRate方法的回调方法。 | 978e41f4b71Sopenharmony_ci 979e41f4b71Sopenharmony_ci**错误码:** 980e41f4b71Sopenharmony_ci 981e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 982e41f4b71Sopenharmony_ci 983e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 984e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 985e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 986e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by callback. | 987e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by callback. | 988e41f4b71Sopenharmony_ci 989e41f4b71Sopenharmony_ci**示例:** 990e41f4b71Sopenharmony_ci 991e41f4b71Sopenharmony_ci```js 992e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 993e41f4b71Sopenharmony_ci 994e41f4b71Sopenharmony_ci//创建soundPool实例 995e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 996e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 997e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 998e41f4b71Sopenharmony_ci rendererFlags: 1 999e41f4b71Sopenharmony_ci} 1000e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1001e41f4b71Sopenharmony_ci if (error) { 1002e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1003e41f4b71Sopenharmony_ci return; 1004e41f4b71Sopenharmony_ci } else { 1005e41f4b71Sopenharmony_ci soundPool = soundPool_; 1006e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1007e41f4b71Sopenharmony_ci let streamID: number = 0; 1008e41f4b71Sopenharmony_ci let selectedAudioRendererRate: audio.AudioRendererRate = audio.AudioRendererRate.RENDER_RATE_NORMAL; // 默认正常速率 1009e41f4b71Sopenharmony_ci // 先调用play方法获取到对应资源的streamID 1010e41f4b71Sopenharmony_ci soundPool.setRate(streamID, selectedAudioRendererRate, (error: BusinessError) => { 1011e41f4b71Sopenharmony_ci if (error) { 1012e41f4b71Sopenharmony_ci console.error(`Failed to setRate soundPool: errCode is ${error.code}, errMessage is ${error.message}`) 1013e41f4b71Sopenharmony_ci } else { 1014e41f4b71Sopenharmony_ci console.info('Succeeded in setRate success, streamID:' + streamID); 1015e41f4b71Sopenharmony_ci } 1016e41f4b71Sopenharmony_ci }) 1017e41f4b71Sopenharmony_ci } 1018e41f4b71Sopenharmony_ci}); 1019e41f4b71Sopenharmony_ci 1020e41f4b71Sopenharmony_ci``` 1021e41f4b71Sopenharmony_ci 1022e41f4b71Sopenharmony_ci### setRate 1023e41f4b71Sopenharmony_ci 1024e41f4b71Sopenharmony_cisetRate(streamID: number, rate: audio.AudioRendererRate): Promise\<void> 1025e41f4b71Sopenharmony_ci 1026e41f4b71Sopenharmony_ci设置音频流的播放速率。使用Promise方式异步获取返回值。 1027e41f4b71Sopenharmony_ci 1028e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1029e41f4b71Sopenharmony_ci 1030e41f4b71Sopenharmony_ci**参数:** 1031e41f4b71Sopenharmony_ci 1032e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1033e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 1034e41f4b71Sopenharmony_ci| streamID | number | 是 | 音频流ID,通过play方法获取。 | 1035e41f4b71Sopenharmony_ci| rate | [audio.AudioRendererRate](../apis-audio-kit/js-apis-audio.md#audiorendererrate8) | 是 | 音频rate相关参数。 | 1036e41f4b71Sopenharmony_ci 1037e41f4b71Sopenharmony_ci**返回值:** 1038e41f4b71Sopenharmony_ci 1039e41f4b71Sopenharmony_ci| 类型 | 说明 | 1040e41f4b71Sopenharmony_ci| ---------------- | -------------------------------- | 1041e41f4b71Sopenharmony_ci| Promise\<void> | 异步音频池setRate方法的Promise返回值。 | 1042e41f4b71Sopenharmony_ci 1043e41f4b71Sopenharmony_ci**错误码:** 1044e41f4b71Sopenharmony_ci 1045e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1046e41f4b71Sopenharmony_ci 1047e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1048e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 1049e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. | 1050e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by promise. | 1051e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by promise. | 1052e41f4b71Sopenharmony_ci 1053e41f4b71Sopenharmony_ci**示例:** 1054e41f4b71Sopenharmony_ci 1055e41f4b71Sopenharmony_ci```js 1056e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1057e41f4b71Sopenharmony_ci 1058e41f4b71Sopenharmony_ci//创建soundPool实例 1059e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1060e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1061e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1062e41f4b71Sopenharmony_ci rendererFlags: 1 1063e41f4b71Sopenharmony_ci} 1064e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1065e41f4b71Sopenharmony_ci if (error) { 1066e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1067e41f4b71Sopenharmony_ci return; 1068e41f4b71Sopenharmony_ci } else { 1069e41f4b71Sopenharmony_ci soundPool = soundPool_; 1070e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1071e41f4b71Sopenharmony_ci let streamID: number = 0; 1072e41f4b71Sopenharmony_ci let selectedAudioRendererRate: audio.AudioRendererRate = audio.AudioRendererRate.RENDER_RATE_NORMAL; // 默认正常速率 1073e41f4b71Sopenharmony_ci // 先调用play方法获取到对应资源的streamID 1074e41f4b71Sopenharmony_ci soundPool.setRate(streamID, selectedAudioRendererRate).then(() => { 1075e41f4b71Sopenharmony_ci console.info('Succeeded in setRate soundpool'); 1076e41f4b71Sopenharmony_ci }, (err: BusinessError) => { 1077e41f4b71Sopenharmony_ci console.error('Failed to setRate soundpool and catch error is ' + err.message); 1078e41f4b71Sopenharmony_ci }); 1079e41f4b71Sopenharmony_ci } 1080e41f4b71Sopenharmony_ci}); 1081e41f4b71Sopenharmony_ci 1082e41f4b71Sopenharmony_ci``` 1083e41f4b71Sopenharmony_ci 1084e41f4b71Sopenharmony_ci### setVolume 1085e41f4b71Sopenharmony_ci 1086e41f4b71Sopenharmony_cisetVolume(streamID: number, leftVolume: number, rightVolume: number, callback: AsyncCallback\<void>): void 1087e41f4b71Sopenharmony_ci 1088e41f4b71Sopenharmony_ci设置音频流播放音量。使用callback方式异步获取返回值。 1089e41f4b71Sopenharmony_ci 1090e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1091e41f4b71Sopenharmony_ci 1092e41f4b71Sopenharmony_ci**参数:** 1093e41f4b71Sopenharmony_ci 1094e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1095e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 1096e41f4b71Sopenharmony_ci| streamID | number | 是 | 音频流ID,通过play方法获取。 | 1097e41f4b71Sopenharmony_ci| leftVolume | number | 是 | 左声道音量,设置范围为0.0-1.0之间。 | 1098e41f4b71Sopenharmony_ci| rightVolume | number | 是 | 右声道音量,当前右声道设置无效,以左声道为准。 | 1099e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 异步音频池setVolume方法的回调方法。 | 1100e41f4b71Sopenharmony_ci 1101e41f4b71Sopenharmony_ci**错误码:** 1102e41f4b71Sopenharmony_ci 1103e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1104e41f4b71Sopenharmony_ci 1105e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1106e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 1107e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. | 1108e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by callback. | 1109e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by callback. | 1110e41f4b71Sopenharmony_ci 1111e41f4b71Sopenharmony_ci**示例:** 1112e41f4b71Sopenharmony_ci 1113e41f4b71Sopenharmony_ci```js 1114e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1115e41f4b71Sopenharmony_ci 1116e41f4b71Sopenharmony_ci//创建soundPool实例 1117e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1118e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1119e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1120e41f4b71Sopenharmony_ci rendererFlags: 1 1121e41f4b71Sopenharmony_ci} 1122e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1123e41f4b71Sopenharmony_ci if (error) { 1124e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1125e41f4b71Sopenharmony_ci return; 1126e41f4b71Sopenharmony_ci } else { 1127e41f4b71Sopenharmony_ci soundPool = soundPool_; 1128e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1129e41f4b71Sopenharmony_ci let streamID: number = 0; 1130e41f4b71Sopenharmony_ci // 先调用play方法获取到对应资源的streamID 1131e41f4b71Sopenharmony_ci //设置音量为0.5 1132e41f4b71Sopenharmony_ci soundPool.setVolume(streamID, 0.5, 0.5, (error: BusinessError) => { 1133e41f4b71Sopenharmony_ci if (error) { 1134e41f4b71Sopenharmony_ci console.error(`Failed to setVolume soundPool: errCode is ${error.code}, errMessage is ${error.message}`) 1135e41f4b71Sopenharmony_ci } else { 1136e41f4b71Sopenharmony_ci console.info('Succeeded in setVolume soundpool, streamID:' + streamID); 1137e41f4b71Sopenharmony_ci } 1138e41f4b71Sopenharmony_ci }) 1139e41f4b71Sopenharmony_ci } 1140e41f4b71Sopenharmony_ci}); 1141e41f4b71Sopenharmony_ci 1142e41f4b71Sopenharmony_ci``` 1143e41f4b71Sopenharmony_ci 1144e41f4b71Sopenharmony_ci### setVolume 1145e41f4b71Sopenharmony_ci 1146e41f4b71Sopenharmony_cisetVolume(streamID: number, leftVolume: number, rightVolume: number): Promise\<void> 1147e41f4b71Sopenharmony_ci 1148e41f4b71Sopenharmony_ci设置音频流的播放音量。使用Promise方式异步获取返回值。 1149e41f4b71Sopenharmony_ci 1150e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1151e41f4b71Sopenharmony_ci 1152e41f4b71Sopenharmony_ci**参数:** 1153e41f4b71Sopenharmony_ci 1154e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1155e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 1156e41f4b71Sopenharmony_ci| streamID | number | 是 | 音频流ID,通过play方法获取。 | 1157e41f4b71Sopenharmony_ci| leftVolume | number | 是 | 左声道音量,设置范围为0.0-1.0之间。 | 1158e41f4b71Sopenharmony_ci| rightVolume | number | 是 | 右声道音量,当前右声道设置无效,以左声道为准。 | 1159e41f4b71Sopenharmony_ci 1160e41f4b71Sopenharmony_ci**返回值:** 1161e41f4b71Sopenharmony_ci 1162e41f4b71Sopenharmony_ci| 类型 | 说明 | 1163e41f4b71Sopenharmony_ci| ---------------- | -------------------------------- | 1164e41f4b71Sopenharmony_ci| Promise\<void> | 异步音频池setVolume方法的Promise返回值。 | 1165e41f4b71Sopenharmony_ci 1166e41f4b71Sopenharmony_ci**错误码:** 1167e41f4b71Sopenharmony_ci 1168e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1169e41f4b71Sopenharmony_ci 1170e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1171e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 1172e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. | 1173e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by promise. | 1174e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by promise. | 1175e41f4b71Sopenharmony_ci 1176e41f4b71Sopenharmony_ci**示例:** 1177e41f4b71Sopenharmony_ci 1178e41f4b71Sopenharmony_ci```js 1179e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1180e41f4b71Sopenharmony_ci 1181e41f4b71Sopenharmony_ci//创建soundPool实例 1182e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1183e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1184e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1185e41f4b71Sopenharmony_ci rendererFlags: 1 1186e41f4b71Sopenharmony_ci} 1187e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1188e41f4b71Sopenharmony_ci if (error) { 1189e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1190e41f4b71Sopenharmony_ci return; 1191e41f4b71Sopenharmony_ci } else { 1192e41f4b71Sopenharmony_ci soundPool = soundPool_; 1193e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1194e41f4b71Sopenharmony_ci let streamID: number = 0; 1195e41f4b71Sopenharmony_ci // 先调用play方法获取到对应资源的streamID 1196e41f4b71Sopenharmony_ci 1197e41f4b71Sopenharmony_ci soundPool.setVolume(streamID, 0.5, 0.5).then(() => { 1198e41f4b71Sopenharmony_ci console.info('Succeeded in setVolume soundpool'); 1199e41f4b71Sopenharmony_ci }, (err: BusinessError) => { 1200e41f4b71Sopenharmony_ci console.error('Failed to setVolume soundPool and catch error is ' + err.message); 1201e41f4b71Sopenharmony_ci }); 1202e41f4b71Sopenharmony_ci } 1203e41f4b71Sopenharmony_ci}); 1204e41f4b71Sopenharmony_ci 1205e41f4b71Sopenharmony_ci``` 1206e41f4b71Sopenharmony_ci 1207e41f4b71Sopenharmony_ci### unload 1208e41f4b71Sopenharmony_ci 1209e41f4b71Sopenharmony_ciunload(soundID: number, callback: AsyncCallback\<void>): void 1210e41f4b71Sopenharmony_ci 1211e41f4b71Sopenharmony_ci卸载音频资源。使用callback方式异步获取返回值。 1212e41f4b71Sopenharmony_ci 1213e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1214e41f4b71Sopenharmony_ci 1215e41f4b71Sopenharmony_ci**参数:** 1216e41f4b71Sopenharmony_ci 1217e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1218e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 1219e41f4b71Sopenharmony_ci| soundID | number | 是 | 资源ID,通过load方法获取。 | 1220e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 异步音频池unload方法的回调方法。 | 1221e41f4b71Sopenharmony_ci 1222e41f4b71Sopenharmony_ci**错误码:** 1223e41f4b71Sopenharmony_ci 1224e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1225e41f4b71Sopenharmony_ci 1226e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1227e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 1228e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by callback. | 1229e41f4b71Sopenharmony_ci| 5400103 | I/O error. Return by callback. | 1230e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by callback. | 1231e41f4b71Sopenharmony_ci 1232e41f4b71Sopenharmony_ci**示例:** 1233e41f4b71Sopenharmony_ci 1234e41f4b71Sopenharmony_ci```js 1235e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1236e41f4b71Sopenharmony_ci 1237e41f4b71Sopenharmony_ci//创建soundPool实例 1238e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1239e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1240e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1241e41f4b71Sopenharmony_ci rendererFlags: 1 1242e41f4b71Sopenharmony_ci} 1243e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1244e41f4b71Sopenharmony_ci if (error) { 1245e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1246e41f4b71Sopenharmony_ci return; 1247e41f4b71Sopenharmony_ci } else { 1248e41f4b71Sopenharmony_ci soundPool = soundPool_; 1249e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1250e41f4b71Sopenharmony_ci let soundID: number = 0; 1251e41f4b71Sopenharmony_ci // 先调用load方法获取到对应资源的soundID 1252e41f4b71Sopenharmony_ci soundPool.unload(soundID, (error: BusinessError) => { 1253e41f4b71Sopenharmony_ci if (error) { 1254e41f4b71Sopenharmony_ci console.error(`Failed to unload soundPool: errCode is ${error.code}, errMessage is ${error.message}`) 1255e41f4b71Sopenharmony_ci } else { 1256e41f4b71Sopenharmony_ci console.info('Succceeded in unload soundPool'); 1257e41f4b71Sopenharmony_ci } 1258e41f4b71Sopenharmony_ci }) 1259e41f4b71Sopenharmony_ci } 1260e41f4b71Sopenharmony_ci}); 1261e41f4b71Sopenharmony_ci 1262e41f4b71Sopenharmony_ci``` 1263e41f4b71Sopenharmony_ci 1264e41f4b71Sopenharmony_ci### unload 1265e41f4b71Sopenharmony_ci 1266e41f4b71Sopenharmony_ciunload(soundID: number): Promise\<void> 1267e41f4b71Sopenharmony_ci 1268e41f4b71Sopenharmony_ci卸载音频资源。使用Promise方式异步获取返回值。 1269e41f4b71Sopenharmony_ci 1270e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1271e41f4b71Sopenharmony_ci 1272e41f4b71Sopenharmony_ci**参数:** 1273e41f4b71Sopenharmony_ci 1274e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1275e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 1276e41f4b71Sopenharmony_ci| soundID | number | 是 | 资源ID,通过load方法获取。 | 1277e41f4b71Sopenharmony_ci 1278e41f4b71Sopenharmony_ci**返回值:** 1279e41f4b71Sopenharmony_ci 1280e41f4b71Sopenharmony_ci| 类型 | 说明 | 1281e41f4b71Sopenharmony_ci| ---------------- | -------------------------------- | 1282e41f4b71Sopenharmony_ci| Promise\<void> | 异步音频池unload方法的Promise返回值。 | 1283e41f4b71Sopenharmony_ci 1284e41f4b71Sopenharmony_ci**错误码:** 1285e41f4b71Sopenharmony_ci 1286e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1287e41f4b71Sopenharmony_ci 1288e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1289e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 1290e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. Return by promise. | 1291e41f4b71Sopenharmony_ci| 5400103 | I/O error. Return by promise. | 1292e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by promise. | 1293e41f4b71Sopenharmony_ci 1294e41f4b71Sopenharmony_ci**示例:** 1295e41f4b71Sopenharmony_ci 1296e41f4b71Sopenharmony_ci```js 1297e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1298e41f4b71Sopenharmony_ci 1299e41f4b71Sopenharmony_ci//创建soundPool实例 1300e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1301e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1302e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1303e41f4b71Sopenharmony_ci rendererFlags: 1 1304e41f4b71Sopenharmony_ci} 1305e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1306e41f4b71Sopenharmony_ci if (error) { 1307e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1308e41f4b71Sopenharmony_ci return; 1309e41f4b71Sopenharmony_ci } else { 1310e41f4b71Sopenharmony_ci soundPool = soundPool_; 1311e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1312e41f4b71Sopenharmony_ci let soundID: number = 0; 1313e41f4b71Sopenharmony_ci // 先调用load方法获取到对应资源的soundID 1314e41f4b71Sopenharmony_ci 1315e41f4b71Sopenharmony_ci soundPool.unload(soundID).then(() => { 1316e41f4b71Sopenharmony_ci console.info('Succceeded in unload soundPool'); 1317e41f4b71Sopenharmony_ci }, (err: BusinessError) => { 1318e41f4b71Sopenharmony_ci console.error('Failed to unload soundPool and catch error is ' + err.message); 1319e41f4b71Sopenharmony_ci }); 1320e41f4b71Sopenharmony_ci } 1321e41f4b71Sopenharmony_ci}); 1322e41f4b71Sopenharmony_ci 1323e41f4b71Sopenharmony_ci``` 1324e41f4b71Sopenharmony_ci 1325e41f4b71Sopenharmony_ci### release 1326e41f4b71Sopenharmony_ci 1327e41f4b71Sopenharmony_cirelease(callback: AsyncCallback\<void>): void 1328e41f4b71Sopenharmony_ci 1329e41f4b71Sopenharmony_ci释放音频池实例。使用callback方式异步获取返回值。 1330e41f4b71Sopenharmony_ci 1331e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1332e41f4b71Sopenharmony_ci 1333e41f4b71Sopenharmony_ci**参数:** 1334e41f4b71Sopenharmony_ci 1335e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1336e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------- | 1337e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 异步音频池release方法的回调方法。 | 1338e41f4b71Sopenharmony_ci 1339e41f4b71Sopenharmony_ci**错误码:** 1340e41f4b71Sopenharmony_ci 1341e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1342e41f4b71Sopenharmony_ci 1343e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1344e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 1345e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by callback. | 1346e41f4b71Sopenharmony_ci 1347e41f4b71Sopenharmony_ci**示例:** 1348e41f4b71Sopenharmony_ci 1349e41f4b71Sopenharmony_ci```js 1350e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1351e41f4b71Sopenharmony_ci 1352e41f4b71Sopenharmony_ci//创建soundPool实例 1353e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1354e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1355e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1356e41f4b71Sopenharmony_ci rendererFlags: 1 1357e41f4b71Sopenharmony_ci} 1358e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1359e41f4b71Sopenharmony_ci if (error) { 1360e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1361e41f4b71Sopenharmony_ci return; 1362e41f4b71Sopenharmony_ci } else { 1363e41f4b71Sopenharmony_ci soundPool = soundPool_; 1364e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1365e41f4b71Sopenharmony_ci soundPool.release((error: BusinessError) => { 1366e41f4b71Sopenharmony_ci if (error) { 1367e41f4b71Sopenharmony_ci console.error(`Failed to release soundPool: errCode is ${error.code}, errMessage is ${error.message}`) 1368e41f4b71Sopenharmony_ci } else { 1369e41f4b71Sopenharmony_ci console.info('Succeeded in releasing soundPool'); 1370e41f4b71Sopenharmony_ci } 1371e41f4b71Sopenharmony_ci }) 1372e41f4b71Sopenharmony_ci } 1373e41f4b71Sopenharmony_ci}); 1374e41f4b71Sopenharmony_ci 1375e41f4b71Sopenharmony_ci 1376e41f4b71Sopenharmony_ci``` 1377e41f4b71Sopenharmony_ci 1378e41f4b71Sopenharmony_ci### release 1379e41f4b71Sopenharmony_ci 1380e41f4b71Sopenharmony_cirelease(): Promise\<void> 1381e41f4b71Sopenharmony_ci 1382e41f4b71Sopenharmony_ci释放音频池实例。使用Promise方式异步获取返回值。 1383e41f4b71Sopenharmony_ci 1384e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1385e41f4b71Sopenharmony_ci 1386e41f4b71Sopenharmony_ci**返回值:** 1387e41f4b71Sopenharmony_ci 1388e41f4b71Sopenharmony_ci| 类型 | 说明 | 1389e41f4b71Sopenharmony_ci| ---------------- | -------------------------------- | 1390e41f4b71Sopenharmony_ci| Promise\<void> | 异步音频池release方法的Promise返回值。 | 1391e41f4b71Sopenharmony_ci 1392e41f4b71Sopenharmony_ci**错误码:** 1393e41f4b71Sopenharmony_ci 1394e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体错误码](errorcode-media.md)。 1395e41f4b71Sopenharmony_ci 1396e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1397e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | 1398e41f4b71Sopenharmony_ci| 5400105 | Service died. Return by promise. | 1399e41f4b71Sopenharmony_ci 1400e41f4b71Sopenharmony_ci**示例:** 1401e41f4b71Sopenharmony_ci 1402e41f4b71Sopenharmony_ci```js 1403e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1404e41f4b71Sopenharmony_ci 1405e41f4b71Sopenharmony_ci//创建soundPool实例 1406e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1407e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1408e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1409e41f4b71Sopenharmony_ci rendererFlags: 1 1410e41f4b71Sopenharmony_ci} 1411e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1412e41f4b71Sopenharmony_ci if (error) { 1413e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1414e41f4b71Sopenharmony_ci return; 1415e41f4b71Sopenharmony_ci } else { 1416e41f4b71Sopenharmony_ci soundPool = soundPool_; 1417e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1418e41f4b71Sopenharmony_ci soundPool.release().then(() => { 1419e41f4b71Sopenharmony_ci console.info('Succeeded in releasing soundPool'); 1420e41f4b71Sopenharmony_ci }, (err: BusinessError) => { 1421e41f4b71Sopenharmony_ci console.error('Failed to release soundPool and catch error is ' + err.message); 1422e41f4b71Sopenharmony_ci }); 1423e41f4b71Sopenharmony_ci } 1424e41f4b71Sopenharmony_ci}); 1425e41f4b71Sopenharmony_ci 1426e41f4b71Sopenharmony_ci``` 1427e41f4b71Sopenharmony_ci 1428e41f4b71Sopenharmony_ci### on('loadComplete') 1429e41f4b71Sopenharmony_ci 1430e41f4b71Sopenharmony_cion(type: 'loadComplete', callback: Callback\<number>): void 1431e41f4b71Sopenharmony_ci 1432e41f4b71Sopenharmony_ci音频池资源加载完成监听。 1433e41f4b71Sopenharmony_ci 1434e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1435e41f4b71Sopenharmony_ci 1436e41f4b71Sopenharmony_ci**参数:** 1437e41f4b71Sopenharmony_ci 1438e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1439e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ------------------------------------------------------------ | 1440e41f4b71Sopenharmony_ci| type | string | 是 | 支持的事件:'loadComplete',对应的ID加载完成会触发此回调。 | 1441e41f4b71Sopenharmony_ci| callback | Callback\<number> | 是 | 对应资源加载完成的资源ID。 | 1442e41f4b71Sopenharmony_ci 1443e41f4b71Sopenharmony_ci**示例:** 1444e41f4b71Sopenharmony_ci 1445e41f4b71Sopenharmony_ci```js 1446e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1447e41f4b71Sopenharmony_ci 1448e41f4b71Sopenharmony_ci//创建soundPool实例 1449e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1450e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1451e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1452e41f4b71Sopenharmony_ci rendererFlags: 1 1453e41f4b71Sopenharmony_ci} 1454e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1455e41f4b71Sopenharmony_ci if (error) { 1456e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1457e41f4b71Sopenharmony_ci return; 1458e41f4b71Sopenharmony_ci } else { 1459e41f4b71Sopenharmony_ci soundPool = soundPool_; 1460e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1461e41f4b71Sopenharmony_ci soundPool.on('loadComplete', (soundId: number) => { 1462e41f4b71Sopenharmony_ci console.info('Succeeded in loadComplete, soundId:' + soundId) 1463e41f4b71Sopenharmony_ci }) 1464e41f4b71Sopenharmony_ci } 1465e41f4b71Sopenharmony_ci}); 1466e41f4b71Sopenharmony_ci 1467e41f4b71Sopenharmony_ci``` 1468e41f4b71Sopenharmony_ci 1469e41f4b71Sopenharmony_ci### off('loadComplete') 1470e41f4b71Sopenharmony_ci 1471e41f4b71Sopenharmony_cioff(type: 'loadComplete'): void 1472e41f4b71Sopenharmony_ci 1473e41f4b71Sopenharmony_ci取消监听资源的加载完成。 1474e41f4b71Sopenharmony_ci 1475e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1476e41f4b71Sopenharmony_ci 1477e41f4b71Sopenharmony_ci**参数:** 1478e41f4b71Sopenharmony_ci 1479e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1480e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 1481e41f4b71Sopenharmony_ci| type | string | 是 | 取消注册的事件:'loadComplete'。 | 1482e41f4b71Sopenharmony_ci 1483e41f4b71Sopenharmony_ci**示例:** 1484e41f4b71Sopenharmony_ci 1485e41f4b71Sopenharmony_ci```js 1486e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1487e41f4b71Sopenharmony_ci 1488e41f4b71Sopenharmony_ci//创建soundPool实例 1489e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1490e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1491e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1492e41f4b71Sopenharmony_ci rendererFlags: 1 1493e41f4b71Sopenharmony_ci} 1494e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1495e41f4b71Sopenharmony_ci if (error) { 1496e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1497e41f4b71Sopenharmony_ci return; 1498e41f4b71Sopenharmony_ci } else { 1499e41f4b71Sopenharmony_ci soundPool = soundPool_; 1500e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1501e41f4b71Sopenharmony_ci soundPool.off('loadComplete') 1502e41f4b71Sopenharmony_ci } 1503e41f4b71Sopenharmony_ci}); 1504e41f4b71Sopenharmony_ci 1505e41f4b71Sopenharmony_ci``` 1506e41f4b71Sopenharmony_ci 1507e41f4b71Sopenharmony_ci### on('playFinished') 1508e41f4b71Sopenharmony_ci 1509e41f4b71Sopenharmony_cion(type: 'playFinished', callback: Callback\<void>): void 1510e41f4b71Sopenharmony_ci 1511e41f4b71Sopenharmony_ci音频池资源播放完成监听。 1512e41f4b71Sopenharmony_ci 1513e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1514e41f4b71Sopenharmony_ci 1515e41f4b71Sopenharmony_ci**参数:** 1516e41f4b71Sopenharmony_ci 1517e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1518e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ------------------------------------------------------------ | 1519e41f4b71Sopenharmony_ci| type | string | 是 | 支持的事件:'playFinished',音频流播放完成会触发此回调。 | 1520e41f4b71Sopenharmony_ci| callback | Callback\<void> | 是 | 异步'playFinished'的回调方法。 | 1521e41f4b71Sopenharmony_ci 1522e41f4b71Sopenharmony_ci**示例:** 1523e41f4b71Sopenharmony_ci 1524e41f4b71Sopenharmony_ci```js 1525e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1526e41f4b71Sopenharmony_ci 1527e41f4b71Sopenharmony_ci//创建soundPool实例 1528e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1529e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1530e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1531e41f4b71Sopenharmony_ci rendererFlags: 1 1532e41f4b71Sopenharmony_ci} 1533e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1534e41f4b71Sopenharmony_ci if (error) { 1535e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1536e41f4b71Sopenharmony_ci return; 1537e41f4b71Sopenharmony_ci } else { 1538e41f4b71Sopenharmony_ci soundPool = soundPool_; 1539e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1540e41f4b71Sopenharmony_ci soundPool.on('playFinished', () => { 1541e41f4b71Sopenharmony_ci console.info('Succeeded in playFinished') 1542e41f4b71Sopenharmony_ci }); 1543e41f4b71Sopenharmony_ci } 1544e41f4b71Sopenharmony_ci}); 1545e41f4b71Sopenharmony_ci 1546e41f4b71Sopenharmony_ci``` 1547e41f4b71Sopenharmony_ci 1548e41f4b71Sopenharmony_ci### off('playFinished') 1549e41f4b71Sopenharmony_ci 1550e41f4b71Sopenharmony_cioff(type: 'playFinished'): void 1551e41f4b71Sopenharmony_ci 1552e41f4b71Sopenharmony_ci取消监听音频池资源播放完成。 1553e41f4b71Sopenharmony_ci 1554e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1555e41f4b71Sopenharmony_ci 1556e41f4b71Sopenharmony_ci**参数:** 1557e41f4b71Sopenharmony_ci 1558e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1559e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 1560e41f4b71Sopenharmony_ci| type | string | 是 | 取消注册的事件:'playFinished'。 | 1561e41f4b71Sopenharmony_ci 1562e41f4b71Sopenharmony_ci**示例:** 1563e41f4b71Sopenharmony_ci 1564e41f4b71Sopenharmony_ci```js 1565e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1566e41f4b71Sopenharmony_ci 1567e41f4b71Sopenharmony_ci//创建soundPool实例 1568e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1569e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1570e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1571e41f4b71Sopenharmony_ci rendererFlags: 1 1572e41f4b71Sopenharmony_ci} 1573e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1574e41f4b71Sopenharmony_ci if (error) { 1575e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1576e41f4b71Sopenharmony_ci return; 1577e41f4b71Sopenharmony_ci } else { 1578e41f4b71Sopenharmony_ci soundPool = soundPool_; 1579e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1580e41f4b71Sopenharmony_ci soundPool.off('playFinished') 1581e41f4b71Sopenharmony_ci } 1582e41f4b71Sopenharmony_ci}); 1583e41f4b71Sopenharmony_ci 1584e41f4b71Sopenharmony_ci``` 1585e41f4b71Sopenharmony_ci 1586e41f4b71Sopenharmony_ci### on('error') 1587e41f4b71Sopenharmony_ci 1588e41f4b71Sopenharmony_cion(type: 'error', callback: ErrorCallback): void 1589e41f4b71Sopenharmony_ci 1590e41f4b71Sopenharmony_ci监听[SoundPool](#soundpool)的错误事件,该事件仅用于错误提示。 1591e41f4b71Sopenharmony_ci 1592e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1593e41f4b71Sopenharmony_ci 1594e41f4b71Sopenharmony_ci**参数:** 1595e41f4b71Sopenharmony_ci 1596e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1597e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ------------------------------------------------------------ | 1598e41f4b71Sopenharmony_ci| type | string | 是 | 错误事件回调类型,支持的事件:'error',用户操作和系统都会触发此事件。 | 1599e41f4b71Sopenharmony_ci| callback | ErrorCallback | 是 | 错误事件回调方法:使用播放器的过程中发生错误,会提供错误码ID和错误信息。 | 1600e41f4b71Sopenharmony_ci 1601e41f4b71Sopenharmony_ciSoundPool回调的**错误分类**<a name = error_info></a>可以分为以下几种: 1602e41f4b71Sopenharmony_ci 1603e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 说明 | 1604e41f4b71Sopenharmony_ci| -------- | --------------------- | ------------------------------------------------------------ | 1605e41f4b71Sopenharmony_ci| 401 | Invalid Parameter. | 入参错误,表示调用无效。 | 1606e41f4b71Sopenharmony_ci| 801 | Unsupport Capability. | 不支持该API能力,表示调用无效。 | 1607e41f4b71Sopenharmony_ci| 5400101 | No Memory. | 内存不足。 | 1608e41f4b71Sopenharmony_ci| 5400102 | Operation Not Allowed. | 当前状态机不支持此操作,表示调用无效。 | 1609e41f4b71Sopenharmony_ci| 5400103 | IO Error. | I/O异常。 | 1610e41f4b71Sopenharmony_ci| 5400105 | Service Died. | 播放进程死亡,音频池依赖的service端发生异常。| 1611e41f4b71Sopenharmony_ci 1612e41f4b71Sopenharmony_ci**示例:** 1613e41f4b71Sopenharmony_ci 1614e41f4b71Sopenharmony_ci```js 1615e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1616e41f4b71Sopenharmony_ci 1617e41f4b71Sopenharmony_ci//创建soundPool实例 1618e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1619e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1620e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1621e41f4b71Sopenharmony_ci rendererFlags: 1 1622e41f4b71Sopenharmony_ci} 1623e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1624e41f4b71Sopenharmony_ci if (error) { 1625e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1626e41f4b71Sopenharmony_ci return; 1627e41f4b71Sopenharmony_ci } else { 1628e41f4b71Sopenharmony_ci soundPool = soundPool_; 1629e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1630e41f4b71Sopenharmony_ci soundPool.on('error', (error: BusinessError) => { 1631e41f4b71Sopenharmony_ci console.error('error happened,and error message is :' + error.message) 1632e41f4b71Sopenharmony_ci console.error('error happened,and error code is :' + error.code) 1633e41f4b71Sopenharmony_ci }) 1634e41f4b71Sopenharmony_ci } 1635e41f4b71Sopenharmony_ci}); 1636e41f4b71Sopenharmony_ci 1637e41f4b71Sopenharmony_ci``` 1638e41f4b71Sopenharmony_ci 1639e41f4b71Sopenharmony_ci### off('error') 1640e41f4b71Sopenharmony_ci 1641e41f4b71Sopenharmony_cioff(type: 'error'): void 1642e41f4b71Sopenharmony_ci 1643e41f4b71Sopenharmony_ci取消监听音频池的错误事件。 1644e41f4b71Sopenharmony_ci 1645e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.Media.SoundPool 1646e41f4b71Sopenharmony_ci 1647e41f4b71Sopenharmony_ci**参数:** 1648e41f4b71Sopenharmony_ci 1649e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1650e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ----------------------------------------- | 1651e41f4b71Sopenharmony_ci| type | string | 是 | 错误事件回调类型,取消注册的事件:'error'。 | 1652e41f4b71Sopenharmony_ci 1653e41f4b71Sopenharmony_ci**示例:** 1654e41f4b71Sopenharmony_ci 1655e41f4b71Sopenharmony_ci```js 1656e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1657e41f4b71Sopenharmony_ci 1658e41f4b71Sopenharmony_ci//创建soundPool实例 1659e41f4b71Sopenharmony_cilet soundPool: media.SoundPool; 1660e41f4b71Sopenharmony_cilet audioRendererInfo: audio.AudioRendererInfo = { 1661e41f4b71Sopenharmony_ci usage: audio.StreamUsage.STREAM_USAGE_MUSIC, 1662e41f4b71Sopenharmony_ci rendererFlags: 1 1663e41f4b71Sopenharmony_ci} 1664e41f4b71Sopenharmony_cimedia.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => { 1665e41f4b71Sopenharmony_ci if (error) { 1666e41f4b71Sopenharmony_ci console.error(`Failed to createSoundPool`) 1667e41f4b71Sopenharmony_ci return; 1668e41f4b71Sopenharmony_ci } else { 1669e41f4b71Sopenharmony_ci soundPool = soundPool_; 1670e41f4b71Sopenharmony_ci console.info(`Succeeded in createSoundPool`) 1671e41f4b71Sopenharmony_ci soundPool.off('error') 1672e41f4b71Sopenharmony_ci } 1673e41f4b71Sopenharmony_ci}); 1674e41f4b71Sopenharmony_ci``` 1675