来自URI解析的通知声音不起作用

时间:2013-03-16 13:38:27

标签: android uri android-notifications

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

你可以看到我试过两次从不起作用的声音,但图标效果很好。我不知道我是否遗漏了任何东西以使其工作,但我使用的所有代码都在我的帖子中。

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

我认为包名是正确的,我的应用程序将其放在每个类的顶部:

package com.example.memoryGuide;

为什么声音永远不会播放?

3 个答案:

答案 0 :(得分:6)

使用

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

注意一件事。

  

String android.content.ContextWrapper.getPackageName()

返回android应用程序的包名称。并且

  

package com.example.memoryGuide;

显示源包名称的包名称。

答案 1 :(得分:2)

如果这个适合你。请投票..........

只需将您的声音文件放在Res \ raw \ siren.mp3文件夹::

然后把这段代码......这绝对适合你。

自定义声音::

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

默认声音::

notification.defaults |= Notification.DEFAULT_SOUND;

对于Custom Vibrate ::

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

默认振动::

notification.defaults |= Notification.DEFAULT_VIBRATE;

答案 2 :(得分:0)

我在printf("Processed event type: %d with value %f\n", id, data); 上进行测试时遇到了类似的问题。虽然我不知道问题的原因,但我 设法找到完成工作的解决方法。

Google's examples之一中,通知就这样构建:

printf("At sim_time = %f:", sim_time); printf("Processed event type: %d with value %f\n", id, data);

注意班级NotificationCompat.Builder。由于某些原因超出了我,除了默认声音之外,它没有播放任何声音。换句话说,只有这个有效:

API 23

但这并没有:

// 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());

只需使用Notification.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); 的兼容性,请使用此功能。