2012-11-16 160 views
0

當我的Notification被觸發時,我想播放MP3聲音。爲此,我在我的「res/raw」文件夾中放入了一個mp3文件,並使用以下指令:通知聲音

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

但是,當Notifications出現時我沒有聲音!

有什麼想法?

回答

0

簡單地說這下面的時候通知時,將觸發該塊內部代碼..

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

我發現這裏的例子 -

這是用來

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

另外的代碼,如果你想自定義聲音(因爲我是你的),你應該使用這個代碼取自here

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

;

+0

第二種方法是行不通的。它顯示了我正在做的事情。 – Zelig63