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(); }
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”);
alarm ringtone ikeep on ringing how to stop that in FCMMessage ?
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.
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”);
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();
It should have been
path
. Thanks for pointing out. fixed the code snippet!how to set the ringtone from the raw directory
Please check the updated post
i have an error in getApplicationContext. could you helğ me? i dont understand 🙁
Are you using it in activity or fragment??
in broadcastreceiver
You need to pass the context
Thanks man ..
how set default alarm ringtone?
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.
Very helpfull, thank you! 🙂