Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How can I include a Notification Sound using a Sound Resource?

Looking at the developer documentation, I see how to use the default sound and how to use a Uri, but I don't see how to use a resource. How can I use one of the sounds in my res/raw folder? Such as an MP3 or WAV file?

like image 829
700 Software Avatar asked Apr 14 '11 14:04

700 Software


People also ask

Can I set different sounds for Android notifications?

You can easily customize notification sounds for a text message, email, or system notification on your Galaxy phone. You can also set notification sounds with downloaded music files.

Can I have a different sound for texts and notifications?

From the main list of message threads, tap “Menu” then choose “Settings“. Select “Notifications“. Select “Sound“, then choose the tone for text messages or choose “None“. You may also select “Vibrate” to turn vibration on or off.


2 Answers

Uri path = Uri.parse("android.resource://[package]/[res id]");

Example:

Uri path = Uri.parse("android.resource://com.mypackage/"+R.raw.mysound_1);
like image 175
Selvin Avatar answered Sep 28 '22 07:09

Selvin


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

Add to notification builder

.setSound(soundUri)
like image 30
Philip Herbert Avatar answered Sep 28 '22 08:09

Philip Herbert