我一直在寻找如何在Android中播放铃声/闹铃声音。
我按下一个按钮,我想播放铃声/闹铃声音。我找不到一个简单、直接的样本。是的,我已经看过闹钟的源代码了.但它并不简单,我也无法编译它。
我不能这样做:
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(this, alert);
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.prepare();
player.start();
}
我得到了这个错误:
04-11 17:15:27.638: ERROR/MediaPlayerService(30): Couldn't open fd for
content://settings/system/ringtone
所以..。如果有人知道如何播放默认铃声/闹铃,请让我知道。
我不喜欢上传任何文件。只需播放默认铃声。
发布于 2011-12-20 06:17:27
您可以使用以下命令简单地播放设置好的铃声:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
发布于 2011-04-16 22:08:12
如果用户从未在其电话上设置过闹钟,则TYPE_ALARM可能会返回null。您可以使用以下命令来说明这一点:
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert == null){
// alert is null, using backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// I can't see this ever being null (as always have a default notification)
// but just incase
if(alert == null) {
// alert backup is null, using 2nd backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
发布于 2013-11-25 01:46:01
这就是我所做的:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.start();
它类似于markov00的方式,但使用MediaPlayer而不是铃声,这可以防止干扰其他声音,如音乐,可能已经在背景中播放。
https://stackoverflow.com/questions/2618182
复制相似问题