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.
精彩评论