I know volume changes by monitoring the system broadcast, but how can I determine if the volume is modified by the user through the volume button or settings or by other applications?
try using handling media buttons, alongside with volume changes broadcast.
If there is volume change, but not key event - app changed volume, if there is volume change and key event - user changed volume using buttons.
https://developer.android.com/guide/topics/media-apps/mediabuttons
Related
I would like to control my app with a headset keys (volume +, middle key, volume -).
I have no problem detecting media keys:
play/pause, triggered by click on middle
next/previous, triggered by volume buttons long press
I do have a problem detecting (and ideally intercepting) everything else. I am especially interested in volume keys.
!! Solutions with onKeyDown or listening to MEDIA_BUTTON or listening to volume changes (e.g. via AudioService) work with phone volume keys, but not with bluetooth volume keys !!
More info:
(just as info) In activity on onKeyDown I do not see headset key presses (it only picks up clicks on phone volume rocker).
I cannot infer clicks from volume changes, as there is no trigger if the device is at max/min volume. Also the volume dialog does not show up if I try to set above max volume with headset, as it does with phone volume buttons.
In debugger, the only thing that happens upon vol+ on max volume is some action reported from avrcp in debug mode:
07:04:08.220 7106-7250/? I/BluetoothAvrcpServiceJni: btavrcp_volume_change_callback
07:04:08.220 7106-30330/? V/Avrcp: MSG_NATIVE_REQ_VOLUME_CHANGE: volume=127 ctype=13
07:04:08.281 5937-13180/? I/EDMNativeHelperService: isAVRCPProfileEnabled
07:04:08.285 7106-7250/? I/BluetoothAvrcpServiceJni: btavrcp_volume_change_callback
07:04:08.285 7106-30330/? V/Avrcp: MSG_NATIVE_REQ_VOLUME_CHANGE: volume=127 ctype=15
How could I get these events with my service? Or is there any useful alternative to detect such headset clicks?
When you push the volume up or down key on the side of your phone, a volume notification pops up on the screen notifying you of the volume change. What is like to know is, could you make an app that replaces that notifying screen? Without making your app just appear over it?
I play a song in background, and the screen is off now. I press the volume key, the volume of the music stream changes. How can it happen? I have searched for a long time for a solution about how to detect the volume keys press event when screen off, but I don't know how to solve the problem so far.
you need a BroadcastReceiver that listens to the according broadcasts. see a similar question here.
If you wish to control the volume of audio played through your app, make use of setVolumeControlStream() method. It directs volume key presses to the audio stream you specify.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
I have a music player service , using a MediaPlayer set to stream type STREAM_RING. It works fine when i change volume pressing the volume keys as long as the screen is not locked or off. How do i set my service to respond to these keys even in locked screen mode? Thanks.
Based off this question, it looks like it's not possible to capture volume up/down presses when the screen is locked. Android capturing volume up/down key presses in broadcast receiver?
Is it possible to register a listener to listen for changes 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