2012-12-26 11 views
20

NotificationCompat.Builderによって作成された通知にどのようにサウンドを追加しますか? 私はresに生のフォルダを作成し、そこにサウンドを追加しました。それでは、通知にどのように追加するのですか?これは、私がここでの問題を推測している私の通知コード通知にサウンドを追加するには?

int NOTIFY_ID=100; 
    Intent notificationIntent = new Intent(this, Notification.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
      .setContentIntent(pendingIntent) 
      .setSmallIcon(R.drawable.notification) 
      .setContentTitle("Warning") 
      .setContentText("Help!") 

    NotificationManager mgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mgr.notify(NOTIFY_ID, mBuilder.build()); 
+0

[NotificationCompat.Builder]に[setSound](http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setSound(android.net.Uri))メソッドがあります](http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html)。それはあなたが探しているものですか? –

答えて

40

あるNotificationCompat.Builderクラスの明白な方法があるとして、Uriとサウンドを参照する方法である - setSound(Uri soundUri)が。

android.resource://[PACKAGE_NAME]/[RESOURCE_ID]

をので、コードはそのように見える終わる可能性:

Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd); 
mBuilder.setSound(sound); 
+0

サウンドファイルはどのような形式にする必要がありますか?私は自分のサウンドではなく、デフォルトのサウンドを得ています。 – karl

+2

私は自分の質問に試行錯誤して答えました:1)mp3は動作し、wavはありません.2)通知を作成し、 'n.defaults&=〜Notification.DEFAULT_SOUND ; ' – karl

+3

Notification.Builderを使用している場合、* builder.setDefaults(〜Notification.DEFAULT_SOUND); *がトリックを行います。先端の@karlに感謝します – Maragues

15

あなたの通知でサウンドを再生するには、次のようにUriを作成する必要があり、あなたのrawリソースにアクセスするには

Notification notification = new Notification(icon, tickerText, when); 

通常の通知をしますか?あなたの通知でカスタムサウンドを再生するには

notification.defaults |= Notification.DEFAULT_SOUND; 

:あなたの通知にデフォルトのサウンドを再生する手順

notification.sound = Uri.parse("file:///sdcard/notification/notification.mp3"); 

それからちょうど通知を送信するように通知マネージャを使用しています。 これらのステートメントの両方を使用すると、アプリケーションはデフォルトのサウンドを使用するようにデフォルト設定されます。

関連する問題