9

Tôi đang cố gắng phát âm thanh tùy chỉnh trên thông báo trên thanh trạng thái. Tệp .mp3 nằm trong số res/raw/. Nhưng khi tôi thông báo cho người dùng âm thanh không được phát. Tôi đã thử với MediaPlayer, và nó hoạt động, nhưng tôi không muốn làm cho nó chơi với MediaPlayer.Âm thanh thông báo tùy chỉnh không phát được

Đây là phương pháp của tôi:

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

Cảm ơn.

Trả lời

26

Từ tài liệu cho ContentResolver:

Các Uri nên là một trong các định dạng sau: android.resource: // package_name/id_number

Bạn đang đi qua các String "R. raw.notificationsound "có nghĩa là không có gì. Thay vào đó hãy thử điều này:

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

làm cách nào để đặt chu kỳ thời gian cho các âm thanh thông báo này ... –

Các vấn đề liên quan