2

これに関連する Stackoverflow には少なくとも 7 つの質問があります。すべての提案と解決策を複数回試しましたが、どれも機能しませんでした。これが私の最新の試みです:

private Notification createNotification() {
       Notification notification = new Notification();


       if(notifyImage=="food")
         {
           notification.icon = R.drawable.food;
           notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://com.example.memoryGuide/raw/start");
         }
       else
       {
           notification.icon = R.drawable.bar; 
           notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://com.example.memoryGuide/raw/start");
       }


       notification.when = System.currentTimeMillis();
       notification.flags |= Notification.FLAG_AUTO_CANCEL;
       notification.flags |= Notification.FLAG_SHOW_LIGHTS;
       notification.defaults |= Notification.DEFAULT_VIBRATE;
       notification.defaults |= Notification.DEFAULT_LIGHTS; 
       notification.ledARGB = Color.WHITE;
       notification.ledOnMS = 1500;
       notification.ledOffMS = 1500;
       return notification;
 }

うまくいかないサウンドを 2 回使用しようとしたことがわかりますが、アイコンは完全に機能します。これを機能させるために何か不足しているかどうかはわかりませんが、使用しているコードはすべて投稿に含まれています。

私のサウンド ファイルは res/raw/start.mp3 にあります。ボタンを押すとこのサウンドが動作するので、サウンドは問題ありません。

パッケージ名は正しいと思います。私のアプリケーションには、各クラスの上部にこれがあります:

package com.example.memoryGuide;

サウンドが再生されない理由はありますか?

4

3 に答える 3

6

使用する

notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
        + "://" + getPackageName() + "/raw/start");

1 つのことに注意してください。

文字列 android.content.ContextWrapper.getPackageName()

Android アプリケーションのパッケージ名を返します。と

パッケージcom.example.memoryGuide;

ソースパッケージ名のパッケージ名を表示します。

于 2013-03-16T19:46:48.980 に答える
3

これがあなたのために働くなら。投票してください........

サウンド ファイルを Res\raw\siren.mp3 フォルダに入れるだけです ::

それからこのコードを入れてください..これは間違いなくあなたのために働くでしょう.

カスタムサウンドの場合::

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

デフォルトサウンドの場合::

notification.defaults |= Notification.DEFAULT_SOUND;

カスタムバイブレーションの場合::

  long[] vibrate = { 0, 100, 200, 300 };
     notification.vibrate = vibrate;

デフォルトの振動の場合::

notification.defaults |= Notification.DEFAULT_VIBRATE;
于 2013-08-01T12:14:47.767 に答える
0

でのテスト中に同様の問題が発生していましたAPI 23。問題の原因はわかりません、仕事を終わらせる回避策を見つけることができました.


Google の例の1 つで、通知は次のように作成されます。

// Get a notification builder that's compatible with platform versions >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

// Define the notification settings.
builder.setSmallIcon(R.drawable.ic_launcher)
        // In a real app, you may want to use a library like Volley
        // to decode the Bitmap.
        .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher))
        .setColor(Color.RED)
        .setContentTitle(notificationDetails)
        .setContentText(getString(R.string.geofence_transition_notification_text))
        .setContentIntent(notificationPendingIntent);

// Dismiss notification once the user touches it.
builder.setAutoCancel(true);

// Get an instance of the Notification manager
NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Issue the notification
mNotificationManager.notify(0, builder.build());

クラスに注意してくださいNotificationCompat.Builder私を超えた何らかの理由で、これはデフォルトの音以外の音を再生しませんでした。言い換えれば、これだけが機能しました:

// Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // Default
...
builder.setSound(soundUri); //Set the sound to play

しかし、これはしませんでした:

Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + 
                           getPackageName() + "/" + R.raw.name_of_sound);

Notification.Builderビルダー クラスを(available for API >= 11) に単純に置き換えた後、上記の「これは機能しませんでした」セクションが期待どおりに機能し始めました。

結論: との互換性を犠牲にする (または興味がない) 場合は、これを使用してくださいAPI < 11

于 2016-04-02T21:23:44.283 に答える