0

I want to play an mp3 sound when my Notification is triggered. For this, I have put an mp3 file in my "res/raw" folder and used the following instruction:

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

But I am getting no sound when the Notifications appears!

Any idea?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Zelig63
  • 1,592
  • 1
  • 23
  • 40

2 Answers2

0

I found an example here -

This is the code that is used

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

On the other hand, if you want to customize the sound (as I ausume you do), you should use this code taken from here

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE

;

Community
  • 1
  • 1
jcw
  • 5,132
  • 7
  • 45
  • 54
0

Simply put this below code inside the block which will be triggered when notification occurs..

mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.mySound);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);  // Set false if you don't want it to loop
mMediaPlayer.start();
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • Works with a collateral problem : the sound level is not controlled by the notification sound level but by the media sound level... – Zelig63 Jun 23 '18 at 07:02