JavaFX MediaPlayer: MP4 Won't Loop on Windows 7

烈酒焚心 提交于 2019-12-23 06:51:13

问题


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.

回答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.



回答2:


The JavaFX MediaPlayer isn't that good, I would recommend using an external library like LWJGL for sounds. That works really well on all operation systems.

Download here: https://www.lwjgl.org/



来源:https://stackoverflow.com/questions/46818209/javafx-mediaplayer-mp4-wont-loop-on-windows-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!