振動(dòng)

2024-01-23 17:49 更新

vibrator模塊提供控制馬達(dá)振動(dòng)啟、停的能力。

說(shuō)明

本模塊首批接口從API version 8開(kāi)始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。

導(dǎo)入模塊

  1. import vibrator from '@ohos.vibrator';

vibrator.startVibration9+

startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback<void>): void

根據(jù)指定振動(dòng)效果和振動(dòng)屬性觸發(fā)馬達(dá)振動(dòng)。

需要權(quán)限:ohos.permission.VIBRATE

系統(tǒng)能力:SystemCapability.Sensors.MiscDevice

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

effect

VibrateEffect

馬達(dá)振動(dòng)效果。

attribute

VibrateAttribute

馬達(dá)振動(dòng)屬性。

callback

AsyncCallback<void>

回調(diào)函數(shù),當(dāng)馬達(dá)振動(dòng)成功,err為undefined,否則為錯(cuò)誤對(duì)象。

錯(cuò)誤碼

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn) ohos.vibrator錯(cuò)誤碼

錯(cuò)誤碼ID

錯(cuò)誤信息

14600101

Device operation failed.

示例:

  1. try {
  2. vibrator.startVibration({
  3. type: 'time',
  4. duration: 1000,
  5. }, {
  6. id: 0,
  7. usage: 'alarm'
  8. }, (error) => {
  9. if (error) {
  10. console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
  11. return;
  12. }
  13. console.log('Callback returned to indicate a successful vibration.');
  14. });
  15. } catch (err) {
  16. console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
  17. }

vibrator.startVibration9+

startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void>

根據(jù)指定振動(dòng)效果和振動(dòng)屬性觸發(fā)馬達(dá)振動(dòng)。

需要權(quán)限:ohos.permission.VIBRATE

系統(tǒng)能力:SystemCapability.Sensors.MiscDevice

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

effect

VibrateEffect

馬達(dá)振動(dòng)效果。

attribute

VibrateAttribute

馬達(dá)振動(dòng)屬性。

返回值:

類型

說(shuō)明

Promise<void>

Promise對(duì)象。

錯(cuò)誤碼

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn) ohos.vibrator錯(cuò)誤碼

錯(cuò)誤碼ID

錯(cuò)誤信息

14600101

Device operation failed.

示例:

  1. try {
  2. vibrator.startVibration({
  3. type: 'time',
  4. duration: 1000
  5. }, {
  6. id: 0,
  7. usage: 'alarm'
  8. }).then(() => {
  9. console.log('Promise returned to indicate a successful vibration');
  10. }, (error) => {
  11. console.error('error.code' + error.code + 'error.message' + error.message);
  12. });
  13. } catch (err) {
  14. console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
  15. }

vibrator.stopVibration9+

stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>): void

按照指定模式停止馬達(dá)的振動(dòng)。

需要權(quán)限:ohos.permission.VIBRATE

系統(tǒng)能力:SystemCapability.Sensors.MiscDevice

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

stopMode

VibratorStopMode

指定的停止振動(dòng)模式。

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)馬達(dá)停止振動(dòng)成功,err為undefined,否則為錯(cuò)誤對(duì)象。

示例:

  1. try {
  2. // 按照固定時(shí)長(zhǎng)振動(dòng)
  3. vibrator.startVibration({
  4. type: 'time',
  5. duration: 1000,
  6. }, {
  7. id: 0,
  8. usage: 'alarm'
  9. }, (error) => {
  10. if (error) {
  11. console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
  12. return;
  13. }
  14. console.log('Callback returned to indicate a successful vibration.');
  15. });
  16. } catch (err) {
  17. console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
  18. }
  19. try {
  20. // 按照VIBRATOR_STOP_MODE_TIME模式停止振動(dòng)
  21. vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) {
  22. if (error) {
  23. console.log('error.code' + error.code + 'error.message' + error.message);
  24. return;
  25. }
  26. console.log('Callback returned to indicate successful.');
  27. })
  28. } catch (err) {
  29. console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
  30. }

vibrator.stopVibration9+

stopVibration(stopMode: VibratorStopMode): Promise<void>

按照指定模式停止馬達(dá)的振動(dòng)。

需要權(quán)限:ohos.permission.VIBRATE

系統(tǒng)能力:SystemCapability.Sensors.MiscDevice

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

stopMode

VibratorStopMode

馬達(dá)停止指定的振動(dòng)模式。

返回值:

類型

說(shuō)明

Promise<void>

Promise對(duì)象。

示例:

  1. try {
  2. // 按照固定時(shí)長(zhǎng)振動(dòng)
  3. vibrator.startVibration({
  4. type: 'time',
  5. duration: 1000
  6. }, {
  7. id: 0,
  8. usage: 'alarm'
  9. }).then(() => {
  10. console.log('Promise returned to indicate a successful vibration');
  11. }, (error) => {
  12. console.error('error.code' + error.code + 'error.message' + error.message);
  13. });
  14. } catch (err) {
  15. console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
  16. }
  17. try {
  18. // 按照VIBRATOR_STOP_MODE_TIME模式停止振動(dòng)
  19. vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME).then(() => {
  20. console.log('Promise returned to indicate a successful vibration.');
  21. }, (error) => {
  22. console.log('error.code' + error.code + 'error.message' + error.message);
  23. });
  24. } catch (err) {
  25. console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
  26. }

EffectId

預(yù)置的振動(dòng)效果。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

名稱

說(shuō)明

EFFECT_CLOCK_TIMER

"haptic.clock.timer"

描述用戶調(diào)整計(jì)時(shí)器時(shí)的振動(dòng)效果。

VibratorStopMode

停止的振動(dòng)模式。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

名稱

說(shuō)明

VIBRATOR_STOP_MODE_TIME

"time"

停止模式為duration模式的振動(dòng)。

VIBRATOR_STOP_MODE_PRESET

"preset"

停止模式為預(yù)置EffectId的振動(dòng)。

VibrateEffect9+

馬達(dá)振動(dòng)效果。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

類型

說(shuō)明

VibrateTime

按照指定持續(xù)時(shí)間觸發(fā)馬達(dá)振動(dòng)。

VibratePreset

按照預(yù)置振動(dòng)類型觸發(fā)馬達(dá)振動(dòng)。

VibrateTime9+

馬達(dá)振動(dòng)時(shí)長(zhǎng)。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

名稱

說(shuō)明

type

"time"

按照指定持續(xù)時(shí)間觸發(fā)馬達(dá)振動(dòng)。

duration

-

馬達(dá)持續(xù)振動(dòng)時(shí)長(zhǎng), 單位ms。

VibratePreset9+

馬達(dá)預(yù)置振動(dòng)類型。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

名稱

說(shuō)明

type

"preset"

按照預(yù)置振動(dòng)效果觸發(fā)馬達(dá)振動(dòng)。

effectId

-

預(yù)置的振動(dòng)效果ID。

count

-

重復(fù)振動(dòng)的次數(shù)。

VibrateAttribute9+

馬達(dá)振動(dòng)屬性。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

名稱

說(shuō)明

id

0

振動(dòng)器id。

usage

-

馬達(dá)振動(dòng)的使用場(chǎng)景。

Usage9+

振動(dòng)使用場(chǎng)景。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

名稱

類型

說(shuō)明

unknown

string

沒(méi)有明確使用場(chǎng)景,最低優(yōu)先級(jí)。

alarm

string

用于警報(bào)場(chǎng)景。

ring

string

用于鈴聲場(chǎng)景。

notification

string

用于通知場(chǎng)景。

communication

string

用于通信場(chǎng)景。

touch

string

用于觸摸場(chǎng)景。

media

string

用于多媒體場(chǎng)景。

physicalFeedback

string

用于物理反饋場(chǎng)景。

simulateReality

string

用于模擬現(xiàn)實(shí)場(chǎng)景。

vibrator.vibrate(deprecated)

vibrate(duration: number): Promise<void>

按照指定持續(xù)時(shí)間觸發(fā)馬達(dá)振動(dòng)。

從API version 9 開(kāi)始不再維護(hù),建議使用 vibrator.startVibration 代替。

需要權(quán)限:ohos.permission.VIBRATE

系統(tǒng)能力:SystemCapability.Sensors.MiscDevice

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

duration

number

馬達(dá)振動(dòng)時(shí)長(zhǎng), 單位ms。

返回值:

類型

說(shuō)明

Promise<void>

Promise對(duì)象。

示例:

  1. vibrator.vibrate(1000).then(() => {
  2. console.log('Promise returned to indicate a successful vibration.');
  3. }, (error) => {
  4. console.log('error.code' + error.code + 'error.message' + error.message);
  5. });

vibrator.vibrate(deprecated)

vibrate(duration: number, callback?: AsyncCallback<void>): void

按照指定持續(xù)時(shí)間觸發(fā)馬達(dá)振動(dòng)。

從API version 9 開(kāi)始不再維護(hù),建議使用 vibrator.startVibration 代替。

需要權(quán)限:ohos.permission.VIBRATE

系統(tǒng)能力:SystemCapability.Sensors.MiscDevice

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

duration

number

馬達(dá)振動(dòng)時(shí)長(zhǎng), 單位ms。

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)馬達(dá)振動(dòng)成功,err為undefined,否則為錯(cuò)誤對(duì)象。

示例:

  1. vibrator.vibrate(1000, function (error) {
  2. if (error) {
  3. console.log('error.code' + error.code + 'error.message' + error.message);
  4. } else {
  5. console.log('Callback returned to indicate a successful vibration.');
  6. }
  7. })

vibrator.vibrate(deprecated)

vibrate(effectId: EffectId): Promise<void>

按照預(yù)置振動(dòng)效果觸發(fā)馬達(dá)振動(dòng)。

從API version 9 開(kāi)始不再維護(hù),建議使用 vibrator.startVibration 代替。

需要權(quán)限:ohos.permission.VIBRATE

系統(tǒng)能力:SystemCapability.Sensors.MiscDevice

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

effectId

EffectId

預(yù)置的振動(dòng)效果ID。

返回值:

類型

說(shuō)明

Promise<void>

Promise對(duì)象。

示例:

  1. vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => {
  2. console.log('Promise returned to indicate a successful vibration.');
  3. }, (error) => {
  4. console.log('error.code' + error.code + 'error.message' + error.message);
  5. });

vibrator.vibrate(deprecated)

vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void

按照指定振動(dòng)效果觸發(fā)馬達(dá)振動(dòng)。

從API version 9 開(kāi)始不再維護(hù),建議使用 vibrator.startVibration 代替。

需要權(quán)限:ohos.permission.VIBRATE

系統(tǒng)能力:SystemCapability.Sensors.MiscDevice

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

effectId

EffectId

預(yù)置的振動(dòng)效果ID。

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)馬達(dá)振動(dòng)成功,err為undefined,否則為錯(cuò)誤對(duì)象。

示例:

  1. vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) {
  2. if (error) {
  3. console.log('error.code' + error.code + 'error.message' + error.message);
  4. } else {
  5. console.log('Callback returned to indicate a successful vibration.');
  6. }
  7. })

vibrator.stop(deprecated)

stop(stopMode: VibratorStopMode): Promise<void>

按照指定模式停止馬達(dá)的振動(dòng)。

從API version 9 開(kāi)始不再維護(hù),建議使用 vibrator.stopVibration 代替。

需要權(quán)限:ohos.permission.VIBRATE

系統(tǒng)能力:SystemCapability.Sensors.MiscDevice

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

stopMode

VibratorStopMode

馬達(dá)停止指定的振動(dòng)模式。

返回值:

類型

說(shuō)明

Promise<void>

Promise對(duì)象。

示例:

  1. // 按照effectId類型啟動(dòng)振動(dòng)
  2. vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) {
  3. if (error) {
  4. console.log('error.code' + error.code + 'error.message' + error.message);
  5. } else {
  6. console.log('Callback returned to indicate a successful vibration.');
  7. }
  8. })
  9. // 使用VIBRATOR_STOP_MODE_PRESET模式停止振動(dòng)
  10. vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
  11. console.log('Promise returned to indicate a successful vibration.');
  12. }, (error) => {
  13. console.log('error.code' + error.code + 'error.message' + error.message);
  14. });

vibrator.stop(deprecated)

stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void

按照指定模式停止馬達(dá)的振動(dòng)。

從API version 9 開(kāi)始不再維護(hù),建議使用 vibrator.stopVibration 代替。

需要權(quán)限:ohos.permission.VIBRATE

系統(tǒng)能力:SystemCapability.Sensors.MiscDevice

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

stopMode

VibratorStopMode

馬達(dá)停止指定的振動(dòng)模式。

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)馬達(dá)停止振動(dòng)成功,err為undefined,否則為錯(cuò)誤對(duì)象。

示例:

  1. // 按照effectId類型啟動(dòng)振動(dòng)
  2. vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) {
  3. if (error) {
  4. console.log('error.code' + error.code + 'error.message' + error.message);
  5. } else {
  6. console.log('Callback returned to indicate a successful vibration.');
  7. }
  8. })
  9. // 使用VIBRATOR_STOP_MODE_PRESET模式停止振動(dòng)
  10. vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function (error) {
  11. if (error) {
  12. console.log('error.code' + error.code + 'error.message' + error.message);
  13. } else {
  14. console.log('Callback returned to indicate successful.');
  15. }
  16. })
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)