• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimedia.audioHaptic (Audio-Haptic) (System API)
2
3Audio-haptic enables users to get rhythmic auditory and haptic feedback while having incoming calls or messages.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Module to Import
10
11```ts
12import { audioHaptic } from '@kit.AudioKit';
13```
14
15## AudioHapticPlayer
16
17Implements audio-haptic playback. Before calling any API in AudioHapticPlayer, you must use [createPlayer](./js-apis-audioHaptic.md#createplayer) to create an AudioHapticPlayer instance.
18
19### isHapticsIntensityAdjustmentSupported<sup>20+</sup>
20
21isHapticsIntensityAdjustmentSupported(): boolean
22
23Checks whether the device supports adjusting the intensity of haptic feedback.
24
25**System API**: This is a system API.
26
27**System capability**: SystemCapability.Multimedia.AudioHaptic.Core
28
29**Return value**
30
31| Type               | Description                           |
32| ------------------- | ------------------------------- |
33| boolean             | Check result. The value **true** is returned if the device supports intensity adjustment, and **false** is returned otherwise.|
34
35**Error codes**
36
37For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
38
39| Error Code  | Error Message                             |
40|---------|-----------------------------------|
41| 202 | Caller is not a system application. |
42
43**Example**
44
45```ts
46const result: boolean = audioHapticPlayerInstance.isHapticsIntensityAdjustmentSupported();
47```
48
49### isHapticsRampSupported<sup>20+</sup>
50
51isHapticsRampSupported(): boolean
52
53Checks whether the device supports gradually adjusting the intensity of haptic feedback, also known as haptic ramping.
54
55**System API**: This is a system API.
56
57**System capability**: SystemCapability.Multimedia.AudioHaptic.Core
58
59**Return value**
60
61| Type               | Description                           |
62| ------------------- | ------------------------------- |
63| boolean             | Check result. The value **true** is returned if the device supports haptic ramping, and **false** is returned otherwise.|
64
65**Error codes**
66
67For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
68
69| Error Code  | Error Message                             |
70|---------|-----------------------------------|
71| 202 | Caller is not a system application. |
72
73**Example**
74
75```ts
76const result: boolean = audioHapticPlayerInstance.isHapticsRampSupported();
77```
78
79### enableHapticsInSilentMode<sup>20+</sup>
80
81enableHapticsInSilentMode(enable: boolean): void
82
83Enables the audio-haptic player to provide haptic feedback even when the device is in silent mode.
84
85> **NOTE**
86>
87> This API must be called before the audio-haptic player is released. It cannot be called during playback.
88
89**System API**: This is a system API.
90
91**System capability**: SystemCapability.Multimedia.AudioHaptic.Core
92
93**Parameters**
94
95| Name | Type                                    | Mandatory| Description                   |
96| -------- | ---------------------------------------- | ---- | ------------------------ |
97| enable     | boolean                                | Yes | Whether to enable haptic feedback in silent mode. The value **true** means to enable haptic feedback in silent mode, and **false** means the opposite.|
98
99**Error codes**
100
101For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Media Error Codes](../apis-media-kit/errorcode-media.md).
102
103| Error Code | Error Message                            |
104|---------|-----------------------------------|
105| 202      | Caller is not a system application. |
106| 5400102  | Operate not permit in current state. |
107
108**Example**
109
110```ts
111audioHapticPlayerInstance.enableHapticsInSilentMode(true);
112```
113
114### setHapticsIntensity<sup>20+</sup>
115
116setHapticsIntensity(intensity: number): Promise&lt;void&gt;
117
118Sets the intensity of haptic feedback for this audio-haptic player. This API uses a promise to return the result.
119
120> **NOTE**
121>
122>This API must be called before the audio-haptic player is released. It can be called only once during a single playback.
123
124**System API**: This is a system API.
125
126**System capability**: SystemCapability.Multimedia.AudioHaptic.Core
127
128**Parameters**
129
130| Name | Type                                    | Mandatory| Description                   |
131| -------- | ---------------------------------------- | ---- | ------------------------ |
132| intensity     | number                              | Yes | Intensity, in the range [0.00, 1.00], where 1.00 indicates the maximum intensity (100%).|
133
134**Return value**
135
136| Type               | Description                           |
137| ------------------- | ------------------------------- |
138| Promise&amp;lt;void&amp;gt; | Promise that returns no value.|
139
140**Error codes**
141
142For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Media Error Codes](../apis-media-kit/errorcode-media.md).
143
144| Error Code  | Error Message                             |
145|---------|-----------------------------------|
146| 202      | Caller is not a system application. |
147| 801      | Function is not supported in current device. |
148| 5400102  | Operate not permit in current state. |
149| 5400108  | Parameter out of range. |
150
151**Example**
152
153```ts
154import { BusinessError } from '@kit.BasicServicesKit';
155
156audioHapticPlayerInstance.setHapticsIntensity(0.5).then(() => {
157  console.info('Promise returned to indicate that set intensity successfully.');
158}).catch ((err: BusinessError) => {
159  console.error(`Failed to set intensity. ${err}`);
160});
161```
162
163### setHapticsRamp<sup>20+</sup>
164
165setHapticsRamp(duration: number, startIntensity: number, endIntensity: number): Promise&lt;void&gt;
166
167Sets the haptic ramp for this audio-haptic player. This API uses a promise to return the result.
168
169> **NOTE**
170>
171>- This API must be called before and after the audio-haptic player plays and before the player is released.
172>- This API can be called only once.
173
174**System API**: This is a system API.
175
176**System capability**: SystemCapability.Multimedia.AudioHaptic.Core
177
178**Parameters**
179
180| Name | Type                                    | Mandatory| Description                   |
181| -------- | ---------------------------------------- | ---- | ------------------------ |
182| duration | number                           | Yes | Duration of the ramp, in ms. The value must be an integer and cannot be less than 100 ms.|
183| startIntensity | number                     | Yes | Start intensity. The value is in the range [0.00, 1.00], where 1.00 indicates the maximum intensity (100%).|
184| endIntensity   | number                     | Yes | End intensity. The value is in the range [0.00, 1.00], where 1.00 indicates the maximum intensity (100%).|
185
186**Return value**
187
188| Type               | Description                           |
189| ------------------- | ------------------------------- |
190| Promise&amp;lt;void&amp;gt; | Promise that returns no value.|
191
192**Error codes**
193
194For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Media Error Codes](../apis-media-kit/errorcode-media.md).
195
196| Error Code  | Error Message                             |
197|---------|-----------------------------------|
198| 202      | Caller is not a system application. |
199| 801      | Function is not supported in current device. |
200| 5400102  | Operate not permit in current state. |
201| 5400108  | Parameter out of range. |
202
203**Example**
204
205```ts
206import { BusinessError } from '@kit.BasicServicesKit';
207
208const duration = 10000;
209const startIntensity = 0.5;
210const endIntensity = 1;
211
212audioHapticPlayerInstance.setHapticsRamp(duration, startIntensity, endIntensity).then(() => {
213  console.info('Promise returned to indicate that set haptics ramp successfully.');
214}).catch ((err: BusinessError) => {
215  console.error(`Failed to set haptics ramp. ${err}`);
216});
217```
218