Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • The problem ONLY persists for mp4 files on Windows 7. When ran the program with oracle's example .flv file (i.e. http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv), it worked fine. Converting my mp4 files to flv is not an option.
  • The program works fine on all the Windows 10 computers I've run it on. This problem only occurs in Windows 7, but I have not tested it on any other operating systems. I need to eventually run this program in a lab with only Windows 7 computers.
  • All the other MediaPlayer parameters (i.e. set autoplay, setMute, and setRate) work fine in both Windows 10 and Windows 7. It's just the setCycleCount attribute that doesn't seem to work on Windows 7.
  • On all the test computers, I made sure the Java was updated to the most recent version. I am using Java 8 update 144.
like image 797
JJavaScript Avatar asked Oct 18 '17 19:10

JJavaScript


2 Answers

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.
like image 152
Isfirs Avatar answered Sep 19 '22 08:09

Isfirs


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.

like image 25
Soni Avatar answered Sep 19 '22 08:09

Soni