1# 使用AudioHaptic开发音振协同播放功能 2<!--Kit: Audio Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @songshenke--> 5<!--Designer: @caixuejiang; @hao-liangfei; @zhanganxiang--> 6<!--Tester: @Filger--> 7<!--Adviser: @zengyawen--> 8 9AudioHaptic<sup>11+</sup>提供音频与振动协同播放及管理的方法,适用于需要在播放音频时同步发起振动的场景,如来电铃声随振、键盘按键反馈、消息通知反馈等。 10 11## 开发指导 12 13使用AudioHaptic播放音频并同步开启振动,涉及到音频及振动资源的管理、音频时延模式及音频流使用类型的配置、音振播放器的创建及管理等。本开发指导将以一次音振协同播放的过程为例,向开发者讲解如何使用AudioHaptic进行音振协同播放,建议配合[AudioHaptic的API说明](../../reference/apis-audio-kit/js-apis-audioHaptic.md#audiohapticmanager)阅读。 14 15### 权限申请 16 17如果应用创建的AudioHapticPlayer需要触发振动,则需要校验应用是否拥有该权限:`ohos.permission.VIBRATE`。 18 191. [声明权限](../../security/AccessToken/declare-permissions.md)。 202. [向用户申请授权](../../security/AccessToken/request-user-authorization.md)。 21 22### 开发步骤及注意事项 23 241. 获取音振管理器实例,并注册音频及振动资源,资源支持情况可以查看[AudioHapticManager](../../reference/apis-audio-kit/js-apis-audioHaptic.md#audiohapticmanager)。 25 26 ```ts 27 import { audio, audioHaptic } from '@kit.AudioKit'; 28 import { BusinessError } from '@kit.BasicServicesKit'; 29 30 let audioHapticManagerInstance: audioHaptic.AudioHapticManager = audioHaptic.getAudioHapticManager(); 31 32 let audioUri = 'data/audioTest.wav'; // 需更改为目标音频资源的Uri。 33 let hapticUri = 'data/hapticTest.json'; // 需更改为目标振动资源的Uri。 34 let id = 0; 35 36 audioHapticManagerInstance.registerSource(audioUri, hapticUri).then((value: number) => { 37 console.info(`Promise returned to indicate that the source id of the registered source ${value}.`); 38 id = value; 39 }).catch((err: BusinessError) => { 40 console.error(`Failed to register source ${err}`); 41 }); 42 ``` 43 442. 设置音振播放器参数,各参数作用可以查看[AudioHapticManager](../../reference/apis-audio-kit/js-apis-audioHaptic.md#audiohapticmanager)。 45 46 ```ts 47 let latencyMode: audioHaptic.AudioLatencyMode = audioHaptic.AudioLatencyMode.AUDIO_LATENCY_MODE_FAST; 48 audioHapticManagerInstance.setAudioLatencyMode(id, latencyMode); 49 50 let usage: audio.StreamUsage = audio.StreamUsage.STREAM_USAGE_NOTIFICATION; 51 audioHapticManagerInstance.setStreamUsage(id, usage); 52 ``` 53 543. 创建AudioHapticPlayer实例。 55 56 ```ts 57 let options: audioHaptic.AudioHapticPlayerOptions = {muteAudio: false, muteHaptics: false}; 58 let audioHapticPlayer: audioHaptic.AudioHapticPlayer | undefined = undefined; 59 60 audioHapticManagerInstance.createPlayer(id, options).then((value: audioHaptic.AudioHapticPlayer) => { 61 console.info(`Create the audio haptic player successfully.`); 62 audioHapticPlayer = value; 63 }).catch((err: BusinessError) => { 64 console.error(`Failed to create player ${err}`); 65 }); 66 ``` 67 684. 调用start()方法,开启音频播放并同步开启振动。 69 70 ```ts 71 audioHapticPlayer.start().then(() => { 72 console.info(`Promise returned to indicate that start playing successfully.`); 73 }).catch((err: BusinessError) => { 74 console.error(`Failed to start playing. ${err}`); 75 }); 76 ``` 77 785. 调用stop()方法,停止音频播放并同步停止振动。 79 80 ```ts 81 audioHapticPlayer.stop().then(() => { 82 console.info(`Promise returned to indicate that stop playing successfully.`); 83 }).catch((err: BusinessError) => { 84 console.error(`Failed to stop playing. ${err}`); 85 }); 86 ``` 87 886. 释放AudioHapticPlayer实例。 89 90 ```ts 91 audioHapticPlayer.release().then(() => { 92 console.info(`Promise returned to indicate that release the audio haptic player successfully.`); 93 }).catch((err: BusinessError) => { 94 console.error(`Failed to release the audio haptic player. ${err}`); 95 }); 96 ``` 97 987. 将已注册的音频及振动资源移除注册。 99 100 ```ts 101 audioHapticManagerInstance.unregisterSource(id).then(() => { 102 console.info(`Promise returned to indicate that unregister source successfully`); 103 }).catch((err: BusinessError) => { 104 console.error(`Failed to unregister source ${err}`); 105 }); 106 ``` 107 108## 相关实例 109 110针对音振协同开发,有以下相关实例可供参考: 111 112- [音振协同示例(ArkTS)(API11)](https://gitcode.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/Media/AudioHaptic) 113