How to set volume in decibels using my android mobile application?

♀尐吖头ヾ 提交于 2019-12-08 04:00:30

问题


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);
    }

}

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

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.

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

Is there any other best way?


回答1:


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

and so on.

To convert the amplitude to dB: dB = 20*log10(ampl)

To convert from dB to amplitude: ampl = 10 ^ (dB/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))




回答2:


You can't set an absolute decibel rating programatically. Such a concept doesn't actually exist with audio. That would require knowing the physical configuration the speakers, room acoustings, how close to the user's ear, and measuring the samples from the sound file.

MediaPlayer.setVolume sets attenuation - not loudness or volume.

The volume specified by MediaPlayer.setVolume is relative to the hardware volume setting. You can't make the media source "louder" than it really is without adjusting the device volume. And with the MediaPlayer by itself, you don't really know how "loud" the user is hearing anything.

At the highest setting: player.setvolume(1.0, 1.0) - This will set the volume on the output stream to match the global volume on the device. This is the default.

And as you might have guessed - player.setvolume(0.0, 0.0) - is absolute silence.

If you want to set global volume, you can use AudioManager.setStreamVolume

Considering that the device and Android volume buttons work perfectly well without the developer having to handle the button presses, I'm not sure why you would want to override default behavior.



来源:https://stackoverflow.com/questions/25604452/how-to-set-volume-in-decibels-using-my-android-mobile-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!