如何使用我的Andr​​oid手机应用程序设置分贝音量? [英] How to set volume in decibels using my android mobile application?

查看:397
本文介绍了如何使用我的Andr​​oid手机应用程序设置分贝音量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个移动应用程序,在这里我需要播放声音文件。在音量按​​钮preSS,我需要增加/通过我的应用程序降低音量。
我使用Media Player播放和使用SetVolume(左,右)函数来设置音量。

I am working on a mobile app, where i need to play the sound file. On volume button press, i need to increase/decrease the volume through my app. I am using Media player to play and using SetVolume(left, right) function to set the volume.

MediaPlayer mp;
float left = 0.0f;
float right = 0.6f;

public void OnPlay(View v){ 
    mp = MediaPlayer.create(this, R.raw.twofiftybeep);
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    mp.start();


    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            // Playing the file continuously
            mp.setVolume(left, right);

            mp.start();

        }
    } );
}

public void OnStop(View v){
    mp.stop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        left= left + 0.05f;
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        left = left - 0.05f;
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }

}

但SetVolume参数从0.0范围为1.0F,其中0.0是最低和1.0F是最大的。

But SetVolume parameters ranges from 0.0f to 1.0f where 0.0f is for minimum and 1.0f is for maximum.

但我想设置像40分贝,50分贝,60分贝等一些事情
在每一个音量按钮preSS,我想递增或递减5分贝。

But i want to set some thing like 40dB, 50dB, 60dB etc. On every volume button press, i want to either increment or decrement 5dB.

我怎样才能使0.0 - 1.0F范围dB值转换

How can i make the 0.0f - 1.0f range to dB value conversion?

有没有其他的最好方式?

Is there any other best way?

推荐答案

有关数字信号DB通常是前pressed相对于满刻度的幅度。

For digital signal dB is typically expressed relative to the full scale amplitude.

1.0 == 0dB
0.5 == -6dB
0.25 == -12dB
...
0.0  == -inf dB

等。

要振幅转换为分贝: DB = 20 * log10的(AMPL)

要从分贝转换为振幅: AMPL = 10 ^(DB / 20)

To convert from dB to amplitude: ampl = 10 ^ (dB/20)

要,然后实现自己的目标最简单的方法是存储你的价值观离开右键以dB为单位,初始化它们都为零。您发布的code可以保持完全一样的,除非你不会允许值大于零的递增。然后值转换为线性的您呼叫 setVolume 。例如 mp.setVolume(Math.pow(10,左/ 20),Math.pow(10,右/ 20))

The simplest way to then achieve your goal is to store your values left and right in dB, initializing them both to zero. Your posted code can stay exactly the same except you wouldn't allow the values to be incremented above zero. Then convert the values to linear as you are calling setVolume. e.g. mp.setVolume(Math.pow(10,left/20), Math.pow(10,right/20))

这篇关于如何使用我的Andr​​oid手机应用程序设置分贝音量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆