How to play ringtone/alarm/notification sound in Android

Playing the default notification sound of the device is the most effective way to notify a user. This can be established by getting the Uri of the audio file from RingtoneManager. To play default alarm tone , ringtone or notification sound from an android app, the following code snippet is useful.

Play Notification Tone

try {
 Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
 Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
 r.play();
 } catch (Exception e) {
 e.printStackTrace();
 }

Play Alarm Tone

To play an Alarm-tone change TYPE_NOTIFICATION to TYPE_ALARM.

try {
 Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
 Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
 r.play();
 } catch (Exception e) {
 e.printStackTrace();
 }

Set & Play a Tone from Raw resources folder

To set a file from the raw folder of your app just change the uri to get the desired file from raw resources. If you have included a tone called sownd.mp3 in your raw resources folder, all you have to do is change

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

to

  Uri path = Uri.parse("android.resource://"+getPackageName()+"/raw/sound.mp3");

The code should look like this now

try {
  Uri path = Uri.parse("android.resource://"+getPackageName()+"/raw/sound.mp3");
  // The line below will set it as a default ring tone replace
  // RingtoneManager.TYPE_RINGTONE with RingtoneManager.TYPE_NOTIFICATION
  // to set it as a notification tone
  RingtoneManager.setActualDefaultRingtoneUri(
                    getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
                    path);
  Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), path); 
  r.play();
 } 
catch (Exception e) {
 e.printStackTrace(); 
}
 
Dost Muhammad Shah

Dost Muhammad Shah

Dost Muhammad specializing in Embedded Design, Firmware development, PCB designing , testing and prototyping. He enjoys sharing his experience with others .Get in touch with Dost on Twitter or via Contact form

 

16 thoughts on “How to play ringtone/alarm/notification sound in Android

  1. I used your solution to play a custom ringtone but it does not work. The solution for that is to write the file name without extension, that means replace this:
    Uri path = Uri.parse(“android.resource://”+getPackageName()+”/raw/sound.mp3”);
    by this:
    Uri path = Uri.parse(“android.resource://”+getPackageName()+”/raw/sound”);

  2. The last one is not working for me. It’s playing the default ringtone instead of the one I have saved in the raw folder.

    1. For me it works if I remove “.mp3” to the filename:
      Uri path = Uri.parse(“android.resource://”+getPackageName()+”/raw/sound”);
      instead of
      Uri path = Uri.parse(“android.resource://”+getPackageName()+”/raw/sound.mp3”);

  3. I don’t understand the last one. When using a tone from Raw resources folder… in the code, “notification” isn’t defined yet it’s used in the line before r.play();

    1. In this post I am explaining how to use the default ring tone of the device. The default Ringtone is the tone that the user has selected as a tone for Notification or call etc.

Leave a Reply to Arslan DeveloperCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.