fhs14647
fhs14647

Reputation: 163

JavaFX background thread task should play music in a loop as background thread

I made a little game, where always background music should be played in a loop. I took this code but that does not work. First it plays as expected, then it begins to overloop and it is impossible to listen to. What did I make wrong?

final Task task = new Task() {

        @Override
        protected Object call() throws Exception {
            int s = INDEFINITE;
            AudioClip audio = new AudioClip(getClass().getResource("aquarium.mp3").toExternalForm());
            audio.setVolume(0.5f);
            audio.setCycleCount(s);
            audio.play();
            return null;
        }
    };
    Thread thread = new Thread(task);
    thread.start();

Upvotes: 6

Views: 4629

Answers (1)

fhs14647
fhs14647

Reputation: 163

The problem was my mp3-File. I took a wav-File, and everything worked.

Upvotes: 2

Related Questions