简体   繁体   English

通知音

[英]sound for notification

I want to play an mp3 sound when my Notification is triggered. 触发Notification我想播放mp3声音。 For this, I have put an mp3 file in my "res/raw" folder and used the following instruction: 为此,我将mp3文件放入“ res / raw”文件夹中,并使用以下说明:

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

But I am getting no sound when the Notifications appears! 但是,当出现Notifications时,我听不到声音!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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