18

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.

Richard Lovejoy
  • 663
  • 10
  • 18
  • Have you seen this post? http://stackoverflow.com/questions/4593552/how-do-you-get-set-media-volume-not-ringtone-volume-in-android – Joel Nov 27 '13 at 01:53
  • Yea getStreamVolume() seems to always return the same value even after I set the volume manually with MediaPlayer.setVolume(); – Richard Lovejoy Nov 27 '13 at 02:54
  • Have you found a solution for this? AFAIK, you have to store current volume levels on your own variables. – Ricardo Dec 21 '14 at 19:19

1 Answers1

4

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.

Templerschaf
  • 142
  • 1
  • 8