Zbarcea Christian
Zbarcea Christian

Reputation: 9548

AudioManager getVolume custom method

My getVolume() method is always returning me 1.0. I have muted my phone, changed the volume but I am getting 1f anytime when I call my method. Am I missing something?

btnPlay.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v){
      float volume = getVolume();
      Log.i("inf", "Volume: " + volume);
      soundPlayID = soundPool.play(my_sound, volume, volume, 1, 0, 1f);
   }
});

private float getVolume(){
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    float maxVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    float volume = actualVolume / maxVolume;

    return volume;
}

Console:

Volume: 1.0

Volume: 1.0

Volume: 1.0

Volume: 1.0

Upvotes: 0

Views: 302

Answers (1)

MiStr
MiStr

Reputation: 1223

The maxVolume here appears to be the SAME as the volume. The calls are identical, and so you will always get 1.0 as a result.

You need to call:

audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

and then you should see more interesting results.

Upvotes: 2

Related Questions