开发者

Play ringtone from the application only for a specific time period

开发者 https://www.devze.com 2023-02-22 09:26 出处:网络
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 spe开发者_如何学Pythoncific duration based on the user input.

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 spe开发者_如何学Pythoncific 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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消