136

I have been looking everywhere how to play a ringtone/alarm sound in Android.

I press a button and I want to play a ringtone/alarm sound. I could not find an easy, straightforward sample. Yes, I already looked at Alarm clock source code... but it is not straightforward and I cannot compile it.

I cannot make this work:

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(this, alert);
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
    player.setAudioStreamType(AudioManager.STREAM_ALARM);
    player.setLooping(true);
    player.prepare();
    player.start();
}

I get this error:

04-11 17:15:27.638: ERROR/MediaPlayerService(30): Couldn't open fd for
content://settings/system/ringtone

So.. please if somebody knows how to play a default ringtone/alarm let me know.

I prefer not to upload any file. Just play a default ringtone.

Melquiades
  • 8,496
  • 1
  • 31
  • 46
Federico
  • 1,369
  • 2
  • 9
  • 3

13 Answers13

210

You can simply play a setted ringtone with this:

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
markov00
  • 3,682
  • 2
  • 24
  • 25
  • 3
    I still get an error - Failed to open ringtone content://settings/system/alarm_alert – Bilbo Baggins Feb 10 '13 at 19:19
  • 3
    Nice and simple. However, depending on the device, this method may interrupt other sounds (like music) that might be playing in Android. – igordc Nov 24 '13 at 17:46
  • Using getApplicationContext() might not be a very good option. More info here: http://stackoverflow.com/questions/9122627/should-i-use-getapplicationcontext-or-activity-this-in-a-long-running-asynctask – Saket Jun 12 '14 at 08:10
  • @BartSimpson how u resolved issue i m also getting this error – user3233280 Oct 26 '14 at 11:22
  • I tried this but the sound may get cut off. I used MediaPlayer as igordcard@ answered it worked perfectly. – wyz Mar 11 '15 at 07:15
  • Ringtone doesn't routed to bluetooth headset :( – Anuj Jain Aug 31 '15 at 10:21
  • 2
    Ringtone can not stoppable. If start ringtone again, plays double. stopPrevious not working, by the way I create ringtone player with the same context object, not getapplicationcontext. – Metehan Toksoy Dec 24 '15 at 18:11
  • may be change context or get system service for ringtone – blackHawk Jun 09 '17 at 14:59
  • @MetehanToksoy, you have to keep track of the `Ringtone` instance, for example, as a class variable. Then you can do something like `myRingtone.stop()` later on. – wildcat12 Jun 23 '17 at 21:55
68

If a user has never set an alarm on their phone, the TYPE_ALARM can return null. You can account for this with:

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

if(alert == null){
    // alert is null, using backup
    alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    // I can't see this ever being null (as always have a default notification)
    // but just incase
    if(alert == null) {  
        // alert backup is null, using 2nd backup
        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);                
    }
}
Melquiades
  • 8,496
  • 1
  • 31
  • 46
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • 1
    The URI returned might not be `null` even though it does not point to a valid sound. You should test the return value of `RingtoneManager.getRingtone()` for `null` instead/as-well – Attila Jan 04 '13 at 18:07
  • In 2017, not working failing to ring. Do you have it working in recent Android? –  Jun 28 '17 at 08:02
66

This is the way I've done:

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.start();

It is similar to markov00's way, but uses MediaPlayer instead of Ringtone which prevents interrupting other sounds, like music, that might already be playing in the background.

igordc
  • 1,525
  • 1
  • 14
  • 20
  • 5
    I tried the top answer (ringtone.play) but the sound may get cut off. I used this approach and it worked perfectly. – wyz Mar 11 '15 at 07:16
  • 1
    This is a better solution for anyone using any other audio components in their app. – EntangledLoops Aug 12 '15 at 00:11
  • @YumYumYum, I just tested and it works. I did nothing but put the above code into my setOnClickListner. What did you do? – feature sky Jun 30 '17 at 13:37
17

Your example is basically what I'm using. It never works on the emulator, however, because the emulator doesn't have any ringtones by default, and content://settings/system/ringtone doesn't resolve to anything playable. It works fine on my actual phone.

synic
  • 26,359
  • 20
  • 111
  • 149
15

For the future googlers: use RingtoneManager.getActualDefaultRingtoneUri() instead of RingtoneManager.getDefaultUri(). According to its name, it would return the actual uri, so you can freely use it. From documentation of getActualDefaultRingtoneUri():

Gets the current default sound's Uri. This will give the actual sound Uri, instead of using this, most clients can use DEFAULT_RINGTONE_URI.

Meanwhile getDefaultUri() says this:

Returns the Uri for the default ringtone of a particular type. Rather than returning the actual ringtone's sound Uri, this will return the symbolic Uri which will resolved to the actual sound when played.

ulmaxy
  • 820
  • 2
  • 14
  • 22
13

This works fine:

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
MediaPlayer thePlayer = MediaPlayer.create(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

try {
    thePlayer.setVolume((float) (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) / 7.0)),
                        (float) (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) / 7.0)));
} catch (Exception e) {
    e.printStackTrace();
}

thePlayer.start();
Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
12

It may be late but there is a new simple solution to this question for who ever wants it.
In kotlin

import android.provider.Settings
val player = MediaPlayer.create(this,Settings.System.DEFAULT_RINGTONE_URI)
player.start()

Above code will play default ringtone but if you want default alarm, change

Settings.System.DEFAULT_RINGTONE_URI

to

Settings.System.DEFAULT_ALARM_ALERT_URI

Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
Reza
  • 845
  • 13
  • 18
9

You can push a MP3 file in your /sdcard folder using DDMS, restart the emulator, then open the Media application, browse to your MP3 file, long press on it and select "Use as phone ringtone".

Error is gone!

Edit: same trouble with notification sounds (e.g. for SMS) solved using Ringdroid application

OcuS
  • 5,320
  • 3
  • 36
  • 45
5
public class AlarmReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        //this will update the UI with message
        Reminder inst = Reminder.instance();
        inst.setAlarmText("");

        //this will sound the alarm tone
        //this will sound the alarm once, if you wish to
        //raise alarm in loop continuously then use MediaPlayer and setLooping(true)
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alarmUri == null) {
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        }
        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
        ringtone.play();

        //this will send a notification message
        ComponentName comp = new ComponentName(context.getPackageName(),
                AlarmService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}
Kumar sunny
  • 51
  • 1
  • 1
3

You could use this sample code:

Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone ringtoneSound = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri)

if (ringtoneSound != null) {
    ringtoneSound.play();
}
Gio MV
  • 105
  • 1
  • 8
2

Copying an audio file to the sd card of the emulator and selecting it via media player as the default ringtone does indeed solve the problem.

Valentin Klinghammer
  • 1,319
  • 1
  • 13
  • 19
1

You can use the below code;

val ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
// for alarm
// val ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
val ringtoneSound: Ringtone = RingtoneManager.getRingtone(requireContext(), ringtoneUri)
    ringtoneSound.play()

for stop;

ringtoneSound.stop()
Ahmet B.
  • 1,290
  • 10
  • 20
-3

Here's some sample code:

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), notification);
mediaPlayer.start();
lilott8
  • 1,116
  • 2
  • 17
  • 41
Nishant
  • 13