How to set the mediaplayer volume programmatically. I use it for alarm notification. Any help is highly appreciated and thanks in advance.
10 Answers
Using AudioManager, you can simply control the volume of media players.
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
also from MediaPlayer (But I didn't try that)
setVolume(float leftVolume, float rightVolume)
Since: API Level 1
Sets the volume on this player. This API is recommended for balancing the output of audio streams within an application. Unless you are writing an application to control user settings, this API should be used in preference to setStreamVolume(int, int, int) which sets the volume of ALL streams of a particular type. Note that the passed volume values are raw scalars. UI controls should be scaled logarithmically.
Parameters
leftVolume left volume scalar
rightVolume right volume scalar

- 868
- 1
- 15
- 34

- 108,599
- 23
- 164
- 151
-
what is the minimum volume to set with value? – Pattabi Raman Nov 23 '11 at 07:04
-
2if 0, it takes no volume. If 1, it is ringing with full volume on the device. – Pattabi Raman Nov 23 '11 at 07:13
-
What is the 20 number? I thought you said volume was from 0 to 1? – Azurespot Feb 23 '15 at 06:10
-
120 is the max volume number (0 is minimum). But I've found out this can be different per device. You can get the max volume number using audioManager.getStreamMaxVolume(streamType) and do some nice math if you'd like adjust it in a more controlled way ;-) – P Kuijpers Aug 04 '17 at 08:02
-
I personally prefer setVolume as it provides far more fine-grained control and is not dependent on the Output device (for example a bluetooth headset might have even less volume steps) – xeruf Nov 11 '17 at 16:04
-
1Note that setVolumes value range (0.0-1.0) is relative to the current media volume. So if the media volume is 0, then it won't matter what the value is, no sound is coming out :) – Abdullah Gheith Jun 17 '19 at 20:30
Hope this help
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
For Volume UP
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
for Volume Down
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);

- 8,048
- 5
- 58
- 78
-
hi dwivedi ji, can we adjust notification sound through our device's volume button ? – pioneerBhawna Aug 14 '14 at 06:11
-
Is there a way to raise the volume of the actual `MediaPlayer` file, not the phone volume? I am getting a `MediaPlayer` whose playback is very low, even if the phone volume is normal. – Azurespot Feb 23 '15 at 06:13
To Hide Volume Controll UI:
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
For Volume UP
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, 0);
for Volume Down
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, 0);

- 48
- 3

- 41
- 1
You can do the below using Kotlin
, this code will check if the media volume is more than 20% of the maximum volume of the device, and will reduce it to be 20% only.
val audio = this.getSystemService(Context.AUDIO_SERVICE) as AudioManager
val level = audio.getStreamVolume(AudioManager.STREAM_MUSIC)
val maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
val percent = 0.2f
val twintyVolume = (maxVolume * percent).toInt()
if ( level > twintyVolume) {
Toast.makeText(this,"audio level is $level", Toast.LENGTH_LONG).show()
audio.setStreamVolume(AudioManager.STREAM_MUSIC,twintyVolume,0)
}

- 22,789
- 24
- 132
- 203
Read this page right here. It explains very well. Basically, unless your app is a replacement alarm clock, you need to make the following call in the "onCreate()" function:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
In this way you can create the volume of your app using the hardware buttons.

- 51
- 2
- 8
Try This
protected static void setVolume(int volume) {
currentVolume = volume;
{
if (volume == 1) {
volume = 2;
}
try {
float vol = ((float) volume / CONSTANT.SYSTEM_MAX_VOLUME);
mediaPlayer.setVolume(vol, vol);
} catch (Exception e) {
e.printStackTrace();
}
}
}

- 668
- 1
- 14
- 24
The below code sets the volume to the maximum level (getStreamMaxVolume()).
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC,am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);

- 11,802
- 17
- 73
- 121
Code:
AudioManager mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
try
{
float count = 100 * 0.2f;
Log.d("--count_float", count + "");
Log.d("--count_final", Math.round(count) + "");
Log.d("--count_volume", new
PreferenceMotionSensor(mContext).getStreamVolume());
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, Math.round(count), 0);
}
catch (Exception e)
{
Log.d("--Error", e.getMessage());
}
Output
D/--count_float: 20.0
D/--count_final: 20
D/--count_volume: 100

- 2,702
- 3
- 32
- 54

- 248
- 2
- 12
Remember to set left and right speaker volumes.
if (System.nanoTime() == alarm){
yourMediaPlayer.setVolume(volume, volume)}
}

- 77
- 4