1# 振动 2 3> **说明:** 4> 5> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 6 7 8## 导入模块 9 10```js 11import vibrator from '@ohos.vibrator'; 12``` 13 14 15## vibrator.vibrate 16 17vibrate(duration: number): Promise<void> 18 19按照指定持续时间触发马达振动。 20 21**权限列表**:ohos.permission.VIBRATE 22 23**系统能力**:SystemCapability.Sensors.MiscDevice 24 25 26**参数:** 27| 参数名 | 类型 | 必填 | 说明 | 28| -------- | ------ | ---- | ------------ | 29| duration | number | 是 | 指示马达振动的持续时间。 | 30 31**返回值:** 32| 类型 | 说明 | 33| ------------------- | ----------- | 34| Promise<void> | 指示触发振动是否成功。 | 35 36 37**示例:** 38 ```js 39 vibrator.vibrate(1000).then(()=>{ 40 console.log("Promise returned to indicate a successful vibration."); 41 }, (error)=>{ 42 console.log("error.code"+error.code+"error.message"+error.message); 43 }); 44 ``` 45 46 47## vibrator.vibrate 48 49vibrate(duration: number, callback?: AsyncCallback<void>): void 50 51按照指定持续时间触发马达振动。 52 53**权限列表**:ohos.permission.VIBRATE 54 55**系统能力**:SystemCapability.Sensors.MiscDevice 56 57**参数:** 58| 参数名 | 类型 | 必填 | 说明 | 59| -------- | ------------------------- | ---- | ----------------------- | 60| duration | number | 是 | 指示马达振动的持续时间。 | 61| callback | AsyncCallback<void> | 否 | 马达执行振动的回调函数,指示触发振动是否成功。 | 62 63**示例:** 64 ```js 65 vibrator.vibrate(1000,function(error){ 66 if(error){ 67 console.log("error.code"+error.code+"error.message"+error.message); 68 }else{ 69 console.log("Callback returned to indicate a successful vibration."); 70 } 71 }) 72 ``` 73 74 75## vibrator.vibrate 76 77vibrate(effectId: EffectId): Promise<void> 78 79按照指定振动效果触发马达振动。 80 81**权限列表**:ohos.permission.VIBRATE 82 83**系统能力**:SystemCapability.Sensors.MiscDevice 84 85**参数:** 86| 参数名 | 类型 | 必填 | 说明 | 87| -------- | --------------------- | ---- | ------------- | 88| effectId | [EffectId](#effectid) | 是 | 指示马达振动效果的字符串。 | 89 90**返回值:** 91| 类型 | 说明 | 92| ------------------- | ----------- | 93| Promise<void> | 指示触发振动是否成功。 | 94 95**示例:** 96 ```js 97 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(()=>{ 98 console.log("Promise returned to indicate a successful vibration."); 99 }, (error)=>{ 100 console.log("error.code"+error.code+"error.message"+error.message); 101 }); 102 ``` 103 104 105## vibrator.vibrate 106 107vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void 108 109按照指定振动效果触发马达振动。 110 111**权限列表**:ohos.permission.VIBRATE 112 113**系统能力**:SystemCapability.Sensors.MiscDevice 114 115**参数:** 116| 参数名 | 类型 | 必填 | 说明 | 117| -------- | ------------------------- | ---- | ----------------------- | 118| effectId | [EffectId](#effectid) | 是 | 指示马达振动效果的字符串。 | 119| callback | AsyncCallback<void> | 否 | 马达执行振动的回调函数,指示触发振动是否成功。 | 120 121**示例:** 122 ```js 123 vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function(error){ 124 if(error){ 125 console.log("error.code"+error.code+"error.message"+error.message); 126 }else{ 127 console.log("Callback returned to indicate a successful vibration."); 128 } 129 }) 130 ``` 131 132 133## vibrator.stop 134 135stop(stopMode: VibratorStopMode): Promise<void> 136 137按照要停止指定的振动模式来停止马达的振动。如果要停止的振动模式与触发马达振动时的模式不相同,则调用本接口会失败。 138 139**权限列表**:ohos.permission.VIBRATE 140 141**系统能力**:SystemCapability.Sensors.MiscDevice 142 143**参数:** 144| 参数名 | 类型 | 必填 | 说明 | 145| -------- | ------------------------------------- | ---- | --------------- | 146| stopMode | [VibratorStopMode](#vibratorstopmode) | 是 | 指示马达要停止指定的振动模式。 | 147 148**返回值:** 149| 类型 | 说明 | 150| ------------------- | ----------- | 151| Promise<void> | 指示停止振动是否成功。 | 152 153**示例:** 154 ```js 155 vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(()=>{ 156 console.log("Promise returned to indicate a successful vibration."); 157 }, (error)=>{ 158 console.log("error.code"+error.code+"error.message"+error.message); 159 }); 160 ``` 161 162 163## vibrator.stop 164 165stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void; 166 167按照要停止指定的振动模式来停止马达的振动。如果要停止的振动模式与触发马达振动时的模式不相同,则调用本接口会失败。 168 169**权限列表**:ohos.permission.VIBRATE 170 171**系统能力**:SystemCapability.Sensors.MiscDevice 172 173**参数:** 174| 参数名 | 类型 | 必填 | 说明 | 175| -------- | ------------------------------------- | ---- | ----------------------- | 176| stopMode | [VibratorStopMode](#vibratorstopmode) | 是 | 指示马达要停止指定的振动模式。 | 177| callback | AsyncCallback<void> | 否 | 马达停止振动的回调函数,指示停止振动是否成功。 | 178 179**示例:** 180 ```js 181 vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function(error){ 182 if(error){ 183 console.log("error.code"+error.code+"error.message"+error.message); 184 }else{ 185 console.log("Callback returned to indicate successful."); 186 } 187 }) 188 ``` 189 190 191## EffectId 192 193表示马达振动效果的字符串。 194 195**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.MiscDevice 196 197| 名称 | 默认值 | 说明 | 198| ------------------ | -------------------- | --------------- | 199| EFFECT_CLOCK_TIMER | "haptic.clock.timer" | 调整定时器时振动器的振动效果。 | 200 201 202## VibratorStopMode 203 204表示马达要停止指定的振动模式。 205 206**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.MiscDevice 207 208| 名称 | 默认值 | 说明 | 209| ------------------------- | -------- | ---------------------------------------- | 210| VIBRATOR_STOP_MODE_TIME | "time" | 停止模式为duration模式的振动。即触发振动时参数类型为number,参数本身为指示振动持续时间的触发方式。 | 211| VIBRATOR_STOP_MODE_PRESET | "preset" | 停止模式为预置EffectId的振动。即触发振动时参数类型为EffectId,参数本身为指示马达振动效果的字符串的触发方式。 | 212