我想知道如何在不通过媒体流播放通知声音的情况下播放通知声音。现在,我可以通过媒体播放器执行此操作,但是我不希望它作为媒体文件播放,我希望它作为通知或警报或铃声播放。这是我的代码现在看起来像的一个示例:
MediaPlayer mp = new MediaPlayer();
mp.reset();
mp.setDataSource(notificationsPath+ (String) apptSounds.getSelectedItem());
mp.prepare();
mp.start();
最佳答案
如果有人仍在寻找解决方案,我在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,但是您希望跟踪自己的Ringring r以便停止播放……例如,当用户单击按钮或其他东西时。