Set the volume of an android application - android

In order for the user to control the volume , my android application has a menu consisting of a slider that provides int values from 0 to 10 , when dragged. After I obtain a value , I must set the volume to the corresponding value chosen by the user , and well , this is the part that I don't know to implement and I 'd like to find about it.

Use the AudioManager class. Essentially the code goes as follows:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(streamType, volume, flags);
The problem is that the volume of the device isn't necessarily mapped from 0 to 10 as you have in your slider. On my emulator, it's from 0 to 7. So what you need to do is getStreamMaxVolume(...) to know what your max is, and then work out your value as a fraction of that. As an example, if your user chooses volume 8 out of 10, that's equivalent to 0.8 * 7 = 5.6, which you should round to 6 out of 7.
The "stream" refers to things like ringer volume, notification volume, music volume, etc. If you want to change the volume of the ringer, you need to make sure all your commands have AudioManager.STREAM_RING as the streamType.

Related

how to control the sound volume of my own android app

I imagine the answer to this question must be something quite typical. The point is that I have some buttons that play sounds when you click on them. I want to control the sound volume. How do I control the sound volume? I am using the following code:
private fun playSound() {
val sound = getSystemService(Context.AUDIO_SERVICE) as AudioManager
sound.playSoundEffect(AudioManager.FX_KEY_CLICK, 1f)
}
https://developer.android.com/reference/android/media/AudioManager#playSoundEffect(int,%20float)
volume float: Sound effect volume. The volume value is a raw scalar so UI controls should be scaled logarithmically. If a volume of -1 is specified, the AudioManager.STREAM_MUSIC stream volume minus 3dB will be used. NOTE: This version is for applications that have their own settings panel for enabling and controlling volume.
So you need to adjust that second parameter. I haven't used it, but it says that's a scalar so I'd imagine 1 is full volume? It doesn't mention any other value as a max constant or anything. -1 gives you a sound a little quieter than the current media volume setting, if that's convenient. You'll need to play around with different values between 0 and 1 (I assume!) and see what you need

Android - Get max safe stream volume

I have a use case to change stream volume programmatically, but on newer android volume, raising the volume above a certain limit (60% as per my observations which corresponds to step 9 on most phones) results in a warning dialog:
Listening at high volume for a long time may damage your hearing. Tap OK to allow the volume
to be increased above safe levels
Cancel OK
I couldn't find any documentation about this in the the android developer portal, all I could find are some random articles citing the European regulations like this one:
According to regulations set by the European Committee for Electrotechnical Standarisation (CENELEC), all electronic devices capable of media playback sold after February 2013 must have a default output volume level of a maximum 85 dB. Users can choose to override the warning to increase the volume to a maximum of 100 dB, but in doing so the warning must re-appear after 20 hours of music playback.
So I need to figure out reliably what that number is, so I don't ever result in a volume change that would show this dialog, but I also don't want to just use step 9 as the max volume and, then find out that it's not the right value for another phone. Does the android API expose the max safe stream volume anywhere? If not, then do they at least document the step number that corresponds to it for different phone?
Thanks!
There's a resource which holds the safe volume step: config_safe_media_volume_index
// .../overlay/frameworks/base/core/res/res/values/config.xml
<integer name="config_safe_media_volume_index">7</integer>
It is defined HERE
And it is used HERE
You can get it dinamically via:
int safeVolumeStep;
int safeVolumeStepResourceId =
getResources().getIdentifier("config_safe_media_volume_index", "integer", "android");
if(safeVolumeStepResourceId != 0) {
safeVolumeStep = getResources().getInteger(safeVolumeStepResourceId);
} else {
Log.w("TESTS", "Resource config_safe_media_volume_index not found. Setting a hardcoded value");
// We probably won't fall here because config_safe_media_volume_index is defined in the AOSP
// It not a vendor specific resource...
// For any case, try to set the safe step manually to 60% of the max volume.
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
safeVolumeStep = (int) (maxVolume * 0.6f);
}
Log.d("TESTS", "Safe Volume Step: " + safeVolumeStep +
" Safe volume step resourceID: " + Integer.toHexString(safeVolumeStepResourceId) );
I tested here in a Galaxy S10 and I'm getting 9.

Android - Setting in-call volume has no effect

My app launches a Skype call. Before the call is made I want to ensure the in-call volume is set to 75% of the maximum. This is what I've tried ...
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int curVol = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
int maxVol = audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
int newVol = <do some calculations>
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, newVol, 0);
<launch Skype call>
However, I'm finding that:
getStreamVolume always returns 5, irrespective of that the volume is actually set to on the device
setStreamVolume has no effect, no matter what value for newVol I use
Anyone know what I might be doing wrong? Also, anyone any idea of what the 'flags' argument for setStreamVolume is used for? It's not described in the API docs.
Thanks
Damian.

Android Media Player setVolume Issues

Until now, I was setting my MediaPlayer volume by setting the stream volume. I don't want to do that anymore because it messes with user settings. I now take the value from a SeekBar (0 to 100) and do valueFromSeekBar / 100 to get a float between 0 and 1 to use in MediaPlayer.setVolume(float, float).
The problem is that the volume level doesn't seem to change. Here is how I set up the MediaPlayer:
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.prepare();
float alarmVolume = AudioUtils.getMediaPlayerScaledVolume(100, alarm.volume);
if(NetworkUtils.isInCall(context)) {
alarmVolume = IN_CALL_VOLUME;
}
mediaPlayer.setVolume(alarmVolume, alarmVolume); //I've even tried hardcoding 0.1f
No matter what I do, it seems like the value I put in MediaPlayer.setVolume gets ignored, and the volume of the stream (in this case the alarm stream) gets used instead. It's most noticeable when the stream volume is set to max, and I play two audio files, one with MediaPlayer.setVolume(1f, 1f) and the other with MediaPlayer.setVolume(0.01f, 0.01f). They are almost indistinguishable from one another. I need a way for my users to be able to position the SeekBar at 1 and get a barely audible sound, or at 100 and have the max sound. Is this possible or am I gonna have to go back to messing with streams?
Set volume:
it will set maximum value(100) to Alarm Stream.
amanager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
amanager.setStreamVolume(AudioManager.STREAM_ALARM, amanager.getStreamMaxVolume(AudioManager.STREAM_ALARM), AudioManager.FLAG_PLAY_SOUND);
Can it be you have two objects "player" and "mediaPlayer"? Here I just used that API, and it works as was to be expected.

Changing volume for one alarm sound only on Android

I am working on a life saving medical app and if the user is in a life threatening situation, they need to hear the alert.
When I have a notification to the status bar or have a dialog appear for a critical message to the user, I need to get their attention. If the media volume or ringer volume is low or off, I want to override it for my alert only. I would prefer not to change the settings for the phone, just for my one sound that I want to play.
When I try:
AudioManager audioManager = (AudioManager)getSystemService(this.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_RING,
audioManager.getStreamMaxVolume(AudioManager.STREAM_RING), 0);
This correctly sets the volume for my stream but has the side effect of changing the stream volume for everyone else.
Is there a way of setting the volume for one song only?
It could be set back after the song is done.
AudioManager audioManager = (AudioManager)getSystemService(this.AUDIO_SERVICE);
int current_volume=audioManager.getStreamVolume(AudioManager.STREAM_RING);
audioManager.setStreamVolume(AudioManager.STREAM_RING,
audioManager.getStreamMaxVolume(AudioManager.STREAM_RING), 0);
// Play here
audioManager.setStreamVolum(AudioManager.STREAM_RING,current_volume,0);

Categories

Resources