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);
Related
How can I control the volume of a background sound?
I'm making a game, and in some states I want to increase or decrease the background sound.
I have tried some things with MediaPlayer and SoundPool, but it did't do what I wanted.
I dont want to change the phone volume, but the volume of the sound. Is this possible? If yes how?
Cheers
You haven't told use what "some things with MediaPlayer and SoundPool" means, or shown us any code, so it's hard to say exactly what the problem might be.
If you want to make the volume keys modify the volume of your app's music rather than the voice call volume, you might be missing a call to setVolumeControlStream (e.g. setVolumeControlStream(AudioManager.STREAM_MUSIC);).
Refer to "Controlling Your App’s Volume and Playback" in the Android developer documentation for more information.
If you're using the MediaPlayer setVolume method, then keep in mind that the maximum volume you can set is 1.0 (i.e. the original volume of the audio data you're playing).
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.
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.
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
If I add two Fragment to one Activity, each playing different media, is there a way to mute the sound coming from one Fragment while the other plays full volume?
From my research, it seems you can only control the devices volume with code, thus if two media are playing, they'll inherit the same volume.
Any idea about getting around this?
you can set volume for every mediaplayer. api