I need to use volume buttons in my application while sleep mode is active( black screen, locked).
I am using onDispachKeyEvent to capture volume buttons and it works perfectly while in normal state.
In music player if you close the screen, you can still change volume. Any idea how is done ?
Thanks
I've recently experimented quite a lot with that at my HTC Desire (and this got confirmed at several phones from another vendors).
The summary is the following: when screen is off – the volume buttons are listened by system only in case music is currently being played.
Even in this case you wouldn't get volume button key press events in Activity when the screen is off - system just modifies the volume level of AudioManager.STREAM_MUSIC when music is playing (and other corresponding audiostreams in different situations: alarm, phone call) - and doesn't do it when there's no music/alarm/call
Related
I have integrated chromecast in my app.When video is running on tv i am able to change volume by clicking on chromcast icon which shows dialog box which allows to adjust volume.But i am not able to adjust the volume through my mobile device volume keys which are working fine when playing video locally.How can I program it in such a way that i am able to adjust volume through volume up down keys of mobile?
If your app is in front, then your activity can override dispatchKeyEvent() and use the Cast SDK Apis to increase or decrease the volume:
Cast.CastApi.setVolume(mApiClient, volume)
If your app is not in front, then you need to use MediaRouter with either MediaSession or RemoteControlClient to achieve that; that is more involved , you can take a look at this library to see how it does that.
My app plays sounds, but the volume buttons only seem to work if the sound is playing. Otherwise if you hit the volume buttons they change the ringer volume instead of the media volume. How can I make it so that while the app is running, hitting the volume buttons always change the media volume? Or is this considered bad practice?
Call this within your activities onCreate().
setVolumeControlStream (AudioManager.STREAM_MUSIC);
I made an app which has several sounds implemented with soundPool, like sounds when user is pressing buttons etc.
But they are short, so if i press hardware button "volume -" on my phone - of course, my phone volumes down sound of incoming calls and not the sound of my SoundPool(I'm not sure if this is the same volume as MediaPlayer uses).
How can I lock my phone's volume hardware buttons (or any volume controls buttons) so by pressing them I could adjust SoundPoool's volume?
Use setVolumeControlStream() to "bind" volume keys to audio stream you use in your app.
Google docs states if an app isn't playing anything, hitting the volume keys adjusts the ringer volume.
see http://developer.android.com/training/managing-audio/volume-playback.html#HardwareVolumeKeys
I'm sure it is possible to change this 'default' behavior something besides STREAM_RING (say STREAM_MUSIC) by creating custom build from source, so that if the user is at, say the Launcher, and presses the volume button the the STREAM_MUSIC volume will change.
(This require is because the device doesn't have a phone so ring volume is a no-op).
I've looked at the Launcher code but it doesn't show (at least not that I can see) that volume buttons are directed/consumed via AudioManager to STREAM_RING.
Also when in 'Settings' the VOLUME buttons change STREAM_RING, which leads me to further believe there is something 'Up the chain' or 'Down the stack in Application Frameworks or in the Android Runtime'.
Is there a 'higher' Activity or system process running that handles the dispatch of VOLUME to the AudioManager with the STREAM_RING as the channel?
Or is there something within AudioManager that could be set so that it processes VOLUME changes to the STREAM_MUSIC (this seems unlikely from the source as it appears to only process the STREAM in the context of an applications preferred STREAM.
The source I'm using is Ice Cream Sandwich 4.0.3.
The solution appears to be setting "config_voice_capable" to false.
This value is found in frameworks/base/core/res/res/values/config.xml
I used an overlay to set the "config_voice_capable" to false, then built the rom.
After the device boots the volume controls no longer default to the RINGER stream.
I was not able to find documentation on this regarding differences between
tables and phone capable devices, but the solution works.
here is my situation:
I have a media player playing music in an Android application. I've found that with certain headphones, the volume is much too loud even when the volume is set to it's lowest setting. As a result, I want to change the volume of the music for all volume levels to be 10% of what it normally is (actually, this value is user-defined of course).
The following works perfectly:
mediaPlayer.setVolume(0.1f, 0.1f);
The volume of the music is now at a good level for listening. However, if the user now changes the volume using the volume rocker (thus changing the music stream volume), the media player changes the volume as expected, but it also seems to reset the 'setVolume' parameters to 1.0, causing a massive volume change. Setting the volume back to 0.1 sets the volume to how it should be (which is 10% of the current music stream volume).
To quote the Android docs for the MediaPlayer.setVolume method:
This API is recommended for balancing the output of audio streams within an application
How can you do this if it gets reset to 1.0 each time the system volume changes?
Any help muchly appreciated. Thanks.
My suggestion would be to try overriding the onKeyDown(...) and manually adjust the volume by getting the AudioManager object using:
getSystemService(AUDIO_SERVICE).adjustVolume(ADJUST_RAISE);
or whatever method you are using to change the volume. By overriding the volume rocker button behaviors, you'll remove any default behaviors that are causing problems.