I am having my own audio file placed in raw folder inside resource folder.I want to set it as notification sound alert. how should i proceed
Open your device's Settings app . Tap Accessibility. Sound Notifications. Tap Open Sound Notifications.
Please used below code when you get notification in BroadcastReceiver then call activity in that activity class used below code so play sound file.
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
Happy Coding..
This is often achieved using the MediaPlayer but that is not an ideal solution because it runs a little independently of notifications, and therefore will behave in unexpected ways for muting, blocking mode, and various other things. For consistency and compatibility, the sound should be played using the audio mechanism of notifications themselves. This can be accomplished by setting an appropriate URI, along the lines of:
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_myicon)
.setContentTitle(title).setAutoCancel(true);
Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/raw/mymp3");
builder.setSound(alarmSound);
...
builder.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
The key part is:
Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/raw/mymp3");
The mp3 file is stored in your /res/raw folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With