无法播放MP4转换文件 - 的JavaFX 2.1(Can't play mp4 conve

2019-08-01 04:43发布

我已经将我.mov视频.mp4 ,但是当我在JavaFX应用程序播放视频,这是行不通的,我能听到的视频文件,但没有图像的音频! 也许.MP4是不正确的编解码器! 任何想法或解决方案?

UPDATE

用同样的代码,我再打MP4视频,在相同条件下(PLATEFORME,JFX版,...),所以我得出的结论是我有问题,这MP4转换的文件,即我使用的软件转换MOV文件不产生由JFX 2.1支持的approriat mp4格式。

Answer 1:

下面的代码示例演示播放中的JavaFX的H.264编码的MP4视频。

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;
  }
}

示例程序输出:(JavaFX的8u72,OS X 10.9.5)。



Answer 2:

你需要确保你的MP4文件在H264编码。 MPEG4不起作用。



文章来源: Can't play mp4 converted file - JavaFX 2.1