A search on how to set the volume in an android app yielded two methods. The first used AudioManager to set the stream volume. The second set the volume on the MediaPlayer object. After experimenting with both those methods, an important distinctions were observed.
mediaPlayer.setVolume(float left, float right)
Both parameters should be between 0.0 and 1.0 inclusive. According to the docs, these are both scalars. Passing in 1.0 for both will set the volume to the media player to be the max of the current volume for that stream.
In order to set the volume of the stream itself, using the AudioManager is necessary. For example, the media stream is turned all the way down but you want your app to make noise using that stream. Once the sound is finished, its polite to return the stream to the original volume set by the user. Resetting the volume can be accomplished by:
1. Save the original volume
2. Add a onCompletionListener to the MediaPayer object
3. In the onCompleteListener, reset the original stream volume.
Code to accomplish this would look something like this:
mp = MediaPlayer.create(context, resourceID); final AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); int maxVol = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); final int origVol = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); audioManager.setStreamVolume( AudioManager.STREAM_MUSIC, maxVol , 0); mp.setOnCompletionListener(new OnCompletionListener(){ @Override public void onCompletion(MediaPlayer mp) { audioManager.setStreamVolume( AudioManager.STREAM_MUSIC, origVol, 0); } }); } mp.start();
Note: The above will play a sound at the max volume.