如何从本地文件播放通知声音

问题描述:

sound = Uri.parse("file://" + audioFilePath.getAbsolutePath()); 
    /*sound = Uri.fromFile(audioFilePath);*/ 
    NotificationCompat.Builder mBuilder = 
        new NotificationCompat.Builder(this); 
      mBuilder 
        .setSmallIcon(R.drawable.notification_icon) 
        .setContentTitle(title) 
        .setContentText(description) 
        .setContentIntent(resultPendingIntent) 
        .setSound(sound); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
       mBuilder.setPriority(Notification.PRIORITY_HIGH); 
      } 
      NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      mNotificationManager.notify(1, mBuilder.build()); 

声音文件存在于本地存储中。通知正常工作,但声音无法播放。我试图从文件路径解析Uri。如何从本地文件播放通知声音

如果你把你的声音在你的资源文件夹(你应该做的,无论如何,通知声音),并使用Notification.Builder代替NotificationCompat.Builder的,你应该能够访问它是这样的:

Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + 
         getPackageName() + "/" + R.raw.name_of_sound); 
Notification.Builder mBuilder = 
       new Notification.Builder(this); 
     mBuilder 
       .setSmallIcon(R.drawable.notification_icon) 
       .setContentTitle(title) 
       .setContentText(description) 
       .setContentIntent(resultPendingIntent) 
       .setSound(sound); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
      mBuilder.setPriority(Notification.PRIORITY_HIGH); 
     } 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(1, mBuilder.build()); 

Source