I'm trying to write an audio player in Java/JavaFX and want to make a playlist play songs automatically. I have a button that triggers a new thread, and everything works fine so far.. but when the first song is over, the new thread (task?) is somehow inactive and doesn't update, so the new song will not be triggered.
If I go to debug mode and make the thread active by hand or have for example a line printed during the whole infinite loop there is no problem getting the new song played automatically by the following code.
Did I forget to implement some update event or something?
Any ideas how I could get this fixed? Would be really welcome and thanks in advance..
public ControlPane(MP3Player mp3Player)
btnPlayPlayList = new Button("playlist");
btnPlayPlayList.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event){
mp3Player.startPlaylist();
}
});
public class MP3Player
public void startPlaylist(){
Thread th = new Thread(playlistController);
th.setDaemon(true);
th.start();
}
public class PlaylistController extends Task
@Override
public void call(){
int i = 0;
boolean playlistIsPlaying = true;
playlsitHelper(i);
while(playlistIsPlaying){
// it works with System.out.println
// System.out.println("temp");
if(audioPlayer.isPlaying() == false){
i++;
if (i < playlist.size())
playlsitHelper(i);
else
playlistIsPlaying = false;
}
}
return playListIsPlaying;
}
private void playlsitHelper(int i){
TrackModel tempTitel = playlist.get(i);
audioPlayer = minim.loadMP3File(tempTitel.getPath());
audioPlayer.play();
}
while(playlistIsPlaying)← This is called a spin loop. It’s a loop that runs very fast, with no pauses or waiting. It gives other threads almost no CPU to run with, including the JavaFX application thread. I’m not sure what the type ofaudioPlayeris, but if it’s a JavaFX MediaPlayer, you need to check for the end of a playback using the onStopped property. (Also, you don’t need a new thread at all if you’re using a JavaFX MediaPlayer to play sound files.)