如何在 Android 中播放铃声/闹钟声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2618182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play ringtone/alarm sound in Android
提问by Federico
I have been looking everywhere how to play a ringtone/alarm sound in Android.
我一直在到处寻找如何在 Android 中播放铃声/闹钟声音。
I press a button and I want to play a ringtone/alarm sound. I could not find an easy, straightforward sample. Yes, I already looked at Alarm clock source code... but it is not straightforward and I cannot compile it.
我按下一个按钮,我想播放铃声/闹钟声音。我找不到简单直接的示例。是的,我已经看过闹钟源代码了……但它并不简单,我无法编译它。
I cannot make this work:
我无法完成这项工作:
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();
}
I get this error:
我收到此错误:
04-11 17:15:27.638: ERROR/MediaPlayerService(30): Couldn't open fd for
content://settings/system/ringtone
So.. please if somebody knows how to play a default ringtone/alarm let me know.
所以.. 如果有人知道如何播放默认铃声/闹钟,请告诉我。
I prefer not to upload any file. Just play a default ringtone.
我不想上传任何文件。只需播放默认铃声。
回答by markov00
You can simply play a setted ringtone with this:
你可以简单地播放一个设置的铃声:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
回答by Blundell
If a user has never set an alarm on their phone, the TYPE_ALARM can return null. You can account for this with:
如果用户从未在手机上设置闹钟,则 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);
}
}
回答by igordc
This is the way I've done:
这是我的做法:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.start();
It is similar to markov00's way, but uses MediaPlayer instead of Ringtone which prevents interrupting other sounds, like music, that might already be playing in the background.
它类似于 markov00 的方式,但使用 MediaPlayer 而不是 Ringtone,以防止打断其他可能已经在后台播放的声音,如音乐。
回答by synic
Your example is basically what I'm using. It never works on the emulator, however, because the emulator doesn't have any ringtones by default, and content://settings/system/ringtone
doesn't resolve to anything playable. It works fine on my actual phone.
你的例子基本上就是我正在使用的。但是,它永远不会在模拟器上运行,因为模拟器默认没有任何铃声,并且content://settings/system/ringtone
不会解析为任何可播放的内容。它在我的实际手机上运行良好。
回答by Kamran Ahmed
This works fine:
这工作正常:
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();
回答by Matvey Rybakov
For the future googlers: use RingtoneManager.getActualDefaultRingtoneUri()
instead of RingtoneManager.getDefaultUri()
. According to its name, it would return the actual uri, so you can freely use it. From documentation of getActualDefaultRingtoneUri()
:
对于未来的谷歌员工:使用RingtoneManager.getActualDefaultRingtoneUri()
而不是RingtoneManager.getDefaultUri()
. 根据它的名字,它会返回实际的uri,所以你可以自由地使用它。从文档getActualDefaultRingtoneUri()
:
Gets the current default sound's Uri. This will give the actualsound Uri, instead of using this, most clients can use DEFAULT_RINGTONE_URI.
获取当前默认声音的 Uri。这将给出实际的声音 Uri,大多数客户端可以使用 DEFAULT_RINGTONE_URI 而不是使用它。
Meanwhile getDefaultUri()
says this:
同时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 symbolicUri which will resolved to the actual sound when played.
返回特定类型的默认铃声的 Uri。这将返回符号Uri,而不是返回实际铃声的声音 Uri,它将在播放时解析为实际声音。
回答by OcuS
You can push a MP3 file in your /sdcard folder using DDMS, restart the emulator, then open the Media application, browse to your MP3 file, long press on it and select "Use as phone ringtone".
您可以使用 DDMS 在 /sdcard 文件夹中推送 MP3 文件,重新启动模拟器,然后打开媒体应用程序,浏览到您的 MP3 文件,长按它并选择“用作手机铃声”。
Error is gone!
错误消失了!
Edit: same trouble with notification sounds (e.g. for SMS) solved using Ringdroid application
编辑:使用 Ringdroid 应用程序解决通知声音(例如短信)的相同问题
回答by Kumar sunny
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);
}
}
回答by Gio MV
You could use this sample code:
您可以使用此示例代码:
Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone ringtoneSound = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri)
if (ringtoneSound != null) {
ringtoneSound.play();
}
回答by Valentin Klinghammer
Copying an audio file to the sd card of the emulator and selecting it via media player as the default ringtone does indeed solve the problem.
将音频文件复制到模拟器的 SD 卡并通过媒体播放器选择它作为默认铃声确实解决了问题。