Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MediaPlayer get volume

I'm interested in retrieving the current volume of a MediaPlayer, but don't see any method like 'getVolume()' on it. There is a setVolume(), so I'm a little confused as to why they don't also provide a way to read this property.

I saw some other answers that suggested to use AudioManager to read the volume:

    int volumeLevel = am.getStreamVolume(AudioManager.STREAM_MUSIC);    
    int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

    float volume = (float)volumeLevel/maxVolume;

However, this seems to return the device volume for all music, and not the volume that I'm setting with 'setVolume()'... is there a way to do get the volume directly from a MediaPlayer? I was thinking perhaps I need to do something with getAudioSessionId(). Thanks.

like image 1000
Richard Lovejoy Avatar asked Nov 27 '13 01:11

Richard Lovejoy


Video Answer


1 Answers

As far as I know this is not possible with the default MediaPlayer. You need to extend it and add your own getVolume() method along with a variable tracking the current volume.

like image 56
Templerschaf Avatar answered Sep 28 '22 14:09

Templerschaf