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:
Environment:
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:
small.mp4
given from the techslide pageThe 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With