仅播放声音的 Android 通知 [英] Android Notification to play sound only

查看:37
本文介绍了仅播放声音的 Android 通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动中有一个倒数计时器,当它达到零时,我会向状态栏发送通知,状态栏还会播放自定义 wav 文件以提醒用户.如果用户当时没有使用该应用程序,我只希望出现状态栏通知,但我仍然希望 wav 文件播放,无论计时器应用程序是否可见.

I have a countdown timer in my activity and when it reaches zero I send a notification to the status bar which also plays a custom wav file to alert the user. I only want the status bar notification to appear if the user isn't using the app at the time but I still want the wav file to play whether the timer app is visible or not.

是否可以让 Android 通知仅播放音频警报?我尝试使用 MediaPlayer 来播放 wav 文件,但这使用它自己的音量设置而不是常规通知音量,因此如果您的媒体音量很低,那么通知声音也会以我不想要的低音量播放.我希望声音以与其他通知相同的音量播放.

Is it possible to make the Android notification only play the audio alert? I've tried using MediaPlayer to play the wav file instead but this uses its own volume settings and not the regular notification volume so if your media volume is low then the notification sound plays at a low volume too which I don't want. I want the sound to play at the same volume as other notifications.

我正在使用这个:http://developer.android.com/guide/topics/ui/notifiers/notifications.html

并提供这样的声音:

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

推荐答案

http://developer.android.com/guide/topics/ui/notifiers/notifications.html有这个注释:

注意:如果 defaults 字段包含 DEFAULT_SOUND,则默认声音会覆盖声字段定义的任何声音.

Note: If the defaults field includes DEFAULT_SOUND, then the default sound overrides any sound defined by the sound field.

所以,如果你只想自定义声音,你可以使用 -

So, if you want to customize only sound, you may use -

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;

这篇关于仅播放声音的 Android 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆