Android URI解析发出的通知声音不起作用

Android URI解析发出的通知声音不起作用,android,uri,android-notifications,Android,Uri,Android Notifications,至少有7个关于Stackoverflow的问题与此相关,我已经多次尝试了每个建议和解决方案,但没有一个有效。以下是我的最新尝试: private Notification createNotification() { Notification notification = new Notification(); if(notifyImage=="food") { notification.icon = R.drawable.

至少有7个关于Stackoverflow的问题与此相关,我已经多次尝试了每个建议和解决方案,但没有一个有效。以下是我的最新尝试:

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;
 }
你可以看到我两次尝试使用的声音从来都不起作用,但图标工作得很完美。我不知道我是否遗漏了任何东西来让它工作,但我使用的所有代码都在我的帖子中

我的声音文件在res/raw/start.mp3中,我可以在按下按钮时让这个声音工作,所以声音很好

我认为包名是正确的,我的应用程序在每个类的顶部都有以下内容:

package com.example.memoryGuide;
知道为什么声音永远不会播放吗?

使用

notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
        + "://" + getPackageName() + "/raw/start");
注意一件事

返回android应用程序的包名。及

包com.example.memoryGuide


显示源程序包名称的程序包名称。

如果此项适用于您。请投赞成票

只需将声音文件放在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;
对于默认振动:

  long[] vibrate = { 0, 100, 200, 300 };
     notification.vibrate = vibrate;
notification.defaults |= Notification.DEFAULT_VIBRATE;

API23
上测试时,我遇到了类似的问题。虽然我不知道问题的原因,但我确实找到了一个解决办法,完成了工作


在其中一种情况下,通知构造如下:

// 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());
注意课堂。出于某种我无法理解的原因,除了默认的之外,它没有播放任何声音。换言之,只有这样才有效:

// 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);
在简单地将builder类替换为(可用于
API>=11
)之后,上面的“this not”部分开始按预期工作


结论:如果您愿意牺牲(或不感兴趣)与
API<11
的兼容性,请使用此选项。

谢谢,它现在就可以工作了,很好地向我展示了如何获取包名:)请不要对几个问题给出完全相同的答案-它们可能不太合适,或者问题是重复的(应该标记为重复)