Yunus Haznedar Yunus Haznedar - 19 days ago 5
Android Question

How to set duration of my ringtone's play

I want to set duration of my ringtone's play. For example I want to stop when it's played 30 second. Have any idea to solve this? Cause ringtone has no setDuration method or something like that.

Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();

Answer

Use a countDownTimer as:

new CountDownTimer(long millisInFuture, long countDownInterval) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
}.start();

You can use onTick and onFinish, so cancel the ringtone when timer finishes.

Hope this helps!