JJavaScript
JJavaScript

Reputation: 182

JavaFX MediaPlayer: MP4 Won't Loop on Windows 7

I've created a basic JavaFX Media Player. On my Windows 10 OS, everything works fine, and it functions exactly as it's supposed to.

private MediaPlayer initializeMediaPlayer(){
    Media media = new Media(getClass().getResource("1-1.mp4").toString());
    MediaPlayer mediaPlayer = new MediaPlayer(media);
    mediaPlayer.setAutoPlay(true);
    mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
    mediaPlayer.setRate(1.25);
    mediaPlayer.setMute(true);
    return mediaPlayer;
}

Yet, when I run this code on Windows 7, the video doesn't loop: it plays for five seconds and at the end of the video, the video just freezes. Given that the video is only 5 seconds long, the loop is absolutely essential for this program to work properly.

Here is what I know about this problem:

Upvotes: 17

Views: 867

Answers (2)

Soni
Soni

Reputation: 77

The JavaFX MediaPlayer isn't all that good, I would recommend using a library like LWJGL for sounds. That should work very well on every OS.

Upvotes: 1

Isfirs
Isfirs

Reputation: 134

Environment:

  • Win 10 Prof
  • Java 8U144 (but tested with 8U177 as well)

I used a mp4 from this website as a sample for my test: techslides.com

My Code (Note: I use a custom FX Framwork, so my I only show you my controller creation method which sets up the player):

@Override
protected BorderPane createView() {
    final BorderPane view = new BorderPane();

    final Media media = new Media(getClass().getResource("small.mp4").toString());
    final MediaPlayer player = new MediaPlayer(media);
    player.setCycleCount(MediaPlayer.INDEFINITE);
    player.setRate(1.25);
    player.setMute(true);
    player.setOnEndOfMedia(() -> {
        player.play();
    });
    player.play();

    final MediaView mediaView = new MediaView(player);
    view.setCenter(mediaView);

    return view;
}

I use a callback and start a replay manually. This works as an infinite loop, even this is the more "complicated" way of doing it, though. Also, this worked for me as well and should be considered the more "correct" way:

@Override
protected BorderPane createView() {
    final BorderPane view = new BorderPane();

    final Media media = new Media(getClass().getResource("small.mp4").toString());
    final MediaPlayer player = new MediaPlayer(media);
    player.setAutoPlay(true);
    player.setCycleCount(MediaPlayer.INDEFINITE); // or Integer.MAX_VALUE
    player.setRate(1.25);
    player.setMute(true);

    final MediaView mediaView = new MediaView(player);
    view.setCenter(mediaView);

    return view;
}

Additional Note:

  • I tested both codes with both the Oracle video you linked and the small.mp4 given from the techslide page
  • If it helps you, I may post a full framework-free code where you can place in your video to see if it should work.

Upvotes: 1

Related Questions