2012-12-07 28 views
35

我复制了MP3(kalimba.mp3)文件输出到在res文件夹中的文件夹raw。但是当通知被触发时,它会产生默认声音。如何设置自定义声音通知在Android的

这是如何使一个通知:

protected void GenerateNotify() { 

    NotificationManager myNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification=new Notification(android.R.drawable.ic_btn_speak_now,"hi",100); 
    Intent intent=new Intent(getApplicationContext(),as.class); 
    PendingIntent contentintent=PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0); 
    notification.setLatestEventInfo(getApplicationContext(), "Hi","date", contentintent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.sound = Uri.parse("android.resource://com.example.serviceproject/" + R.raw.kalimba); 
    myNotificationManager.notify(NOTIFICATION_ID,notification); 
} 
+0

您的代码看起来确定。尝试重新启动你的模拟器或手机。 http://stackoverflow.com/questions/5682321/android-notification-sound-defaulting-back-instead-of-playing-custom-sound –

回答

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

如果定义DEFAULT_SOUND,则默认的声音将覆盖任何声音

+0

如何添加.MP3文件通知。 –

+0

把你的mp3文件放到原始文件夹中 – QuokMoon

+2

如果我不写'notification.defaults',那么播放自定义声音的可能性是什么? –

16

R.raw.kalimba是整数资源ID;你想在Uri声音资源的。因此,尝试:

notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE 
     + "://" + getPackageName() + "/raw/kalimba"); 
3

尝试:

乌里声音= Uri.parse( “android.resource://” + context.getPackageName()+“/原料/ notifysnd); 通知。 setSound(音);

1

,就应该替换该行:

notification.sound = Uri.parse("android.resource://com.example.serviceproject/" + R.raw.kalimba); 

与此:

notification.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/kalimba")); 

或在某些情况下:

notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/kalimba"); 
相关问题