Prateek Jain
Prateek Jain

Reputation: 1234

Play ringtone from the application only for a specific time period

I need to play a ringtone from a application which I am able to achieve. Now, I want to play the ringtone only for the specific duration based on the user input.

If user selects 60sec, the audio should play 60sec only and then stop.

Is there any way to achieve this?

Cheers, Prateek

Upvotes: 0

Views: 2212

Answers (2)

Verne Carlson
Verne Carlson

Reputation: 41

Yes, timertask works well for this. Here's the code I use and it works:

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);

Upvotes: 4

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

Of course. Use TimerTask to stop playback after the given period of time.

Upvotes: 0

Related Questions