如何在Android中播放铃声/闹钟声音
我一直在寻找如何在Android中播放铃声/闹钟声音。如何在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
所以..请,如果有人知道如何玩默认铃声/报警让我知道。
我不喜欢上传任何文件。只需播放默认的铃声。
你的例子基本上是我用的。然而,它从不在模拟器上工作,因为模拟器默认没有任何铃声,并且content://settings/system/ringtone
不能解析为任何可播放的内容。它在我的实际电话上正常工作。
您可以使用DDMS在/ sdcard文件夹中推送MP3文件,重新启动模拟器,然后打开媒体应用程序,浏览至您的MP3文件,长按此文件并选择“用作手机铃声”。
错误消失了!
编辑:与通知的声音(如SMS)一样的烦恼使用Ringdroid应用
复制音频文件到模拟器的SD卡,并通过媒体播放器将其选择为默认铃声确实解决这个问题解决了。
如果用户从未在手机上设置过闹钟,则TYPE_ALARM可以返回空值。您可以考虑此:
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);
}
}
你可以简单地起到设置好的铃声与此:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
我仍然收到一个错误 - 无法打开铃声内容:// settings/system/alarm_alert – 2013-02-10 19:19:43
好而简单。但是,根据设备的不同,此方法可能会中断Android中可能播放的其他声音(如音乐)。 – igordcard 2013-11-24 17:46:19
使用getApplicationContext()可能不是一个很好的选择。更多信息在这里:http://stackoverflow.com/questions/9122627/should-i-use-getapplicationcontext-or-activity-this-in-a-long-running-asynctask – Saket 2014-06-12 08:10:55
这工作得很好:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
MediaPlayer thePlayer = MediaPlayer.create(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
try {
thePlayer.setVolume((float) (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION)/7.0)),
(float) (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION)/7.0)));
} catch (Exception e) {
e.printStackTrace();
}
thePlayer.start();
为什么你将音量除以7.0?它是一个通常已知的工作值还是你自己挖出的东西? – 2013-10-22 14:30:30
我挖出来的东西...:D – 2013-10-23 03:41:05
调用thePlayer.prepare()没用,MediaPlayer.create帮你 – onizukaek 2015-03-18 15:41:48
这是我做的方式:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.start();
它与ma相似rkov00的方式,但使用MediaPlayer而不是铃声,以防止中断其他声音,如音乐,可能已经在后台播放。
我试过了最上面的答案(ringtone.play),但声音可能会被切断。我使用这种方法,它完美运作。 – wyz 2015-03-11 07:16:13
这是任何人在他们的应用程序中使用任何其他音频组件的更好的解决方案。 – EntangledLoops 2015-08-12 00:11:03
@YumYumYum,我刚刚测试过,它的工作原理。我什么也没做,只是把上面的代码放到我的setOnClickListner中。你做了什么? – 2017-06-30 13:37:18
下面是一些示例代码:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), notification);
mediaPlayer.start();
用一些解释来解释代码,代码只有答案不值得赞赏。 – 2014-07-16 09:29:45
来吧人,你从来没有读过上面的答案可能。 stackoverflow.com/a/20177743/3332634 – yshahak 2015-07-05 08:29:31
您可以使用此示例代码:
Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone ringtoneSound = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri)
if (ringtoneSound != null) {
ringtoneSound.play();
}
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
//this will update the UI with message
Reminder inst = Reminder.instance();
inst.setAlarmText("");
//this will sound the alarm tone
//this will sound the alarm once, if you wish to
//raise alarm in loop continuously then use MediaPlayer and setLooping(true)
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
//this will send a notification message
ComponentName comp = new ComponentName(context.getPackageName(),
AlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
对于未来的Google:使用RingtoneManager.getActualDefaultRingtoneUri()
,而不是RingtoneManager.getDefaultUri()
。根据它的名字,它会返回实际的uri,所以你可以自由使用它。从getActualDefaultRingtoneUri()
文档:
Gets the current default sound's Uri. This will give the actual sound Uri, instead of using this, most clients can use DEFAULT_RINGTONE_URI.
同时getDefaultUri()
这样说:
Returns the Uri for the default ringtone of a particular type. Rather than returning the actual ringtone's sound Uri, this will return the symbolic Uri which will resolved to the actual sound when played.
这个工程。谢谢 – 2017-09-29 16:12:53
@ V.Kalyuzhnyu总是很乐意提供帮助 – 2017-09-29 16:22:52
返回的URI可能不是'null'即使它并不指向有效的声音。您应该测试'RingtoneManager.getRingtone()'的返回值为'null'而不是/井 – Attila 2013-01-04 18:07:54
2017年,无法响铃失败。你有没有在最近的Android工作? – YumYumYum 2017-06-28 08:02:19
@YumYumYum它适用于模拟器上的我。 – 2017-06-28 19:48:25