1e41f4b71Sopenharmony_ci# Using AudioHaptic for Audio-Haptic Playback 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciAudioHaptic<sup>11+</sup> provides APIs for audio-haptic playback and management. It applies to scenarios where haptic feedback needs to be initiated synchronously during audio playback, for example, when there are incoming calls or messages or users are typing. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## Development Guidelines 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciThe entire process of audio-haptic development involves management of audio and haptic sources, configuration of an audio latency mode and audio stream usage, and creation and management of an audio-haptic player. This topic uses the process of one-time audio-haptic playback as an example to describe how to use **AudioHaptic**. Before the development, read [AudioHaptic](../../reference/apis-audio-kit/js-apis-audioHaptic.md#audiohapticmanager) for better understanding. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci### Requesting Permissions 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciIf the audio-haptic player needs to trigger vibration, check whether the application has the permission **ohos.permission.VIBRATE**. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci1. [Declare the permission](../../security/AccessToken/declare-permissions.md). 14e41f4b71Sopenharmony_ci2. [Request user authorization](../../security/AccessToken/request-user-authorization.md). 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci### How to Develop 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci1. Obtain an **AudioHapticManager** instance, and register the audio and haptic sources. For details about the sources supported, see [AudioHapticManager](../../reference/apis-audio-kit/js-apis-audioHaptic.md#audiohapticmanager). 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci ```ts 21e41f4b71Sopenharmony_ci import { audio, audioHaptic } from '@kit.AudioKit'; 22e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci let audioHapticManagerInstance: audioHaptic.AudioHapticManager = audioHaptic.getAudioHapticManager(); 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci let audioUri = 'data/audioTest.wav'; // Change it to the URI of the target audio source. 27e41f4b71Sopenharmony_ci let hapticUri = 'data/hapticTest.json'; // Change it to the URI of the target haptic source. 28e41f4b71Sopenharmony_ci let id = 0; 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci audioHapticManagerInstance.registerSource(audioUri, hapticUri).then((value: number) => { 31e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that the source id of the registerd source ${value}.`); 32e41f4b71Sopenharmony_ci id = value; 33e41f4b71Sopenharmony_ci }).catch ((err: BusinessError) => { 34e41f4b71Sopenharmony_ci console.error(`Failed to register source ${err}`); 35e41f4b71Sopenharmony_ci }); 36e41f4b71Sopenharmony_ci ``` 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci2. Set the parameters of an audio-haptic player. For details, see [AudioHapticManager](../../reference/apis-audio-kit/js-apis-audioHaptic.md#audiohapticmanager). 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci ```ts 41e41f4b71Sopenharmony_ci let latencyMode: audioHaptic.AudioLatencyMode = audioHaptic.AudioLatencyMode.AUDIO_LATENCY_MODE_FAST; 42e41f4b71Sopenharmony_ci audioHapticManagerInstance.setAudioLatencyMode(id, latencyMode); 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci let usage: audio.StreamUsage = audio.StreamUsage.STREAM_USAGE_NOTIFICATION; 45e41f4b71Sopenharmony_ci audioHapticManagerInstance.setStreamUsage(id, usage); 46e41f4b71Sopenharmony_ci ``` 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci3. Create an **AudioHapticPlayer** instance. 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci ```ts 51e41f4b71Sopenharmony_ci let options: audioHaptic.AudioHapticPlayerOptions = {muteAudio: false, muteHaptics: false}; 52e41f4b71Sopenharmony_ci let audioHapticPlayer: audioHaptic.AudioHapticPlayer | undefined = undefined; 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci audioHapticManagerInstance.createPlayer(id, options).then((value: audioHaptic.AudioHapticPlayer) => { 55e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that the audio haptic player instance.`); 56e41f4b71Sopenharmony_ci audioHapticPlayer = value; 57e41f4b71Sopenharmony_ci }).catch ((err: BusinessError) => { 58e41f4b71Sopenharmony_ci console.error(`Failed to create player ${err}`); 59e41f4b71Sopenharmony_ci }); 60e41f4b71Sopenharmony_ci console.info(`Create the audio haptic player successfully.`); 61e41f4b71Sopenharmony_ci ``` 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci4. Call **start()** to start the audio-haptic player. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci ```ts 66e41f4b71Sopenharmony_ci audioHapticPlayer.start().then(() => { 67e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that start playing successfully.`); 68e41f4b71Sopenharmony_ci }).catch ((err: BusinessError) => { 69e41f4b71Sopenharmony_ci console.error(`Failed to start playing. ${err}`); 70e41f4b71Sopenharmony_ci }); 71e41f4b71Sopenharmony_ci ``` 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci5. Call **stop()** to stop the audio-haptic player. 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci ```ts 76e41f4b71Sopenharmony_ci audioHapticPlayer.stop().then(() => { 77e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that stop playing successfully.`); 78e41f4b71Sopenharmony_ci }).catch ((err: BusinessError) => { 79e41f4b71Sopenharmony_ci console.error(`Failed to stop playing. ${err}`); 80e41f4b71Sopenharmony_ci }); 81e41f4b71Sopenharmony_ci ``` 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci6. Release the **AudioHapticPlayer** instance. 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci ```ts 86e41f4b71Sopenharmony_ci audioHapticPlayer.release().then(() => { 87e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that release the audio haptic player successfully.`); 88e41f4b71Sopenharmony_ci }).catch ((err: BusinessError) => { 89e41f4b71Sopenharmony_ci console.error(`Failed to release the audio haptic player. ${err}`); 90e41f4b71Sopenharmony_ci }); 91e41f4b71Sopenharmony_ci ``` 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci7. Unregister the audio and haptic sources. 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci ```ts 96e41f4b71Sopenharmony_ci audioHapticManagerInstance.unregisterSource(id).then(() => { 97e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that unregister source successfully`); 98e41f4b71Sopenharmony_ci }).catch ((err: BusinessError) => { 99e41f4b71Sopenharmony_ci console.error(`Failed to unregistere source ${err}`); 100e41f4b71Sopenharmony_ci }); 101e41f4b71Sopenharmony_ci ``` 102