Calling setVolumeControlStream from a Service

2019-03-17 23:12发布

问题:

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?

回答1:

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.



回答2:

I think this library can provide you what you need:

http://code.google.com/p/media-volume-control/