Friday, July 2, 2010

Setting Android volume programmatically to maximum

I searched a bit how to the volume of an Android application to maximum, and I saw few post regarding people asking about MediaPlayer.setValue function.

It's not the right function for setting the volume (see the help).
To set the volume to maximum (or any relative value):

// Get the AudioManager
AudioManager audioManager =
(AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
// Set the volume of played media to maximum.
audioManager.setStreamVolume (
AudioManager.STREAM_MUSIC,
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
0);

4 comments:

Mihir Palkhiwala said...
This comment has been removed by the author.
Latheef a said...

HI
Thanks,I will the above method.

Latheef a said...

HI
Thanks,I will the above method.

Vaibhav Khandekar said...

(AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
hows me an error. i am not using onCreate function.