9

ステータスバー通知でカスタムサウンドを再生しようとしています。 .mp3ファイルはres/raw/にあります。しかし、ユーザーに通知すると、そのサウンドは再生されません。私はMediaPlayerを試しましたが、うまくいきましたが、MediaPlayerで再生したくありません。カスタム通知音が再生されない

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

ありがとう:

は、ここに私の方法です。

答えて

26

ContentResolverのためのドキュメントから:

ウリは、次のいずれかの形式でなければなりません: android.resource://パッケージ名/ ID_NUMBER

あなたは、文字列「R.を渡していますraw.notificationsound "は何も意味しません。 代わりにこれを試してみてください。

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

を私はこれらの通知音の継続サイクルを設定するのですか...これらの上にパターンを繰り返すようにする方法 –

関連する問題