-1

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();
    } 
New contributor
Momo Tamura is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
5
  • 6
    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 of audioPlayer is, 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.) Commented 2 days ago
  • 4
    JavaFX is event driven. As VGR notes, you should be responding to events, not running your own thread. Here is related question and some old example code. The example code is just to demo the concept, it is not intended as a production ready audio player. – Commented yesterday
  • Thank you so much, that exactly solved my problem. I just added a small Thread.sleep(500) and it works as it should. I added a new thread because i call the function from a button.setOnAction event and i head to close the event to not get frozen UI. I use the minim.jar as a sound library. Commented yesterday
  • 2
    This is not the correct way to approach this, as @jewelsea notes. You should not be polling the status of the media player, either in a background thread or in the FX Application Thread. You should be responding to events instead. Read the linked question. Commented yesterday
  • 2
    If you provide a minimal reproducible example we might be able to be more specific about the solution. Commented yesterday

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.