Is there a listener to listen for changes in the volume in android?
Is it possible to register a listener to listen for chan开发者_如何学Goges in the volume level of the music stream in Android?
I'm displaying the actual volume in a SeekBar and I would like to change the seekbar if the user changes the volume with the hardware volume keys. At the moment the correct volume is displayed until the user changes the volume with hardware keys.
This is not a perfect answer but a hack:
android.media.VOLUME_CHANGED_ACTION
I found above action in native logs while changing volume via hard volume key.
01-25 16:11:24.015: DEBUG/VolumePanel(189): onVolumeChanged(streamType: 2, flags: 4)
01-25 16:11:24.015: DEBUG/BluetoothA2dpService(189): Received intent with action: android.media.VOLUME_CHANGED_ACTION
so go ahead and register BroadcastReceiver with action "android.media.VOLUME_CHANGED_ACTION" if you don't have any other solution.
Other way of doing is; Taking over the volume key on Android .
One option would be to use registerMediaButtonEventReceiver and have your application handle the hardware keys. You could adjust the volume seekbar in your app and use AudioManager to adjust the volume.
Another possibility would be to create a service in your app that runs in the background and periodically checks the volume and adjusts your seekbar accordingly.
You can display the volume by accessing the current volume from AudioManger if your app is currently in focus. Otherwise, there is no concrete way (official way using api) of doing this.
Android recommends letting the system do all of this for you by calling setVolumeControlStream(). This will bring up a volume seekbar for the audio stream that your app is using whenever the user tries to adjust the volume with their hardware buttons.
"You may be tempted to try and listen for volume key presses and modify the volume of your audio stream that way. Resist the urge. Android provides the handy setVolumeControlStream() method to direct volume key presses to the audio stream you specify.
Having identified the audio stream your application will be using, you should set it as the volume stream target. You should make this call early in your app’s lifecycle—because you only need to call it once during the activity lifecycle, you should typically call it within the onCreate() method (of the Activity or Fragment that controls your media). This ensures that whenever your app is visible, the volume controls function as the user expects.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
From this point onwards, pressing the volume keys on the device affect the audio stream you specify (in this case “music”) whenever the target activity or fragment is visible."
From: http://developer.android.com/training/managing-audio/volume-playback.html
I've faced a similar issue. The app should show a volume off indicator in case of Media Sound has turned off during the video playback. This approach handles cases when the user changes Media level by swipe too. So, wrote a simple Rx wrapper for ContentObserver which observes Media Sound level directly ))
Gist link here
精彩评论