Hmm. While the basic idea is sound (and incidentally not unlike the interrupt mechanism) this particular proposal seems to leave no way to gracefully (or less gracefully) suspend or terminate the task.
I assume that the OP might want to `interrupt' the thread halfway through because, say, you want to play a different MP3. Or pause it -- and if you resume you do not necessarily resume at the `start' of the track.
If all the OP wants to do is simply play music (and no loop/seeking w/ever) then simply recoding his MP3 class to be a Runnable() subclass, and doing something like:
Code:
class PlaybackController {
// Java util concurrent convenience class that handles thread management for us
Executor playbackService= Executors.newSingleThreadExecutor();
// currently playing mp3; possibly null, possibly done according to isDone()
Future playbackTask;
public void play(MP3 mp3) {
stop(); // stop previous track if applicable
playbackTask=Executors.submit(mp3);
}
public void stop() {
if(playbackTask!=null && !paybackTask.isDone()) {
playbackTask.cancel(true); // cancel, possibly interrupting the thread
}
}
}
Bookmarks