Android Play Notification Sound From Different Resources

In the previous series of tutorial we have seen how to create notification both in kotlin and java 

In this tutorial we will see playing notification sound different resources( audio file kept different resource like from project "res/raw" and hosted in CDN ) .


Kotlin


To play notification sound we need Uri of the file hosted .


1. Default
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val r = RingtoneManager.getRingtone(context, defaultSoundUri)
r.play()

2. Resource 

Make Sure you have kept custom_sound audio inside "res/raw" folder 

val rawPathUri: Uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.custom_sound);
val r = RingtoneManager.getRingtone(context, rawPathUri)
r.play()

3. Remote CDN
val cdnPathUri: Uri = Uri.parse("https://www.myinstants.com/media/sounds/beeper_emergency_call.mp3")
val r = RingtoneManager.getRingtone(context, cdnPathUri)
r.play()


JAVA


To play notification sound we need Uri of the file hosted .


1. Default
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(MainActivity.this, defaultSoundUri);
r.play();)

2. Resource

Make Sure you have kept custom_sound audio inside "res/raw" folder .
 
Uri rawPathUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.custom_sound);
Ringtone r = RingtoneManager.getRingtone(MainActivity.this, rawPathUri);
r.play();

3. Remote CDN
Uri cdnPathUri = Uri.parse("https://www.myinstants.com/media/sounds/beeper_emergency_call.mp3");
Ringtone r = RingtoneManager.getRingtone(MainActivity.this, cdnPathUri);
r.play();

No comments:

Post a Comment