如何播放Android通知声音 [英] How to play an android notification sound
问题描述
我想知道如何在不通过媒体流播放的情况下播放通知声音.现在我可以通过媒体播放器执行此操作,但是我不希望它作为媒体文件播放,我希望它作为通知或警报或铃声播放.这是我的代码现在的示例:
I was wondering how I could play a notification sound without playing it over the media stream. Right now I can do this via the media player, however I don't want it to play as a media file, I want it to play as a notification or alert or ringtone. heres an example of what my code looks like right now:
MediaPlayer mp = new MediaPlayer();
mp.reset();
mp.setDataSource(notificationsPath+ (String) apptSounds.getSelectedItem());
mp.prepare();
mp.start();
推荐答案
如果有人仍在寻找解决方案,我在 如何在Android中播放铃声/闹钟声
If anyone's still looking for a solution to this, I found an answer at How to play ringtone/alarm sound in Android
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
您可以将 TYPE_NOTIFICATION 更改为 TYPE_ALARM,但您需要跟踪您的铃声,以便停止播放...例如,当用户单击按钮或其他内容时.
You can change TYPE_NOTIFICATION to TYPE_ALARM, but you'll want to keep track of your Ringtone r in order to stop playing it... say, when the user clicks a button or something.
这篇关于如何播放Android通知声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!