I have converted my .mov
video to .mp4
, but when I play the video in my javafx application, it doesn't work, I can hear the audio of the video file but without pictures ! maybe the .mp4 is not in the right codec! any idea or solution?
UPDATE
With the same code I have played another mp4 video, in the same conditions (plateforme, JFX Version, ...), so I'm concluding that I have problem with this mp4 converted file, ie the software that I'm using to convert the mov file doesn't generate the approriat mp4 format that is supported by JFX 2.1.
The following code sample demonstrates playing an h.264 encoded mp4 video in JavaFX.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.media.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/** plays an mp4 video in JavaFX 2.1+ */
public class OnlineVideoPlayer extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) {
final Label status = new Label("Init");
MediaPlayer mediaPlayer = createMediaPlayer(
"http://www.html5videoplayer.net/videos/toystory.mp4",
status
);
VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(new MediaView(mediaPlayer), status);
stage.setScene(new Scene(layout, 500, 300, Color.CORNSILK));
stage.show();
if (mediaPlayer != null) {
mediaPlayer.play();
}
}
/**
* creates a media player using a url to the media
* and tracks the status of playing the media via the status label
*/
private MediaPlayer createMediaPlayer(final String url, final Label status) {
Media hit = new Media(url);
MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.setOnError(new Runnable() {
@Override public void run() {
status.setText("Error");
}
});
mediaPlayer.setOnPlaying(new Runnable() {
@Override public void run() {
status.setText("Playing: " + url);
}
});
mediaPlayer.setOnEndOfMedia(new Runnable() {
@Override public void run() {
status.setText("Done");
}
});
return mediaPlayer;
}
}
Sample program output: (JavaFX 8u72, OS X 10.9.5).
You need to make sure your .mp4 file is encoded in H264. MPEG4 does not work.