JavaFX - Playing loop video


Question

How should I loop a video in JavaFX? I'm trying to just play a video one time after another, so I was looking for some sample code in many places and I could'nt make it work!

This is what doesn't work for me:

public MyMediaPlayer (){
    media = new Media(getVideo());
    mediaPlayer = new MediaPlayer(media);
    mediaView = new MediaView(mediaPlayer);
    startMediaPlayer();
}

private String getVideo() {
    return getClass().getResource("videos/limbo.mp4").toString();
}

public final void startMediaPlayer() {
    mediaPlayer.setMute(true);
    mediaPlayer.setCycleCount(javafx.scene.media.MediaPlayer.INDEFINITE); //this is the line that should do the magic, but it doesn't...
    mediaPlayer.play();
}
1
1
10/25/2013 6:55:00 PM

Accepted Answer

The following works for me (video loops forever). I can't replicate your issue.

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.media.*;
import javafx.stage.Stage;

public class VideoPlayerExample extends Application {
  public static void main(String[] args) throws Exception { launch(args); }
  @Override public void start(final Stage stage) throws Exception {
    final MediaPlayer oracleVid = new MediaPlayer(
      new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv")
    );
    stage.setScene(new Scene(new Group(new MediaView(oracleVid)), 540, 208));
    stage.show();

    oracleVid.setMute(true);
    oracleVid.setRate(20);

    oracleVid.setCycleCount(MediaPlayer.INDEFINITE);

    oracleVid.play();
  }
}

I'm under Java 7, doesn't work there . . . the problem seems to be MP4 format.

If you can't play MP4 files, either:

  1. The MP4 is not encoded in a format JavaFX understands (the JavaFX 2.2 Media javadoc details the allowed formats).

    OR

  2. You don't have appropriate codecs installed on your machine to allow the MP4 file to be decoded. See the JavaFX 2.2 Media system requirements for information on what you need to install on your machine to allow MP4 files to be displayed.

1
11/3/2013 11:22:30 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon