0

I'm playing some music from a service with Androids MediaPlayer. When the music is playing, the music volume can be changed by the volume rocker keys. However I don't want that the user can change the music volume during playback.

How can this be done?

It is possible when getting the keydown event and returning true, but that only works when the app is on top. Since I play the music from a service, it is also played when the screen is off or the app in background, so the volume can still be changed.

Another idea I had is to call

setVolumeControlStream( AudioManager.STREAM_RING );

after having started playing the music, but I can't call this from a service but only from an activity...

Or is it possible to play the music on another stream (Ring or alarm), but let the volume rocker still change the music stream?

Any ideas?

casperOne
  • 73,706
  • 19
  • 184
  • 253
DominicM
  • 2,186
  • 5
  • 24
  • 42
  • why would you like to prevent this? Sounds like a nice feature to be able to adjust the sound level? – vidstige May 15 '12 at 12:27

2 Answers2

0

'You cannot intercept the key while your application is in background, but instead of listening to the KeyPress itself. You can register a ContentObserver'

Is there a broadcast action for volume changes?

Community
  • 1
  • 1
Broak
  • 4,161
  • 4
  • 31
  • 54
  • This works, but the onChange method of the ContentObserver is only called after the dialog showing the new volume has disappeared. So you can still change the volume, the volume dialog will show up with the new volume and when it disappears, you can reset the volume. This isn't exactly what I was looking for... Can you maybe prevent the music volume dialog from showing up so onChange is called immediately? – DominicM May 15 '12 at 12:44
0

Since I haven't found another way to do it, I check in a timer about 5 times per second if the volume has been changed. If it has, I change it back.

This is a really ugly solution, but it works and if there is no other way to do it, I have to leave it like this.

The only really horrible thing is that the volume dialog shows up when you press the volume rocker. Is there a way to disable the volume dialog?

Or do you know a better solution?

DominicM
  • 2,186
  • 5
  • 24
  • 42