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