I need to call setVolumeControlStream
from a service that plays some sound via STREAM_SYSTEM
.
Obviously in an Activity that is no problem, but how can I do this with a service?
Asked
Active
Viewed 4,296 times
18
2 Answers
3
From the looks of it, this isn't possible the way you are trying to do it.
As said in the android reference
The suggested audio stream will be tied to the window of this Activity
Hence with a service that has no activity, there is no window to tie the audio stream to.
However, It looks like you should be able to receive media key events (including volume keys) from a broadcaster as shown here which would allow you to change your service volume without an activity being visible.

Guykun
- 2,780
- 23
- 28
-
I've looked at this solution and it doesn't seem to work: there is no way to use broadcast receivers to receive volume keys events. On the link you posted, android.intent.action.MEDIA_BUTTON is used for playback buttons, like play/pause, next, previous, etc... But no volume buttons. – J.-B. C. Aug 23 '12 at 09:08
-
My mistake then, I guess this isn't possible from a service. The reason seemingly is therefore to stop apps capturing the volume events and interfering with the ability of the user to change ringer volume/system volume. Is there any reason why you cannot use STREAM_MUSIC? – Guykun Aug 23 '12 at 09:39
-
In my case, it's because I would like to mute other apps while my app plays music. As all other apps use STREAM_MUSIC, the solution I found was to mute STREAM_MUSIC and use another stream. – J.-B. C. Aug 23 '12 at 10:37
-
Some music apps such as MortPlayer and Audible allow you to control the media volume even when the Activity is not active. There must be another solution somehow... – antonyt Dec 07 '12 at 02:59