Can't play mp4 converted file - JavaFX 2.1

狂风中的少年 提交于 2019-12-02 04:31:15

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.

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