Android, control the volume of a background sound - android

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).

Related

Increase volume of speakerphone over the limit Android

I use this code to set the maximum volume of the speaker.
AudioManager audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
audioManager.setSpeakerphoneOn(false);
I would increase more this volume, but how can i do? I tried to use this:
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,2,0);
But nothing, the volume remain set to the same level.
An app as this: https://play.google.com/store/apps/details?id=com.mxtech.videoplayer.ad, for example, allow you to increase the volume to the 300%.
Any help?
Thank you.
What this app probably does is increase the volume of the file it is playing like for example the vlc player does when you increase the volume beyond 100%. It has nothing to do with the volume you set to the phone, it just compensates for files which have a more quiet audio track.
EDIT:
Of the top of my head I don't know of an easy solution, I imagine it would be very difficult to write such a functionality yourself. That's why I suggest you look for a library such as this:
https://code.google.com/p/musicg/
Or you can look at open source projects which implement this functionality or at least something similar such as this:
https://code.google.com/p/ringdroid/
But I fear even if you use musicg or find some bits of code in ringdroid which help you it's probably still not going to be easy.

Issue with volume buttons in android app

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);

API to record audio with specific volume on Android

I want to make an app to record using MediaRecorder with certain volume set by user.
Is there any possibility to set the volume when recording? I try to see in MediaRecorder API but i can't find setVolume or something like that..
Is there any work around? Or any post process that i have to do to make it happen?
Thanks.
You can not do it while recording, but you can set the volume while playing with the setVolume(float, float) method on MediaPlayer.

Lower system volume

I want to lower the ringtone, media volume, and alarm volume from an app (button press, no slider). So I've been reading up on AudioManager , but I'm still a bit confused on how to do this.
Also, with Audio Manager, is the volume set for the whole phone or just for the app? I want it to be for the whole phone
but I'm still a bit confused on how to do this
Call adjustStreamVolume() with your desired stream (e.g., STREAM_ALARM) and change (e.g., -1). Here is a sample project that demonstrates this.
is the volume set for the whole phone or just for the app?
Android does not have a concept of app-specific volume, so this is for the whole phone.

Android MediaPlayer ignores it's internal volume when the system volume changes

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.

Categories

Resources