6

I m using the MediaPlayer to play one of the internal alarm ringtone. i m using the setVolume(1.0f, 1.0f) to maximize the volume when the ringtone is played. but the ringtone doesn't play full volume ( when I compare it to playing the ringtone separately or through the built it android alarm)

here is my code

mediaPlayer.setDataSource(context, ringtoneUri);
mediaPlayer.setLooping(looping);
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.prepare();
mediaPlayer.start();

I added the following permission android.permission.MODIFY_AUDIO_SETTINGS ( not sure if this is needed )

Any Idea why the mediaPlayer still won't play the sound at maximum?

Sammy
  • 4,538
  • 8
  • 33
  • 52

3 Answers3

13

Here is the solution I found.

AudioManager amanager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = amanager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
amanager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolume, 0);

MediaPlayer mediaPlayer= new MediaPlayer();

mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); // this is important.

mediaPlayer.setDataSource(context, ringtoneUri);
mediaPlayer.setLooping(looping);  
mediaPlayer.prepare();
mediaPlayer.start();
Sammy
  • 4,538
  • 8
  • 33
  • 52
  • excellent answer, thanks a bunch!!! -> if you don't know how to set a ringtoneUri from a raw mp3 this might be handy: https://stackoverflow.com/a/20111291/2135941 – SanThee Jul 28 '18 at 14:44
10

I encountered the same issue, and then noticed this this in the MediaPlayer documentation:

While in the Prepared state, properties such as audio/sound volume, screenOnWhilePlaying, looping can be adjusted by invoking the corresponding set methods.

Calling setVolume after calling prepare fixes this, so that audio is played at max volume. Actually, according to the docs I just quoted, you should call setLooping after prepare as well:

mediaPlayer.setDataSource(context, ringtoneUri);
mediaPlayer.prepare();
mediaPlayer.setLooping(looping);
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.start();
shadowmatter
  • 1,352
  • 2
  • 18
  • 30
0

Since setAudioStreamType() is now deprecated you should use the method setAudioAttributes() instead. Below is the full example

var mediaPlayer: MediaPlayer = MediaPlayer()
fun playAudio(audioUrl: String) {

        mediaPlayer.apply {
            if (isPlaying) {
                stop()
                reset()
                release()
            }
        }

        mediaPlayer = MediaPlayer()
        try {

            mediaPlayer.apply {
                setAudioAttributes(
                    AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                        .setUsage(AudioAttributes.USAGE_ALARM)
                        .build()
                )
                setVolume(2f,2f)
                setDataSource(audioUrl)
                prepare()
                start()
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
slaviboy
  • 1,407
  • 17
  • 27