TAGS :Viewed: 4 - Published at: a few seconds ago

[ How can i use the the physical volume button on the phone to change the volume of applications? ]

I use android.media.MediaPlayer to play some sounds in my app, but when i press the physical volume button, it only changes the system volume (ringtone volume). I searched in google but i haven't had any idea yet. I use android.media.MediaPlayer not Audiomanger.

Answer 1

Call the following right before you start playing music


and call the following after you finish


This makes your volume rocker control all music streams and then resets it to default behavior (depends on what's happening).

Don't forget to call myMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC) before myMediaPlayer.prepare(). Replace STREAM_MUSIC in both calls with some other value if it fits the played sound better. This makes the music you play conform to the music volume set in system, which is now controlled by the volume rocker.