1e41f4b71Sopenharmony_ci# @ohos.multimedia.audioHaptic (音振协同) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci音振协同,表示在播放声音时,可同步发起振动。可用于来电通知、消息提醒等场景。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 导入模块 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci```ts 13e41f4b71Sopenharmony_ciimport { audioHaptic } from '@kit.AudioKit'; 14e41f4b71Sopenharmony_ci``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## audioHaptic.getAudioHapticManager 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_cigetAudioHapticManager(): AudioHapticManager 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci获取音振管理器。 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**返回值:** 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci| 类型 | 说明 | 27e41f4b71Sopenharmony_ci| ----------------------------- | ------------ | 28e41f4b71Sopenharmony_ci| [AudioHapticManager](#audiohapticmanager) | 音振管理器。 | 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci**示例:** 31e41f4b71Sopenharmony_ci```ts 32e41f4b71Sopenharmony_cilet audioHapticManagerInstance: audioHaptic.AudioHapticManager = audioHaptic.getAudioHapticManager(); 33e41f4b71Sopenharmony_ci``` 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci## AudioLatencyMode 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci枚举,音频时延模式。 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 42e41f4b71Sopenharmony_ci| ------------------------------- | ------ | -------------------------------------------- | 43e41f4b71Sopenharmony_ci| AUDIO_LATENCY_MODE_NORMAL | 0 | 普通时延模式。 | 44e41f4b71Sopenharmony_ci| AUDIO_LATENCY_MODE_FAST | 1 | 低时延模式。该模式适用于比较短的音频文件,音频文件过长时可能被截断,该特性与[SoundPool](../apis-media-kit/js-apis-inner-multimedia-soundPool.md#soundpool)一致。 | 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci## AudioHapticPlayerOptions 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci音振播放器选项。 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci| 名称 | 类型 |必填 | 说明 | 53e41f4b71Sopenharmony_ci| --------- | -------------- | ---- | --------------------------------- | 54e41f4b71Sopenharmony_ci| muteAudio | boolean | 否 | 是否将音频静音,true表示将音频静音,false表示正常播放声音。若不填该参数,则默认为false。 | 55e41f4b71Sopenharmony_ci| muteHaptics | boolean | 否 | 是否禁止振动,true表示将禁止振动,false表示正常振动。若不填该参数,则默认为false。 | 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci## AudioHapticManager 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci管理音振协同功能。在调用AudioHapticManager的接口前,需要先通过[getAudioHapticManager](#audiohapticgetaudiohapticmanager)创建实例。 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci### registerSource 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ciregisterSource(audioUri: string, hapticUri: string): Promise<number> 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci注册音频和振动资源的Uri,使用Promise方式异步返回结果。 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**参数:** 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 72e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------ | 73e41f4b71Sopenharmony_ci| audioUri | string | 是 | 音频资源的Uri。对普通时延模式,音频资源格式和路径格式的支持可参考[media.AVPlayer](../apis-media-kit/js-apis-media.md#avplayer9);对低时延模式,音频资源格式支持可参考[SoundPool](../apis-media-kit/js-apis-inner-multimedia-soundPool.md#soundpool),路径格式需满足[文件管理模块open函数](../apis-core-file-kit/js-apis-file-fs.md#fsopen)的要求。对两种时延模式,均建议传入文件的绝对路径。 | 74e41f4b71Sopenharmony_ci| hapticUri | string | 是 | 振动资源的Uri。振动资源格式支持可参考[vibrator](../apis-sensor-service-kit/js-apis-vibrator.md#hapticfiledescriptor10),路径格式需满足[文件管理模块open函数](../apis-core-file-kit/js-apis-file-fs.md#fsopen)的要求。建议传入文件的绝对路径。 | 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci**返回值:** 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci| 类型 | 说明 | 79e41f4b71Sopenharmony_ci| ------------------- | ------------------------------- | 80e41f4b71Sopenharmony_ci| Promise<number> | Promise回调返回注册资源的source id。 | 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**错误码:** 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体服务错误码](../apis-media-kit/errorcode-media.md)。 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 87e41f4b71Sopenharmony_ci| ------- |-----------------------------------| 88e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci**示例:** 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci```ts 93e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_cilet audioUri = 'data/audioTest.wav'; // 需更改为目标音频资源的Uri 96e41f4b71Sopenharmony_cilet hapticUri = 'data/hapticTest.json'; // 需更改为目标振动资源的Uri 97e41f4b71Sopenharmony_cilet id = 0; 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ciaudioHapticManagerInstance.registerSource(audioUri, hapticUri).then((value: number) => { 100e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that the source id of the registerd source ${value}.`); 101e41f4b71Sopenharmony_ci id = value; 102e41f4b71Sopenharmony_ci}).catch ((err: BusinessError) => { 103e41f4b71Sopenharmony_ci console.error(`Failed to register source ${err}`); 104e41f4b71Sopenharmony_ci}); 105e41f4b71Sopenharmony_ci``` 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci### unregisterSource 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ciunregisterSource(id: number): Promise<void> 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci取消注册音频和振动资源,使用Promise方式异步返回结果。 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**参数:** 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 118e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------ | 119e41f4b71Sopenharmony_ci| id | number | 是 | 已注册资源的source id。 | 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci**错误码:** 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体服务错误码](../apis-media-kit/errorcode-media.md)。 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 126e41f4b71Sopenharmony_ci| ------- |-----------------------------------| 127e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci**示例:** 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci```ts 132e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_cilet audioUri = 'data/audioTest.wav'; // 需更改为目标音频资源的Uri 135e41f4b71Sopenharmony_cilet hapticUri = 'data/hapticTest.json'; // 需更改为目标振动资源的Uri 136e41f4b71Sopenharmony_cilet id = 0; 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ciaudioHapticManagerInstance.registerSource(audioUri, hapticUri).then((value: number) => { 139e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that the source id of the registerd source ${value}.`); 140e41f4b71Sopenharmony_ci id = value; 141e41f4b71Sopenharmony_ci}).catch ((err: BusinessError) => { 142e41f4b71Sopenharmony_ci console.error(`Failed to register source ${err}`); 143e41f4b71Sopenharmony_ci}); 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ciaudioHapticManagerInstance.unregisterSource(id).then(() => { 146e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that unregister source successfully`); 147e41f4b71Sopenharmony_ci}).catch ((err: BusinessError) => { 148e41f4b71Sopenharmony_ci console.error(`Failed to unregistere source ${err}`); 149e41f4b71Sopenharmony_ci}); 150e41f4b71Sopenharmony_ci``` 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci### setAudioLatencyMode 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_cisetAudioLatencyMode(id:number, latencyMode: AudioLatencyMode): void 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci设置音频时延模式。 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci**参数:** 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 163e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------ | 164e41f4b71Sopenharmony_ci| id | number | 是 | 已注册资源的source id。 | 165e41f4b71Sopenharmony_ci| latencyMode | [AudioLatencyMode](#audiolatencymode) | 是 | 音频时延模式。 | 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci**错误码:** 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体服务错误码](../apis-media-kit/errorcode-media.md)。 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 172e41f4b71Sopenharmony_ci| ------- |-----------------------------------| 173e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 174e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. | 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci**示例:** 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci```ts 179e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_cilet audioUri = 'data/audioTest.wav'; // 需更改为目标音频资源的Uri 182e41f4b71Sopenharmony_cilet hapticUri = 'data/hapticTest.json'; // 需更改为目标振动资源的Uri 183e41f4b71Sopenharmony_cilet id = 0; 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ciaudioHapticManagerInstance.registerSource(audioUri, hapticUri).then((value: number) => { 186e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that the source id of the registerd source ${value}.`); 187e41f4b71Sopenharmony_ci id = value; 188e41f4b71Sopenharmony_ci}).catch ((err: BusinessError) => { 189e41f4b71Sopenharmony_ci console.error(`Failed to register source ${err}`); 190e41f4b71Sopenharmony_ci}); 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_cilet latencyMode: audioHaptic.AudioLatencyMode = audioHaptic.AudioLatencyMode.AUDIO_LATENCY_MODE_FAST; 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ciaudioHapticManagerInstance.setAudioLatencyMode(id, latencyMode); 195e41f4b71Sopenharmony_ci``` 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci### setStreamUsage 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_cisetStreamUsage(id: number, usage: audio.StreamUsage): void 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci设置音频流使用类型。 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci**参数:** 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 208e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------ | 209e41f4b71Sopenharmony_ci| id | number | 是 | 已注册资源的source id。 | 210e41f4b71Sopenharmony_ci| usage | [audio.StreamUsage](js-apis-audio.md#streamusage) | 是 | 音频流使用类型。 | 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci**错误码:** 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体服务错误码](../apis-media-kit/errorcode-media.md)。 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 217e41f4b71Sopenharmony_ci| ------- |-----------------------------------| 218e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 219e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. | 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci**示例:** 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci```ts 224e41f4b71Sopenharmony_ciimport { audio } from '@kit.AudioKit'; 225e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_cilet audioUri = 'data/audioTest.wav'; // 需更改为目标音频资源的Uri 228e41f4b71Sopenharmony_cilet hapticUri = 'data/hapticTest.json'; // 需更改为目标振动资源的Uri 229e41f4b71Sopenharmony_cilet id = 0; 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ciaudioHapticManagerInstance.registerSource(audioUri, hapticUri).then((value: number) => { 232e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that the source id of the registerd source ${value}.`); 233e41f4b71Sopenharmony_ci id = value; 234e41f4b71Sopenharmony_ci}).catch ((err: BusinessError) => { 235e41f4b71Sopenharmony_ci console.error(`Failed to register source ${err}`); 236e41f4b71Sopenharmony_ci}); 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_cilet usage: audio.StreamUsage = audio.StreamUsage.STREAM_USAGE_NOTIFICATION; 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ciaudioHapticManagerInstance.setStreamUsage(id, usage); 241e41f4b71Sopenharmony_ci``` 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci### createPlayer 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_cicreatePlayer(id: number, options?: AudioHapticPlayerOptions): Promise<AudioHapticPlayer> 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci创建音振播放器,使用Promise方式异步返回结果。 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.VIBRATE 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci如果应用创建的AudioHapticPlayer需要触发振动,则需要校验应用是否拥有该权限。 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci**参数:** 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 258e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------ | 259e41f4b71Sopenharmony_ci| id | number | 是 | 已注册资源的source id。 | 260e41f4b71Sopenharmony_ci| options | [AudioHapticPlayerOptions](#audiohapticplayeroptions) | 否 | 音振播放器选项。 | 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci**返回值:** 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci| 类型 | 说明 | 265e41f4b71Sopenharmony_ci| ------------------- | ------------------------------- | 266e41f4b71Sopenharmony_ci| Promise<[AudioHapticPlayer](#audiohapticplayer)> | Promise回调返回创建的音振播放器。 | 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci**错误码:** 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体服务错误码](../apis-media-kit/errorcode-media.md)。 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 273e41f4b71Sopenharmony_ci| ------- |-----------------------------------| 274e41f4b71Sopenharmony_ci| 201 | Permission denied. | 275e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 276e41f4b71Sopenharmony_ci| 5400102 | Operation not allowed. | 277e41f4b71Sopenharmony_ci| 5400103 | I/O error. | 278e41f4b71Sopenharmony_ci| 5400106 | Unsupport format. | 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci**示例:** 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci```ts 283e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_cilet audioUri = 'data/audioTest.wav'; // 需更改为目标音频资源的Uri 286e41f4b71Sopenharmony_cilet hapticUri = 'data/hapticTest.json'; // 需更改为目标振动资源的Uri 287e41f4b71Sopenharmony_cilet id = 0; 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ciaudioHapticManagerInstance.registerSource(audioUri, hapticUri).then((value: number) => { 290e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that the source id of the registerd source ${value}.`); 291e41f4b71Sopenharmony_ci id = value; 292e41f4b71Sopenharmony_ci}).catch ((err: BusinessError) => { 293e41f4b71Sopenharmony_ci console.error(`Failed to register source ${err}`); 294e41f4b71Sopenharmony_ci}); 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_cilet options: audioHaptic.AudioHapticPlayerOptions = {muteAudio: false, muteHaptics: false}; 297e41f4b71Sopenharmony_cilet audioHapticPlayerInstance: audioHaptic.AudioHapticPlayer | undefined = undefined; 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ciaudioHapticManagerInstance.createPlayer(id, options).then((value: audioHaptic.AudioHapticPlayer) => { 300e41f4b71Sopenharmony_ci audioHapticPlayerInstance = value; 301e41f4b71Sopenharmony_ci console.info(`Create the audio haptic player successfully.`); 302e41f4b71Sopenharmony_ci}).catch ((err: BusinessError) => { 303e41f4b71Sopenharmony_ci console.error(`Failed to create the audio haptic player. ${err}`); 304e41f4b71Sopenharmony_ci}); 305e41f4b71Sopenharmony_ci``` 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ci## AudioHapticType 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci枚举,音振类型。 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 314e41f4b71Sopenharmony_ci| ------------------------------- | ------ | -------------------------------------------- | 315e41f4b71Sopenharmony_ci| AUDIO_HAPTIC_TYPE_AUDIO | 0 | 音频。 | 316e41f4b71Sopenharmony_ci| AUDIO_HAPTIC_TYPE_HAPTIC | 1 | 振动。 | 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci## AudioHapticPlayer 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_ci音振播放器,提供音振协同播放功能。在调用AudioHapticPlayer的接口前,需要先通过[createPlayer](#createplayer)创建实例。 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci### isMuted 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ciisMuted(type: AudioHapticType): boolean 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci查询该音振类型是否被静音。 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ci**参数:** 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 333e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------ | 334e41f4b71Sopenharmony_ci| type | [AudioHapticType](#audiohaptictype) | 是 | 音振类型。 | 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci**返回值:** 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci| 类型 | 说明 | 339e41f4b71Sopenharmony_ci| ------------------- | ------------------------------- | 340e41f4b71Sopenharmony_ci| boolean | 查询的音振类型是否被静音。 | 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci**错误码:** 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体服务错误码](../apis-media-kit/errorcode-media.md)。 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 347e41f4b71Sopenharmony_ci| ------- |-----------------------------------| 348e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Parameter verification failed. | 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ci**示例:** 351e41f4b71Sopenharmony_ci 352e41f4b71Sopenharmony_ci```ts 353e41f4b71Sopenharmony_cilet audioHapticType: audioHaptic.AudioHapticType = audioHaptic.AudioHapticType.AUDIO_HAPTIC_TYPE_AUDIO; 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_cilet result: boolean = audioHapticPlayerInstance.isMuted(audioHapticType); 356e41f4b71Sopenharmony_ci``` 357e41f4b71Sopenharmony_ci 358e41f4b71Sopenharmony_ci### start 359e41f4b71Sopenharmony_ci 360e41f4b71Sopenharmony_cistart(): Promise<void> 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_ci开始播放,使用Promise方式异步返回结果。 363e41f4b71Sopenharmony_ci 364e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci**返回值:** 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci| 类型 | 说明 | 369e41f4b71Sopenharmony_ci| ------------------- | -------------------------------- | 370e41f4b71Sopenharmony_ci| Promise<void> | Promise回调返回开始播放成功或失败。 | 371e41f4b71Sopenharmony_ci 372e41f4b71Sopenharmony_ci**错误码:** 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体服务错误码](../apis-media-kit/errorcode-media.md)。 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 377e41f4b71Sopenharmony_ci|---------|-----------------------------------| 378e41f4b71Sopenharmony_ci| 5400102 | Operate not permit. | 379e41f4b71Sopenharmony_ci| 5400103 | IO error. | 380e41f4b71Sopenharmony_ci| 5400105 | Service died. | 381e41f4b71Sopenharmony_ci 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci**示例:** 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci```ts 386e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ciaudioHapticPlayerInstance.start().then(() => { 389e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that start playing successfully.`); 390e41f4b71Sopenharmony_ci}).catch ((err: BusinessError) => { 391e41f4b71Sopenharmony_ci console.error(`Failed to start playing. ${err}`); 392e41f4b71Sopenharmony_ci}); 393e41f4b71Sopenharmony_ci``` 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ci### stop 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_cistop(): Promise<void> 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci停止播放,使用Promise方式异步返回结果。 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ci**返回值:** 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci| 类型 | 说明 | 406e41f4b71Sopenharmony_ci| ------------------- | -------------------------------- | 407e41f4b71Sopenharmony_ci| Promise<void> | Promise回调返回停止播放成功或失败。 | 408e41f4b71Sopenharmony_ci 409e41f4b71Sopenharmony_ci**错误码:** 410e41f4b71Sopenharmony_ci 411e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体服务错误码](../apis-media-kit/errorcode-media.md)。 412e41f4b71Sopenharmony_ci 413e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 414e41f4b71Sopenharmony_ci|---------|-----------------------------------| 415e41f4b71Sopenharmony_ci| 5400102 | Operate not permit. | 416e41f4b71Sopenharmony_ci| 5400105 | Service died. | 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci**示例:** 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci```ts 421e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 422e41f4b71Sopenharmony_ci 423e41f4b71Sopenharmony_ciaudioHapticPlayerInstance.stop().then(() => { 424e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that stop playing successfully.`); 425e41f4b71Sopenharmony_ci}).catch ((err: BusinessError) => { 426e41f4b71Sopenharmony_ci console.error(`Failed to stop playing. ${err}`); 427e41f4b71Sopenharmony_ci}); 428e41f4b71Sopenharmony_ci``` 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci### release 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_cirelease(): Promise<void> 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci释放音振播放器,使用Promise方式异步返回结果。 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci**返回值:** 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci| 类型 | 说明 | 441e41f4b71Sopenharmony_ci| ------------------- | ------------------------------- | 442e41f4b71Sopenharmony_ci| Promise<void> | Promise回调返回释放成功或失败。 | 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci**错误码:** 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[媒体服务错误码](../apis-media-kit/errorcode-media.md)。 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 449e41f4b71Sopenharmony_ci|---------|-----------------------------------| 450e41f4b71Sopenharmony_ci| 5400105 | Service died. | 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci**示例:** 453e41f4b71Sopenharmony_ci 454e41f4b71Sopenharmony_ci```ts 455e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ciaudioHapticPlayerInstance.release().then(() => { 458e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that release the audio haptic player successfully.`); 459e41f4b71Sopenharmony_ci}).catch ((err: BusinessError) => { 460e41f4b71Sopenharmony_ci console.error(`Failed to release the audio haptic player. ${err}`); 461e41f4b71Sopenharmony_ci}); 462e41f4b71Sopenharmony_ci``` 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_ci### on('endOfStream') 465e41f4b71Sopenharmony_ci 466e41f4b71Sopenharmony_cion(type: 'endOfStream', callback: Callback<void>): void 467e41f4b71Sopenharmony_ci 468e41f4b71Sopenharmony_ci监听流结束事件(音频流播放结束时触发),使用callback方式返回结果。 469e41f4b71Sopenharmony_ci 470e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 471e41f4b71Sopenharmony_ci 472e41f4b71Sopenharmony_ci**参数:** 473e41f4b71Sopenharmony_ci 474e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 475e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- | -------------------------------------------------------------------------- | 476e41f4b71Sopenharmony_ci| type | string | 是 | 事件回调类型,支持的事件为:'endOfStream'(流结束事件)。 | 477e41f4b71Sopenharmony_ci| callback | Callback<void> | 是 | 回调函数,无返回结果。 | 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ci**示例:** 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ci```ts 482e41f4b71Sopenharmony_ciaudioHapticPlayerInstance.on('endOfStream', () => { 483e41f4b71Sopenharmony_ci console.info(`Receive the callback of endOfStream.`); 484e41f4b71Sopenharmony_ci}); 485e41f4b71Sopenharmony_ci``` 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ci### off('endOfStream') 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_cioff(type: 'endOfStream', callback?: Callback<void>): void 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ci取消监听流结束事件,使用callback方式返回结果。 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ci**参数:** 496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 498e41f4b71Sopenharmony_ci| ----- | ----- | ---- | ------------------------------------------------ | 499e41f4b71Sopenharmony_ci| type | string | 是 | 要取消订阅事件的类型。支持的事件为:'endOfStream'。 | 500e41f4b71Sopenharmony_ci| callback | Callback<void> | 否 | 回调函数,无返回结果。 | 501e41f4b71Sopenharmony_ci 502e41f4b71Sopenharmony_ci**示例:** 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci```ts 505e41f4b71Sopenharmony_ci// 取消该事件的所有监听 506e41f4b71Sopenharmony_ciaudioHapticPlayerInstance.off('endOfStream'); 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听 509e41f4b71Sopenharmony_cilet endOfStreamCallback = () => { 510e41f4b71Sopenharmony_ci console.info(`Receive the callback of endOfStream.`); 511e41f4b71Sopenharmony_ci}; 512e41f4b71Sopenharmony_ci 513e41f4b71Sopenharmony_ciaudioHapticPlayerInstance.on('endOfStream', endOfStreamCallback); 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ciaudioHapticPlayerInstance.off('endOfStream', endOfStreamCallback); 516e41f4b71Sopenharmony_ci``` 517e41f4b71Sopenharmony_ci 518e41f4b71Sopenharmony_ci### on('audioInterrupt') 519e41f4b71Sopenharmony_ci 520e41f4b71Sopenharmony_cion(type: 'audioInterrupt', callback: Callback<audio.InterruptEvent>): void 521e41f4b71Sopenharmony_ci 522e41f4b71Sopenharmony_ci监听音频中断事件(当音频焦点发生变化时触发),使用callback方式返回结果。 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ci**参数:** 527e41f4b71Sopenharmony_ci 528e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 529e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- | -------------------------------------------------------------------------- | 530e41f4b71Sopenharmony_ci| type | string | 是 | 事件回调类型,支持的事件为:'audioInterrupt'(音频中断事件)。 | 531e41f4b71Sopenharmony_ci| callback | Callback<[audio.InterruptEvent](js-apis-audio.md#interruptevent9)> | 是 | 回调函数,返回播放中断时,应用接收的中断事件信息。 | 532e41f4b71Sopenharmony_ci 533e41f4b71Sopenharmony_ci**示例:** 534e41f4b71Sopenharmony_ci 535e41f4b71Sopenharmony_ci```ts 536e41f4b71Sopenharmony_ciimport { audio } from '@kit.AudioKit'; 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_cilet isPlaying: boolean; // 标识符,表示是否正在渲染 539e41f4b71Sopenharmony_cilet isDucked: boolean; // 标识符,表示是否被降低音量 540e41f4b71Sopenharmony_ci 541e41f4b71Sopenharmony_ciaudioHapticPlayerInstance.on('audioInterrupt', (interruptEvent: audio.InterruptEvent) => { 542e41f4b71Sopenharmony_ci // 在发生音频打断事件时,audioHapticPlayerInstance收到interruptEvent回调,此处根据其内容做相应处理。 543e41f4b71Sopenharmony_ci // 1、可选:读取interruptEvent.forceType的类型,判断系统是否已强制执行相应操作。 544e41f4b71Sopenharmony_ci // 注:默认焦点策略下,INTERRUPT_HINT_RESUME为INTERRUPT_SHARE类型,其余hintType均为INTERRUPT_FORCE类型。因此对forceType可不做判断。 545e41f4b71Sopenharmony_ci // 2、必选:读取interruptEvent.hintType的类型,做出相应的处理。 546e41f4b71Sopenharmony_ci if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 547e41f4b71Sopenharmony_ci // 音频焦点事件已由系统强制执行,应用需更新自身状态及显示内容等 548e41f4b71Sopenharmony_ci switch (interruptEvent.hintType) { 549e41f4b71Sopenharmony_ci case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 550e41f4b71Sopenharmony_ci // 音频流已被暂停,临时失去焦点,待可重获焦点时会收到resume对应的interruptEvent 551e41f4b71Sopenharmony_ci console.info('Force paused. Update playing status and stop writing'); 552e41f4b71Sopenharmony_ci isPlaying = false; // 简化处理,代表应用切换至暂停状态的若干操作 553e41f4b71Sopenharmony_ci break; 554e41f4b71Sopenharmony_ci case audio.InterruptHint.INTERRUPT_HINT_STOP: 555e41f4b71Sopenharmony_ci // 音频流已被停止,永久失去焦点,若想恢复渲染,需用户主动触发 556e41f4b71Sopenharmony_ci console.info('Force stopped. Update playing status and stop writing'); 557e41f4b71Sopenharmony_ci isPlaying = false; // 简化处理,代表应用切换至暂停状态的若干操作 558e41f4b71Sopenharmony_ci break; 559e41f4b71Sopenharmony_ci case audio.InterruptHint.INTERRUPT_HINT_DUCK: 560e41f4b71Sopenharmony_ci // 音频流已被降低音量渲染 561e41f4b71Sopenharmony_ci console.info('Force ducked. Update volume status'); 562e41f4b71Sopenharmony_ci isDucked = true; // 简化处理,代表应用更新音量状态的若干操作 563e41f4b71Sopenharmony_ci break; 564e41f4b71Sopenharmony_ci case audio.InterruptHint.INTERRUPT_HINT_UNDUCK: 565e41f4b71Sopenharmony_ci // 音频流已被恢复正常音量渲染 566e41f4b71Sopenharmony_ci console.info('Force ducked. Update volume status'); 567e41f4b71Sopenharmony_ci isDucked = false; // 简化处理,代表应用更新音量状态的若干操作 568e41f4b71Sopenharmony_ci break; 569e41f4b71Sopenharmony_ci default: 570e41f4b71Sopenharmony_ci break; 571e41f4b71Sopenharmony_ci } 572e41f4b71Sopenharmony_ci } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 573e41f4b71Sopenharmony_ci // 音频焦点事件需由应用进行操作,应用可以自主选择如何处理该事件,建议应用遵从InterruptHint提示处理 574e41f4b71Sopenharmony_ci switch (interruptEvent.hintType) { 575e41f4b71Sopenharmony_ci case audio.InterruptHint.INTERRUPT_HINT_RESUME: 576e41f4b71Sopenharmony_ci // 建议应用继续渲染(说明音频流此前被强制暂停,临时失去焦点,现在可以恢复渲染) 577e41f4b71Sopenharmony_ci // 由于INTERRUPT_HINT_RESUME操作需要应用主动执行,系统无法强制,故INTERRUPT_HINT_RESUME事件一定为INTERRUPT_SHARE类型 578e41f4b71Sopenharmony_ci console.info('Resume force paused renderer or ignore'); 579e41f4b71Sopenharmony_ci // 若选择继续渲染,需在此处主动执行开始渲染的若干操作 580e41f4b71Sopenharmony_ci break; 581e41f4b71Sopenharmony_ci default: 582e41f4b71Sopenharmony_ci break; 583e41f4b71Sopenharmony_ci } 584e41f4b71Sopenharmony_ci } 585e41f4b71Sopenharmony_ci}); 586e41f4b71Sopenharmony_ci``` 587e41f4b71Sopenharmony_ci 588e41f4b71Sopenharmony_ci### off('audioInterrupt') 589e41f4b71Sopenharmony_ci 590e41f4b71Sopenharmony_cioff(type: 'audioInterrupt', callback?: Callback<audio.InterruptEvent>): void 591e41f4b71Sopenharmony_ci 592e41f4b71Sopenharmony_ci取消监听音频中断事件,使用callback方式返回结果。 593e41f4b71Sopenharmony_ci 594e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Multimedia.AudioHaptic.Core 595e41f4b71Sopenharmony_ci 596e41f4b71Sopenharmony_ci**参数:** 597e41f4b71Sopenharmony_ci 598e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 599e41f4b71Sopenharmony_ci| ----- | ----- | ---- | ------------------------------------------------- | 600e41f4b71Sopenharmony_ci| type | string | 是 | 要取消订阅事件的类型。支持的事件为:'audioInterrupt'。 | 601e41f4b71Sopenharmony_ci| callback | Callback<[audio.InterruptEvent](js-apis-audio.md#interruptevent9)> | 否 | 回调函数,取消监听时,返回应用中断事件信息。 | 602e41f4b71Sopenharmony_ci 603e41f4b71Sopenharmony_ci**示例:** 604e41f4b71Sopenharmony_ci 605e41f4b71Sopenharmony_ci```ts 606e41f4b71Sopenharmony_ciimport { audio } from '@kit.AudioKit'; 607e41f4b71Sopenharmony_ci 608e41f4b71Sopenharmony_ci// 取消该事件的所有监听 609e41f4b71Sopenharmony_ciaudioHapticPlayerInstance.off('audioInterrupt'); 610e41f4b71Sopenharmony_ci 611e41f4b71Sopenharmony_ci// 同一监听事件中,on方法和off方法传入callback参数一致,off方法取消对应on方法订阅的监听 612e41f4b71Sopenharmony_cilet isPlaying: boolean; // 标识符,表示是否正在渲染 613e41f4b71Sopenharmony_cilet isDucked: boolean; // 标识符,表示是否被降低音量 614e41f4b71Sopenharmony_cilet audioInterruptCallback = (interruptEvent: audio.InterruptEvent) => { 615e41f4b71Sopenharmony_ci // 在发生音频打断事件时,audioHapticPlayerInstance收到interruptEvent回调,此处根据其内容做相应处理。 616e41f4b71Sopenharmony_ci // 1、可选:读取interruptEvent.forceType的类型,判断系统是否已强制执行相应操作。 617e41f4b71Sopenharmony_ci // 注:默认焦点策略下,INTERRUPT_HINT_RESUME为INTERRUPT_SHARE类型,其余hintType均为INTERRUPT_FORCE类型。因此对forceType可不做判断。 618e41f4b71Sopenharmony_ci // 2、必选:读取interruptEvent.hintType的类型,做出相应的处理。 619e41f4b71Sopenharmony_ci if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { 620e41f4b71Sopenharmony_ci // 音频焦点事件已由系统强制执行,应用需更新自身状态及显示内容等 621e41f4b71Sopenharmony_ci switch (interruptEvent.hintType) { 622e41f4b71Sopenharmony_ci case audio.InterruptHint.INTERRUPT_HINT_PAUSE: 623e41f4b71Sopenharmony_ci // 音频流已被暂停,临时失去焦点,待可重获焦点时会收到resume对应的interruptEvent 624e41f4b71Sopenharmony_ci console.info('Force paused. Update playing status and stop writing'); 625e41f4b71Sopenharmony_ci isPlaying = false; // 简化处理,代表应用切换至暂停状态的若干操作 626e41f4b71Sopenharmony_ci break; 627e41f4b71Sopenharmony_ci case audio.InterruptHint.INTERRUPT_HINT_STOP: 628e41f4b71Sopenharmony_ci // 音频流已被停止,永久失去焦点,若想恢复渲染,需用户主动触发 629e41f4b71Sopenharmony_ci console.info('Force stopped. Update playing status and stop writing'); 630e41f4b71Sopenharmony_ci isPlaying = false; // 简化处理,代表应用切换至暂停状态的若干操作 631e41f4b71Sopenharmony_ci break; 632e41f4b71Sopenharmony_ci case audio.InterruptHint.INTERRUPT_HINT_DUCK: 633e41f4b71Sopenharmony_ci // 音频流已被降低音量渲染 634e41f4b71Sopenharmony_ci console.info('Force ducked. Update volume status'); 635e41f4b71Sopenharmony_ci isDucked = true; // 简化处理,代表应用更新音量状态的若干操作 636e41f4b71Sopenharmony_ci break; 637e41f4b71Sopenharmony_ci case audio.InterruptHint.INTERRUPT_HINT_UNDUCK: 638e41f4b71Sopenharmony_ci // 音频流已被恢复正常音量渲染 639e41f4b71Sopenharmony_ci console.info('Force ducked. Update volume status'); 640e41f4b71Sopenharmony_ci isDucked = false; // 简化处理,代表应用更新音量状态的若干操作 641e41f4b71Sopenharmony_ci break; 642e41f4b71Sopenharmony_ci default: 643e41f4b71Sopenharmony_ci break; 644e41f4b71Sopenharmony_ci } 645e41f4b71Sopenharmony_ci } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { 646e41f4b71Sopenharmony_ci // 音频焦点事件需由应用进行操作,应用可以自主选择如何处理该事件,建议应用遵从InterruptHint提示处理 647e41f4b71Sopenharmony_ci switch (interruptEvent.hintType) { 648e41f4b71Sopenharmony_ci case audio.InterruptHint.INTERRUPT_HINT_RESUME: 649e41f4b71Sopenharmony_ci // 建议应用继续渲染(说明音频流此前被强制暂停,临时失去焦点,现在可以恢复渲染) 650e41f4b71Sopenharmony_ci // 由于INTERRUPT_HINT_RESUME操作需要应用主动执行,系统无法强制,故INTERRUPT_HINT_RESUME事件一定为INTERRUPT_SHARE类型 651e41f4b71Sopenharmony_ci console.info('Resume force paused renderer or ignore'); 652e41f4b71Sopenharmony_ci // 若选择继续渲染,需在此处主动执行开始渲染的若干操作 653e41f4b71Sopenharmony_ci break; 654e41f4b71Sopenharmony_ci default: 655e41f4b71Sopenharmony_ci break; 656e41f4b71Sopenharmony_ci } 657e41f4b71Sopenharmony_ci } 658e41f4b71Sopenharmony_ci}; 659e41f4b71Sopenharmony_ci 660e41f4b71Sopenharmony_ciaudioHapticPlayerInstance.on('audioInterrupt', audioInterruptCallback); 661e41f4b71Sopenharmony_ci 662e41f4b71Sopenharmony_ciaudioHapticPlayerInstance.off('audioInterrupt', audioInterruptCallback); 663e41f4b71Sopenharmony_ci``` 664