android – 自定义通知声音无法播放

前端之家收集整理的这篇文章主要介绍了android – 自定义通知声音无法播放前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在状态栏通知中进行自定义声音播放. .mp3文件在res / raw /中.但是当我通知用户声音没有播放.我已经试过MediaPlayer,它的作品,但我不想让它播放与MediaPlayer.

这是我的方法

  1. public void showNotification()
  2. {
  3. String ns = Context.NOTIFICATION_SERVICE;
  4. NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  5.  
  6. int icon = R.drawable.Feedback; // icon from resources
  7. CharSequence tickerText = mContext.getString(R.string.statusbar_notification); // ticker-text
  8. long when = System.currentTimeMillis(); // notification time
  9. Context context = getApplicationContext(); // application Context
  10. CharSequence contentTitle = mContext.getString(R.string.statusbar_notification); // message title
  11. CharSequence contentText = mContext.getString(R.string.statusbar_notificatione_detailed); // message text
  12.  
  13. Intent notificationIntent = new Intent(mContext,Main.class);
  14. PendingIntent contentIntent = PendingIntent.getActivity(mContext,notificationIntent,0);
  15.  
  16. // the next two lines initialize the Notification,using the configurations above
  17. Notification notification = new Notification(icon,tickerText,when);
  18.  
  19. //notification.defaults |= Notification.DEFAULT_SOUND;
  20. notification.defaults |= Notification.DEFAULT_VIBRATE;
  21. notification.sound = Uri.parse("android.resource://" + getPackageName() + "/R.raw.notificationsound");
  22.  
  23. notification.setLatestEventInfo(context,contentTitle,contentText,contentIntent);
  24. mNotificationManager.notify(1,notification);
  25. }

谢谢.

解决方法

从ContentResolver的文档:

The Uri should be one of the following formats:
android.resource://package_name/id_number

你传递的字符串“R.raw.notificationsound”这意味着什么.
相反尝试这样:

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

猜你在找的Android相关文章