2011-11-07 192 views
9

我試圖在狀態欄通知上進行自定義聲音播放。 .mp3文件位於res/raw/。但是當我通知用戶聲音沒有播放時。我嘗試過使用MediaPlayer,它可以工作,但我不想讓它與MediaPlayer一起玩。自定義通知聲音未播放

這裏是我的方法:

public void showNotification() 
{ 
     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     int icon = R.drawable.feedback;  // icon from resources 
     CharSequence tickerText = mContext.getString(R.string.statusbar_notification); // ticker-text 
     long when = System.currentTimeMillis();   // notification time 
     Context context = getApplicationContext();  // application Context 
     CharSequence contentTitle = mContext.getString(R.string.statusbar_notification); // message title 
     CharSequence contentText = mContext.getString(R.string.statusbar_notificatione_detailed);  // message text 

     Intent notificationIntent = new Intent(mContext, Main.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0); 

     // the next two lines initialize the Notification, using the configurations above 
     Notification notification = new Notification(icon, tickerText, when); 

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

     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
     mNotificationManager.notify(1, notification); 
} 

感謝。

回答

26

從ContentResolver的文檔:

的URI應該是以下格式之一: android.resource:// PACKAGE_NAME/ID_NUMBER

你傳入字符串「R. raw.notificationsound「,這意味着什麼。 而是試試這個:

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

我該如何設置這些通知聲音的持續時間週期......如何重複這些通知模式 –