2017-08-02 9 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

あなたは(通知が聞こえるために、あなたがとにかくやる必要があります)あなたのリソースフォルダ内のサウンドを入れて、代わりにNotificationCompat.BuilderのNotification.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

関連する問題