audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) returns 0

Rohan Kandwal picture Rohan Kandwal · Jul 27, 2013 · Viewed 7.4k times · Source

I am making an Speaking caller name application which pause the ringtone before speaking the caller name using TextToSpeech. I am detecting the current Volume using

int musicVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

Mostly this returns 0 but sometimes 15 the correct value. The phone is in normal mode and the volume of phone is full. I need this value to correctly speak the caller name but this unpredictable behaviour is making me crazy.

What wrong am i doing? Isn't this the correct way to detect ringtone volume?

Answer

Glaicon picture Glaicon · May 23, 2014

It places the example below that it functions, volume is of 0 to the 15.

public void pegarVolume(){

    AudioManager AudioManager = (AudioManager) getSystemService (Context.AUDIO_SERVICE);
    int musicVolume = AudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
}