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.
Asked
Active
Viewed 368 times
1
-
Googled "android register app volume", result: http://developer.android.com/training/managing-audio/volume-playback.html – Eugen Pechanec Jan 21 '15 at 02:02
-
but i use android.media.MediaPlayer not Audiomanger – Kỳ Đoàn Văn Jan 21 '15 at 02:07
-
possible duplicate of [Android - Volume Buttons used in my application](http://stackoverflow.com/questions/2874743/android-volume-buttons-used-in-my-application) – GreyBeardedGeek Jan 21 '15 at 02:10
-
oh. i tried , but it deny my sound control ( can not change app volume) – Kỳ Đoàn Văn Jan 21 '15 at 02:47
1 Answers
1
Call the following right before you start playing music
setVolumeControlStream(AudioManager.STREAM_MUSIC);
and call the following after you finish
setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);
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.
Source:
http://developer.android.com/training/managing-audio/volume-playback.html
http://developer.android.com/reference/android/media/MediaPlayer.html#setAudioStreamType(int)

Eugen Pechanec
- 37,669
- 7
- 103
- 124
-
oh. it worked , thanks. even though it took another error, but i have to put these codes to my app. – Kỳ Đoàn Văn Jan 21 '15 at 08:22