4
votes

I'm working on a music app which consists in a Service playing music while other apps are running.

In order to enjoy the music and only the music, the Service mutes all the other apps. In order to do that, it mutes STREAM_MUSIC and play music over STREAM_VOICE_CALL (found that solution to mute other apps here)

As the Service uses STREAM_VOICE_CALL to play music, what I'm trying to find is a way to make the volume buttons control this stream when a sound is playing.

What I already tried:

  • setVolumeControlStream: only works in Activities
  • Capturing volume keys pressed events: Services do not receive such events (source)
  • audioManager.setMode(AudioManager.MODE_IN_CALL) : does not work in my case. Plus, the SDK documentation states that this "mode should only be used by the telephony application when it places a phone call".

At this point, I'm out of options and any help would be appreciated. Thanks!

2
What kind of sound are you trying to play? Something through the earpiece or something through the speaker? Is there a reason you need to use STREAM_VOICE_CALL? Can you not use STREAM_MUSIC?Nick
The service is part of a music player, the sounds are musics. The service also mutes STREAM_MUSIC, in order to mute other apps so that the music won't scramble with sounds, this is why I use another stream.J.-B. C.
By choosing to mute all other apps and forcing yourself to use another stream, you're making things too difficult for yourself. The user will not be able to detect if an event happens that only plays a sound if they are listening. Personally, I would not want a solution like that.tolgap
I would agee with you if I muted every stream. But the only stream that is muted is STREAM_MUSIC, so notifications can still be heard.J.-B. C.

2 Answers

3
votes

I would personally not suggest you to STREAM_VOICE_CALL as it is meant to be recommended to use it for Voice calls as compared to media playback. Using this may prevent you from using features exposed by Android for media playback.

Having said this & taking into consideration your use-case, I think you need to look for changing 'call volume' level as compared to 'media volume' level.

Have a look at the MediaPlaybackService.java as used in the stock Music Player app and try to customize it as per your needs.

-1
votes

I havn't tried it myself. but looks like it should work. Kindly try it out.

m_audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
m_audioManager.setMode(AudioManager.MODE_IN_CALL);