First, most loops in a thread should look similar to this:
while(!isInterrupted()) {
try {
// do something (like play song)
} catch (InterruptedException e) {
interrupt();
}
}
If there are blocking calls inside the loop (which it sounds like from your description), they likely allow interrupt(). A call to interrupt() from the outside of the thread will break the blocking call and dump you into the catch block. This ends up clearing the "interrupt" flag, so you need to call interrupt again to get out of the loop.
Depending on how you're playing music:
- If you're using a library that has a "pause" or "stop" method to play the music, you can just use that. Looks like you're using jFugue - its player has a pause() method - have you tried calling that?
- If you're doing something else, you'll need to keep track of the song's position and call interrupt() on the thread. If you want to restart, you'll need to seek back to the tracked position
Note that if you want to restart with the above loop, you'll need a nested loop such as:
while(true) {
while(!isInterrupted()) {
...
}
}
If you only have the !isInterrupted() loop, the thread will finish, and cannot be restarted.
Hope this helps!