Yunus Haznedar Yunus Haznedar - 1 year ago 84
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);;

Answer Source

Use a countDownTimer as:

new CountDownTimer(long millisInFuture, long countDownInterval) {

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

     public void onFinish() {

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

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download