设置在Android中的铃声循环

时间:2014-09-11 13:28:15

标签: android android-audiomanager

我想反复播放我目前在Android中设置的铃声 这是我的代码

Uri ring = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), ring);
r.play();

但它只播放一次。在我停止服务之前,我该怎样连续播放?

1 个答案:

答案 0 :(得分:0)

RingtoneManager ringtoneManager = new RingtoneManager(this);

Uri alarmRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

MediaPlayer mMediaPlayer = new MediaPlayer();
try {
    mMediaPlayer.setDataSource(this, alarmRingtoneUri);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
    mMediaPlayer.setLooping(true);
    mMediaPlayer.prepare();
    mMediaPlayer.start(); 
} catch (IllegalArgumentException e) {

    e.printStackTrace();

} catch (SecurityException e) {

    e.printStackTrace();

} catch (IllegalStateException e) {

    e.printStackTrace();

} catch (IOException e) {

    e.printStackTrace();
}