2 slaks SLaks 于 2013.06.17 16:37 提问

如何在 notification 上添加声音?

如何添加声音到一个notification中,这个notification是通过NotificationCompat.Builder创建的。我在res中创建了一个原始文件夹,然后添加了声音。那么现在如何把声音添加到notification中?
有关 Notification 代码:

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

3个回答

cj6585256
cj6585256   2013.06.17 16:56
已采纳

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);
Notification notification = mBuilder.build();
notification.sound = Uri.parse("your path");

baimy1985
baimy1985   2013.06.17 17:33

源码里一段代码,这个应该也可以,ringtoneStr是音频uri

final Notification.Builder noti = new Notification.Builder(context)
                .setWhen(mostRecentNotification.mTimeMillis);
noti.setSound(TextUtils.isEmpty(ringtoneStr) ? null : Uri.parse(ringtoneStr));
mojieke
mojieke   2013.06.24 17:04

关键是追加声音设置时,注意Uri问题就好了,其实声音提醒可以直接调系统自带的声音就可以了

Csdn user default icon
上传中...
上传图片
插入图片