如何为通知添加声音?

时间:2023-01-19 16:16:17

How do you add sound to a notification created by NotificationCompat.Builder? I created a raw folder in res and added the sound there. So how do I now add it to notification? This is my Notification code

如何为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());

2 个解决方案

#1


45  

I'm guessing the problem here is how to reference the sound with a Uri, as there is an obvious method in the NotificationCompat.Builder class - setSound(Uri soundUri).

我猜这里的问题是如何用Uri引用声音,因为NotificationCompat.Builder类中有一个明显的方法 - setSound(Uri soundUri)。

To access your raw resources you need to create the Uri as follows:

要访问原始资源,您需要按如下方式创建Uri:

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

So the code could end up looking like that:

所以代码最终看起来像这样:

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

#2


16  

To play a sound with your notification:

要在通知中播放声音:

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

Do normal notification procedures

做正常的通知程序

To play the default sound with your notification:

要使用您的通知播放默认声音:

notification.defaults |= Notification.DEFAULT_SOUND;

To play a custom sound with your notification:

要使用您的通知播放自定义声音:

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

Then just use the notification manager to send the notification. If both of these statements are used, the application will default to using the default sound.

然后只需使用通知管理器发送通知。如果同时使用这两个语句,则应用程序将默认使用默认声音。

#1


45  

I'm guessing the problem here is how to reference the sound with a Uri, as there is an obvious method in the NotificationCompat.Builder class - setSound(Uri soundUri).

我猜这里的问题是如何用Uri引用声音,因为NotificationCompat.Builder类中有一个明显的方法 - setSound(Uri soundUri)。

To access your raw resources you need to create the Uri as follows:

要访问原始资源,您需要按如下方式创建Uri:

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

So the code could end up looking like that:

所以代码最终看起来像这样:

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

#2


16  

To play a sound with your notification:

要在通知中播放声音:

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

Do normal notification procedures

做正常的通知程序

To play the default sound with your notification:

要使用您的通知播放默认声音:

notification.defaults |= Notification.DEFAULT_SOUND;

To play a custom sound with your notification:

要使用您的通知播放自定义声音:

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

Then just use the notification manager to send the notification. If both of these statements are used, the application will default to using the default sound.

然后只需使用通知管理器发送通知。如果同时使用这两个语句,则应用程序将默认使用默认声音。