Android MediaPlayer edit

Getting and setting system volume

Audio 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:

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

Table Of Contents
39 ACRA
61 MediaPlayer
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close