Getting and setting system volume

suggest change

Audio stream types

There are different audio stream types. Each one has its own volume.

Every example here is written for AudioManager.STREAM_RING stream type. However this is not the only one. The available stream types are:

Setting volume

To get the volume of a specific stream, call:

AudioManager audio = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);

This value is of little use when the maximum value for the stream is not known:

AudioManager audio = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
int streamMaxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_RING);

The ratio of those two values gives a relative volume (0 < volume < 1):

float volume = ((float) currentVolume) / streamMaxVolume;

Adjusting volume by one step

To make the volume for the stream higher by one step, call:

AudioManager audio = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
audio.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_RAISE, 0);

To make the volume for the stream lower by one step, call:

AudioManager audio = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
audio.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_LOWER, 0);

Setting MediaPlayer to use specific stream type

There is a helper function from MediaPlayer to do this. Just call void setAudioStreamType(int streamtype):

MediaPlayer mMedia = new MediaPlayer();
mMedia.setAudioStreamType(AudioManager.STREAM_RING);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents