简体   繁体   English

控制Android手机的振动强度? 可能吗?

[英]controlling vibration intensity in android phones? is it possible?

I am developing a game. 我正在开发一款游戏。 In which, i want do set different vibration intensities for different events. 其中,我想为不同的事件设置不同的振动强度。 I just want know if its really possible to control the vibration intensity and duration. 我只想知道它是否真的可以控制振动强度和持续时间。 Any advice or reference links, could be very helpful. 任何建议或参考链接都可能非常有用。 Thanks in advance. 提前致谢。

I've made a simple trick to somehow reduce the intensity of vibration. 我做了一个简单的技巧,以某种方式减少振动的强度。 My idea is to interleave vibration intervals with silent intervals. 我的想法是将振动间隔与静音间隔交错。 If you have one millisecond of vibration and then one second of silence and so on it seems like it's one constant vibration but weaker than normal. 如果你有一毫秒的振动然后一秒钟的静音等等,它似乎是一个恒定的振动但比正常弱。 You can try to increase the silence intervals to make the vibration even weaker. 您可以尝试增加静音间隔,使振动更弱。 Here goes the code example: 这是代码示例:

int strong_vibration = 30; //vibrate with a full power for 30 secs
int interval = 1000;
int dot = 1; //one millisecond of vibration
int short_gap = 1; //one millisecond of break - could be more to weaken the vibration
long[] pattern = {
        0,  // Start immediately
        strong_vibration, 
        interval,
        // 15 vibrations and 15 gaps = 30millis
        dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, //yeah I know it doesn't look good, but it's just an example. you can write some code to generate such pattern. 
    };

I think it depends on what you mean by intensity. 我认为这取决于你的强度是什么意思。 You can control the pattern and length of the vibration, but I don't think you can make it vibrate "stronger". 你可以控制振动的模式和长度,但我认为你不能让它振动“更强”。

http://developer.android.com/reference/android/os/Vibrator.html http://developer.android.com/reference/android/os/Vibrator.html

PWM can be used to produce a vibration pattern of various pulse widths, resulting in lower average voltage to the vibrator motor (and thus weaker vibration output). PWM可用于产生各种脉冲宽度的振动模式,从而导致振动器电机的平均电压较低(因此振动输出较弱)。

I've posted a simple proof of concept method here . 我已经发布的概念方法的一个简单证明这里 This method will generate a pattern with the specified intensity and duration. 此方法将生成具有指定强度和持续时间的模式。 The transition in that method isn't quite linear, so I have posted a bounty to hopefully get some alternate suggestions. 该方法的过渡并不是非常线性的,所以我发布了一笔赏金以希望获得一些替代建议。 Will update when I have an even better algorithm. 当我有一个更好的算法时会更新。

This could help you but It only works for API level 26 or above. 这可以帮到你,但它只适用于API级别26或更高级别。

public void vibrate(View view) {
    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

        long[] wave_time = {0, 100, 0, 100, 0, 100, 0, 100, 0, 100};
        int[] wave_ampl = {0, 50, 0, 100, 0, 150, 0, 200, 0, 255};

        VibrationEffect vibrationEffect = null;
        vibrationEffect = VibrationEffect.createWaveform(wave_time, wave_ampl, -1);
        vibrator.vibrate(vibrationEffect);
    }

}

Here wave_time array represents two types of times: 这里wave_time数组代表两种类型的时间:

  1. Time in which it should be idle (0th index, 2nd index, .etc) 它应该空闲的时间(第0个索引,第2个索引,.etc)
  2. Time In which it should vibrate (1st index, 3rd index, .etc) 振动的时间(第一指数,第三指数,.etc)

wave_ampl array represents the strength of the vibration wrt wave_time array. wave_ampl数组表示振动wrt wave_time数组的强度。

Explanation: 说明:

Phone waits for 0 ms (0th index of wave_time ) and starts vibrating for 100 ms (1st index of wave_time ) with intensity of 50 (1st index of wave_ampl ). 电话等待0毫秒(第0的索引wave_time ),并开始振动100毫秒(的第一索引wave_time )具有50强度(的第一索引wave_ampl )。


Similarly for phone vibrates for 100 ms (3rd index of wave_time ) with intensity of 100 (3rd index of wave_ampl ). 类似地,对于电话振动100毫秒(的第三索引wave_time )具有100强度(的第三索引wave_ampl )。


The maximum intensity is 255 in android. android中最大强度为255

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM