小编典典

如何在 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

所以..如果有人知道如何播放默认铃声/闹钟,请告诉我。

我不想上传任何文件。只需播放默认铃声。


阅读 256

收藏
2022-08-02

共1个答案

小编典典

你可以用这个简单地播放一个设置的铃声:

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
2022-08-02