Michael B
Michael B

Reputation: 1699

Adjusting Volume in Android Audiomanager is inaccurate

in my Android Application i have a method, which allows me to adjust the volume. This works like it should.

public void adjustVolume(int adjustType){
    if(myAudioManager == null) {
        myAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    }
    if (adjustType == 0){

        myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

        Toast.makeText(getApplicationContext(), "mute audio", Toast.LENGTH_SHORT).show();
    }
    else if(adjustType == 1) {
        myAudioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
        Toast.makeText(getApplicationContext(), "decrease volume", Toast.LENGTH_SHORT).show();
    }
    else if (adjustType == 2){
        myAudioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
        Toast.makeText(getApplicationContext(), "increase volume", Toast.LENGTH_SHORT).show();
    }
    else if (adjustType == 3){
        int maxVolume = myAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
        myAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, maxVolume, AudioManager.FLAG_SHOW_UI);
        Toast.makeText(getApplicationContext(), "Max volume", Toast.LENGTH_SHORT).show();
    }

    Log.d(LOG_TAG, "Volume is " + myAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM));
}

e.g. I can call adjustVolume(1) to decrease the volume, and adjustVolume(2) to increase the volume. From Volume zero, to max volume it takes 15 steps

So far, so good.

Now I have another method, where I want to set the volume directly:

public void adjustVolumeDirect(int volumeValue) {
    if(myAudioManager == null) {
        myAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    }

    //maxVolume is 7
    int maxVolume = myAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);

    float factor = (float)volumeValue / 100;
    int targetVolume = (int) (maxVolume * factor);

    Log.d(LOG_TAG,"MAX_VOLUME = "+maxVolume+" VOLUMEVALUE = " +volumeValue+ " TARGET VOLUME = " + targetVolume + "factor = " + factor);
    myAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM,targetVolume , AudioManager.FLAG_SHOW_UI);

}

e.g. adjustVolumeDirect(100) should set the Volume to max. adjustVolumeDirect(50) should set in to 50% and so on. And this part does not work like I expect it. I can only set the volume in 7 steps! The method above gives me 15 steps!

Can anyone give me a hint how to solve this? I want to set the volume in 10-steps:

adjustVolumeDirect(10) = 10%
adjustVolumeDirect(20) = 20%
...
adjustVolumeDirect(100) = 100%

How can I achieve this?

Upvotes: 1

Views: 2508

Answers (2)

Terranology
Terranology

Reputation: 633

I've resolved the issue with this:

mAudioManager.getStreamMaxVolume(audio.STREAM_MUSIC)**/3**
mAudioManager.getStreamMaxVolume(audio.STREAM_MUSIC)**/2**

mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audio.getStreamMaxVolume(mAudioManager.STREAM_MUSIC)/3, AudioManager.FLAG_SHOW_UI);

Upvotes: -1

IngoAlbers
IngoAlbers

Reputation: 5812

The reason you have a different number of steps is probably because two different audio streams are affected - for example the system audio stream, which may have 7 steps and the media audio stream, which might have 15.

The number of steps are different for various android devices and are defined in the system. See also this issue in the android bug tracker.

There are different ways to allow a finer tuning depending on your device. You can probably find some more information here on the android stackexchange site.

Upvotes: 1

Related Questions