0

I have the following code, which creates a MediaPlayer and plays an audio:

public void playSong(Songs song) {
    this.media = new Media(getClass().getResource("/sounds/login_song.wav").toString());
    this.soundPlayer = new MediaPlayer(media);
    this.soundPlayer.setVolume(this.currentVolume);
    this.soundPlayer.setAutoPlay(true);
    this.soundPlayer.setCycleCount(MediaPlayer.INDEFINITE);
    this.soundPlayer.play();
}

for creating an audio-loop I used:

this.soundPlayer.setAutoPlay(true);
this.soundPlayer.setCycleCount(MediaPlayer.INDEFINITE);

This works, when Iam starting my application from IDE. Iam using: Eclipse Oxygen.3a Release (4.7.3a).

When I export my application to a runnable JAR-file, the audio is only played once and doesnt loop. Running the Jar from terminal does not help, since no errors occur (java -jar MyApplication.jar).

Why are the audios not looping?

Moritz Schmidt
  • 2,635
  • 3
  • 27
  • 51
  • could you run this command in CMD `java -jar JARNAME.jar` – Ahmed Emad Aug 26 '18 at 08:15
  • Hello, I said "running the jar from terminal does not help, since no errors occur". I used your command. – Moritz Schmidt Aug 26 '18 at 08:16
  • 1
    sorry didn't see – Ahmed Emad Aug 26 '18 at 08:18
  • 1
    you can make some tests to know where exactly is the problem you can change the file , try on a different machine , use another IDE or try different file extension. I had a problem before with mp4 files changing some video attribute such as bit rate and frame rate made the video to work – Ahmed Emad Aug 26 '18 at 08:24
  • 1
    You might be running into the same issue noted in https://stackoverflow.com/questions/46818209/javafx-mediaplayer-mp4-wont-loop-on-windows-7?noredirect=1#comment88583553_46818209 – Slaw Aug 27 '18 at 01:33

1 Answers1

1

i tried your code and it worked well from both IDE and jar, but i have a hack for you if you want to loop indefinitely using the setOnEndOfMedia() method like this

UPDATE:

not a perfect solution but recreating the media player in onEndOfMedia will somehow solve this problem (i did this try catch block to get visual exceptions while testing from the jar)

MediaPlayer mp;
Media m = null;

@Override
public void start(Stage ps) throws Exception {
    try {
        m = new Media(getClass().getResource("/play.wav").toString());
        mp = new MediaPlayer(m);
        Runnable onEnd = new Runnable() {
            @Override
            public void run() {
                mp.dispose();
                mp = new MediaPlayer(m);
                mp.play();
                mp.setOnEndOfMedia(this);
            }
        };
        mp.setOnEndOfMedia(onEnd);
        mp.play();
        ps.show();
    } catch (Exception ex) {
        Alert al = new Alert(AlertType.ERROR);
        al.setContentText(ex.getMessage());
        al.showAndWait();
    }
}
SDIDSA
  • 894
  • 10
  • 19