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

5 comments:

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

HI
Thanks,I will the above method.

A.Abdul Latheef said...

HI
Thanks,I will the above method.

Unknown said...

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

Unknown said...

this.getSystemService(Context.AUDIO_SERVICE);
im getting an error in this line ;