简体   繁体   中英

sound for notification

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?

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

;

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM