URI解析后的通知声音无法正常工作。

3
至少有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;

有什么想法为什么声音从未播放?
3个回答

7

使用

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

请注意一件事。

String android.content.ContextWrapper.getPackageName()

返回你的Android应用程序的包名。而

package com.example.memoryGuide;

显示了你源代码的包名。


谢谢,现在它可以工作了,你很棒,向我展示如何获取包名! :) - deucalion0

4

如果这个对你有用,请投票支持..........

只需将您的声音文件放入 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;

1
请不要在多个问题中发布完全相同的答案-它们要么不真正适合,要么问题是重复的(应标记为这样)。 - kleopatra

0

我在测试 API 23 的时候遇到了类似的问题。虽然我不知道问题的原因,但是我找到了一种解决办法来完成工作。


Google的示例中,通知是这样构建的:
// 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(适用于API >= 11),上面的“this didn't”部分就如预期一样开始工作了。

结论:如果你愿意牺牲(或者不关心)与API < 11的兼容性,那么请使用此方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接