Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MediaPlayer volume is very low ( already adjusted Volume )

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?

like image 454
Sammy Avatar asked Nov 26 '11 14:11

Sammy


People also ask

How to change volume Android studio?

By default, pressing the volume control modifies the volume of the active audio stream. If your app isn't currently playing anything, hitting the volume keys adjusts the music volume (or the ringer volume before Android 9). Unless your app is an alarm clock, you should play audio with usage AudioAttributes.

What is MediaPlayer in Android?

android.media.MediaPlayer. MediaPlayer class can be used to control playback of audio/video files and streams. MediaPlayer is not thread-safe. Creation of and all access to player instances should be on the same thread. If registering callbacks, the thread must have a Looper.


1 Answers

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();
like image 143
Sammy Avatar answered Oct 20 '22 00:10

Sammy