振動

2024-01-23 17:49 更新

vibrator模塊提供控制馬達振動啟、停的能力。

說明

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

導入模塊

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

vibrator.startVibration9+

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

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

需要權限:ohos.permission.VIBRATE

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

參數(shù):

參數(shù)名

類型

必填

說明

effect

VibrateEffect

馬達振動效果。

attribute

VibrateAttribute

馬達振動屬性。

callback

AsyncCallback<void>

回調函數(shù),當馬達振動成功,err為undefined,否則為錯誤對象。

錯誤碼

以下錯誤碼的詳細介紹請參見 ohos.vibrator錯誤碼

錯誤碼ID

錯誤信息

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ù)指定振動效果和振動屬性觸發(fā)馬達振動。

需要權限:ohos.permission.VIBRATE

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

參數(shù):

參數(shù)名

類型

必填

說明

effect

VibrateEffect

馬達振動效果。

attribute

VibrateAttribute

馬達振動屬性。

返回值:

類型

說明

Promise<void>

Promise對象。

錯誤碼

以下錯誤碼的詳細介紹請參見 ohos.vibrator錯誤碼

錯誤碼ID

錯誤信息

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

按照指定模式停止馬達的振動。

需要權限:ohos.permission.VIBRATE

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

參數(shù):

參數(shù)名

類型

必填

說明

stopMode

VibratorStopMode

指定的停止振動模式。

callback

AsyncCallback<void>

回調函數(shù)。當馬達停止振動成功,err為undefined,否則為錯誤對象。

示例:

  1. try {
  2. // 按照固定時長振動
  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模式停止振動
  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>

按照指定模式停止馬達的振動。

需要權限:ohos.permission.VIBRATE

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

參數(shù):

參數(shù)名

類型

必填

說明

stopMode

VibratorStopMode

馬達停止指定的振動模式。

返回值:

類型

說明

Promise<void>

Promise對象。

示例:

  1. try {
  2. // 按照固定時長振動
  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模式停止振動
  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

預置的振動效果。

系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

名稱

說明

EFFECT_CLOCK_TIMER

"haptic.clock.timer"

描述用戶調整計時器時的振動效果。

VibratorStopMode

停止的振動模式。

系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

名稱

說明

VIBRATOR_STOP_MODE_TIME

"time"

停止模式為duration模式的振動。

VIBRATOR_STOP_MODE_PRESET

"preset"

停止模式為預置EffectId的振動。

VibrateEffect9+

馬達振動效果。

系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

類型

說明

VibrateTime

按照指定持續(xù)時間觸發(fā)馬達振動。

VibratePreset

按照預置振動類型觸發(fā)馬達振動。

VibrateTime9+

馬達振動時長。

系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

名稱

說明

type

"time"

按照指定持續(xù)時間觸發(fā)馬達振動。

duration

-

馬達持續(xù)振動時長, 單位ms。

VibratePreset9+

馬達預置振動類型。

系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

名稱

說明

type

"preset"

按照預置振動效果觸發(fā)馬達振動。

effectId

-

預置的振動效果ID。

count

-

重復振動的次數(shù)。

VibrateAttribute9+

馬達振動屬性。

系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

名稱

說明

id

0

振動器id。

usage

-

馬達振動的使用場景。

Usage9+

振動使用場景。

系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.Sensors.MiscDevice

名稱

類型

說明

unknown

string

沒有明確使用場景,最低優(yōu)先級。

alarm

string

用于警報場景。

ring

string

用于鈴聲場景。

notification

string

用于通知場景。

communication

string

用于通信場景。

touch

string

用于觸摸場景。

media

string

用于多媒體場景。

physicalFeedback

string

用于物理反饋場景。

simulateReality

string

用于模擬現(xiàn)實場景。

vibrator.vibrate(deprecated)

vibrate(duration: number): Promise<void>

按照指定持續(xù)時間觸發(fā)馬達振動。

從API version 9 開始不再維護,建議使用 vibrator.startVibration 代替。

需要權限:ohos.permission.VIBRATE

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

參數(shù):

參數(shù)名

類型

必填

說明

duration

number

馬達振動時長, 單位ms。

返回值:

類型

說明

Promise<void>

Promise對象。

示例:

  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ù)時間觸發(fā)馬達振動。

從API version 9 開始不再維護,建議使用 vibrator.startVibration 代替。

需要權限:ohos.permission.VIBRATE

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

參數(shù):

參數(shù)名

類型

必填

說明

duration

number

馬達振動時長, 單位ms。

callback

AsyncCallback<void>

回調函數(shù)。當馬達振動成功,err為undefined,否則為錯誤對象。

示例:

  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>

按照預置振動效果觸發(fā)馬達振動。

從API version 9 開始不再維護,建議使用 vibrator.startVibration 代替。

需要權限:ohos.permission.VIBRATE

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

參數(shù):

參數(shù)名

類型

必填

說明

effectId

EffectId

預置的振動效果ID。

返回值:

類型

說明

Promise<void>

Promise對象。

示例:

  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

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

從API version 9 開始不再維護,建議使用 vibrator.startVibration 代替。

需要權限:ohos.permission.VIBRATE

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

參數(shù):

參數(shù)名

類型

必填

說明

effectId

EffectId

預置的振動效果ID。

callback

AsyncCallback<void>

回調函數(shù)。當馬達振動成功,err為undefined,否則為錯誤對象。

示例:

  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>

按照指定模式停止馬達的振動。

從API version 9 開始不再維護,建議使用 vibrator.stopVibration 代替。

需要權限:ohos.permission.VIBRATE

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

參數(shù):

參數(shù)名

類型

必填

說明

stopMode

VibratorStopMode

馬達停止指定的振動模式。

返回值:

類型

說明

Promise<void>

Promise對象。

示例:

  1. // 按照effectId類型啟動振動
  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模式停止振動
  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

按照指定模式停止馬達的振動。

從API version 9 開始不再維護,建議使用 vibrator.stopVibration 代替。

需要權限:ohos.permission.VIBRATE

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

參數(shù):

參數(shù)名

類型

必填

說明

stopMode

VibratorStopMode

馬達停止指定的振動模式。

callback

AsyncCallback<void>

回調函數(shù)。當馬達停止振動成功,err為undefined,否則為錯誤對象。

示例:

  1. // 按照effectId類型啟動振動
  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模式停止振動
  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. })
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號