1# Using AudioHaptic for Audio-Haptic Playback
2
3AudioHaptic<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.
4
5## Development Guidelines
6
7The 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.
8
9### Requesting Permissions
10
11If the audio-haptic player needs to trigger vibration, check whether the application has the permission **ohos.permission.VIBRATE**.
12
131. [Declare the permission](../../security/AccessToken/declare-permissions.md).
142. [Request user authorization](../../security/AccessToken/request-user-authorization.md).
15
16### How to Develop
17
181. 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).
19
20   ```ts
21   import { audio, audioHaptic } from '@kit.AudioKit';
22   import { BusinessError } from '@kit.BasicServicesKit';
23
24   let audioHapticManagerInstance: audioHaptic.AudioHapticManager = audioHaptic.getAudioHapticManager();
25
26   let audioUri = 'data/audioTest.wav'; // Change it to the URI of the target audio source.
27   let hapticUri = 'data/hapticTest.json'; // Change it to the URI of the target haptic source.
28   let id = 0;
29
30   audioHapticManagerInstance.registerSource(audioUri, hapticUri).then((value: number) => {
31     console.info(`Promise returned to indicate that the source id of the registerd source ${value}.`);
32     id = value;
33   }).catch ((err: BusinessError) => {
34     console.error(`Failed to register source ${err}`);
35   });
36   ```
37
382. Set the parameters of an audio-haptic player. For details, see [AudioHapticManager](../../reference/apis-audio-kit/js-apis-audioHaptic.md#audiohapticmanager).
39
40   ```ts
41   let latencyMode: audioHaptic.AudioLatencyMode = audioHaptic.AudioLatencyMode.AUDIO_LATENCY_MODE_FAST;
42   audioHapticManagerInstance.setAudioLatencyMode(id, latencyMode);
43
44   let usage: audio.StreamUsage = audio.StreamUsage.STREAM_USAGE_NOTIFICATION;
45   audioHapticManagerInstance.setStreamUsage(id, usage);
46   ```
47
483. Create an **AudioHapticPlayer** instance.
49
50   ```ts
51   let options: audioHaptic.AudioHapticPlayerOptions = {muteAudio: false, muteHaptics: false};
52   let audioHapticPlayer: audioHaptic.AudioHapticPlayer | undefined = undefined;
53
54   audioHapticManagerInstance.createPlayer(id, options).then((value: audioHaptic.AudioHapticPlayer) => {
55     console.info(`Promise returned to indicate that the audio haptic player instance.`);
56     audioHapticPlayer = value;
57   }).catch ((err: BusinessError) => {
58     console.error(`Failed to create player ${err}`);
59   });
60   console.info(`Create the audio haptic player successfully.`);
61   ```
62
634. Call **start()** to start the audio-haptic player.
64
65   ```ts
66   audioHapticPlayer.start().then(() => {
67     console.info(`Promise returned to indicate that start playing successfully.`);
68   }).catch ((err: BusinessError) => {
69     console.error(`Failed to start playing. ${err}`);
70   });
71   ```
72
735. Call **stop()** to stop the audio-haptic player.
74
75   ```ts
76   audioHapticPlayer.stop().then(() => {
77     console.info(`Promise returned to indicate that stop playing successfully.`);
78   }).catch ((err: BusinessError) => {
79     console.error(`Failed to stop playing. ${err}`);
80   });
81   ```
82
836. Release the **AudioHapticPlayer** instance.
84
85   ```ts
86   audioHapticPlayer.release().then(() => {
87     console.info(`Promise returned to indicate that release the audio haptic player successfully.`);
88   }).catch ((err: BusinessError) => {
89     console.error(`Failed to release the audio haptic player. ${err}`);
90   });
91   ```
92
937. Unregister the audio and haptic sources.
94
95   ```ts
96   audioHapticManagerInstance.unregisterSource(id).then(() => {
97     console.info(`Promise returned to indicate that unregister source successfully`);
98   }).catch ((err: BusinessError) => {
99     console.error(`Failed to unregistere source ${err}`);
100   });
101   ```
102