Getting and setting system volume
suggest changeAudio stream types
There are different profiles of ringtone streams. Each one of them has it’s different volume.
Every example here is written for AudioManager.STREAM_RING stream type. However this is not the only one. The available stream types are:
STREAM_ALARMSTREAM_DTMFSTREAM_MUSICSTREAM_NOTIFICATIONSTREAM_RINGSTREAM_SYSTEMSTREAM_VOICE_CALL
Setting volume
To get the volume of specific profile, call:
AudioManager audio = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
This value is very little useful, when the maximum value for the stream is not known:
AudioManager audio = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
int streamMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
The ratio of those two value will give 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 class to do this.Just call void setAudioStreamType(int streamtype):
MediaPlayer mMedia = new MediaPlayer();
mMedia.setAudioStreamType(AudioManager.STREAM_RING);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents