2017-08-02 117 views
0
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。如何從本地文件播放通知聲音

回答

1

如果你把你的聲音在你的資源文件夾(你應該做的,無論如何,通知聲音),並使用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