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?
Related
I wrote an app which starts record audio in response to holding volume up button on phone or headphone, I'm doing it inside dispatchKeyEvent method and it's fine
Problem is, I want to do it when the phone is locked in my pocket, I want to just hold my headphone volume button and start recording, How can I do that in Android? How can you do anything in response to user actions when the phone is locked? For example, how can a music player respond to the user pressing play/pause button on headphone? I know that it is possible, but I don't know how...
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
I am developing a simple app in which i am the counting of volume button press 3 times. If counter is equal to three then some action will happen.
My problem is when i press the hardware volume button when phone is locked then onReceive() method of BroadcastReceiver is not called. Its working fine when phone is unlocked.
Is there any way or trick that When i press volume up button in locked mode then my counter increase and vice versa.
I researched on this and conclusion is that Its is not possible.
But I did in some way but its not good approach because It will drain the battery too much.
I start the media player in background with zero volume. Now volume keys start working when even mobile is locked. As example Music app in which volume buttons work even when mobile is locked.
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'm playing some music from a service with Androids MediaPlayer. When the music is playing, the music volume can be changed by the volume rocker keys. However I don't want that the user can change the music volume during playback.
How can this be done?
It is possible when getting the keydown event and returning true, but that only works when the app is on top. Since I play the music from a service, it is also played when the screen is off or the app in background, so the volume can still be changed.
Another idea I had is to call
setVolumeControlStream( AudioManager.STREAM_RING );
after having started playing the music, but I can't call this from a service but only from an activity...
Or is it possible to play the music on another stream (Ring or alarm), but let the volume rocker still change the music stream?
Any ideas?
'You cannot intercept the key while your application is in background, but instead of listening to the KeyPress itself. You can register a ContentObserver'
Is there a broadcast action for volume changes?
Since I haven't found another way to do it, I check in a timer about 5 times per second if the volume has been changed. If it has, I change it back.
This is a really ugly solution, but it works and if there is no other way to do it, I have to leave it like this.
The only really horrible thing is that the volume dialog shows up when you press the volume rocker. Is there a way to disable the volume dialog?
Or do you know a better solution?