在android中设置铃声响铃时间

时间:2017-03-07 16:42:19

标签: android ringtone

我开发了一个带有铃声设置的Android应用程序。问题是我想将我的铃声设置为8秒。我刚接触到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) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepare();
            mMediaPlayer.start();

1 个答案:

答案 0 :(得分:1)

这里我使用Timer来停止铃声。

long ringDelay = 3500;
Uri notification =RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
final Ringtone alarmRingtone= RingtoneManager.getRingtone(getApplicationContext(), notification);
alarmRingtone.play();
TimerTask task = new TimerTask() {
@Override
public void run() {
    alarmRingtone.stop();
}
};
Timer timer = new Timer();
timer.schedule(task, ringDelay);