简体   繁体   English

如何在通知中添加声音?

[英]How can I add a sound to a notification?

I have a notification that works perfectly, but no sound plays, not even the default sound, I've tried numerous approaches listed on this website to try to get a sound to play but to no avail. 我收到的通知效果很好,但是没有声音播放,甚至没有默认声音,我已经尝试了本网站上列出的多种方法来尝试播放声音,但无济于事。

If I create a media player I can play the audio file just fine when the application starts up, but I can't seem to get the file to play when a notification is started. 如果创建媒体播放器,则在应用程序启动时可以正常播放音频文件,但是在启动通知时似乎无法播放该文件。

final Notification.Builder notifyDetailsBuilder = new Notification.Builder(context)
.setContentTitle("Your Jewellery Received A New Message")
.setContentText("Click To View Message")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)                
.setContentIntent(intent);

final Notification notifyDetails = notifyDetailsBuilder.getNotification();                              
mNotificationManager.notify(35178, notifyDetails);

Any help would be greatly appreciated. 任何帮助将不胜感激。

Try 尝试

RingtoneManager ringtoneManager = new RingtoneManager(context);
Uri defaultSoundUri = ringtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final Notification.Builder builder = new Notification.Builder(context)
    ...
    .setSound(defaultSoundUri);

set the defaults to 0 将默认设置为0

RingtoneManager ringtoneManager = new RingtoneManager(context);
Uri defaultSoundUri = ringtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final Notification.Builder builder = new Notification.Builder(context)
    .setDefaults(0)
    .setSound(defaultSoundUri)

... the rest of your code ...其余的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM