17

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.
bugfreerammohan
  • 1,471
  • 1
  • 7
  • 22
JJavaScript
  • 182
  • 6
  • Any resolution? – mre Jun 08 '18 at 17:24
  • @Slaw I've observed that, if the video is stored within an external JAR, it will never loop. Adding this resource locally allowed for the expected loop behavior. I am using Windows 7, Java 8 update 51. – mre Jun 10 '18 at 22:33
  • I can't reproduce the problem in Windows 7 Java version 1.8.0_73. I tried loading the file either from resources or locally, I also tried to play video with more than 5 Sec, exactly 5 Sec and less than 5 Sec (3 Sec to be precise) and everything works fine. I might try it with a latest version of Java, but this was installed on this old PC, maybe the problem appeared in the latest versions. – JKostikiadis Jun 11 '18 at 14:34
  • @JKostikiadis Thanks. Are the videos mp4 or flv? – mre Jun 11 '18 at 15:17
  • @mre yes every video was a MP4 file (which was recorded by phone to be honest) if the OP can post a sample video, would be nice to try that too, – JKostikiadis Jun 11 '18 at 15:22
  • @JKostikiadis "maybe the problem appeared in the latest versions" - maybe, but I am also seeing it in 1.8.0_51, which is earlier than yours. – mre Jun 11 '18 at 17:45
  • @mre Very strange.. two cases here. The most likely I am testing it wrong somehow or case two the version i test it did not have this bug ( which is very unlikely to be the only one or to be one of the few who doesnt). All i did was to create a mediaplayer using the code above and with a MediaView to display the video on a stage using a BorderPane as parent of the MediaView. The video was playing without any problem looping endlessly. Maybe there is something about the video encoding or sound encoding that cause the bug? – JKostikiadis Jun 11 '18 at 17:56
  • @mre I would like to know more about the file you are using to make the testing. I mean format duration maybe there is something i am missing and if its possible to provide an example of a file which I could use as well. – JKostikiadis Jun 11 '18 at 18:06
  • @JKostikiadis in your test, where was the video file stored? for me, i consume this from an external JAR. unfortuantely, i can't provide you with the source video. however, i can give you its codec: _H264 - MPEG-4 AVC (part 10) (avc1)_. – mre Jun 11 '18 at 18:52
  • I have found a workaround where you copy the resource URL to a local, temporary file and then load that as a URI. Not great, but it's something. – mre Jun 11 '18 at 21:09
  • This also appears relevant - https://stackoverflow.com/questions/15660824/media-play-wav-file-inside-jar. – mre Jun 11 '18 at 21:18
  • 1
    It's an open bug as you can see here https://bugs.openjdk.java.net/browse/JDK-8088375 also try possible solution by adding this line in your method given above >>>>>>player.setStopTime(new Duration(40000)); – this_is_om_vm Jun 12 '18 at 10:08
  • so it's solved? – Maciej Pulikowski Jun 13 '18 at 20:14
  • @MaciejPulikowski I would say no. Only workarounds have been identified. – mre Jun 15 '18 at 17:05

2 Answers2

1

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.
Isfirs
  • 124
  • 1
  • 12
1

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.

Soni
  • 77
  • 1
  • 10