无法播放mp4转换的文件-JavaFX 2.1 [英] Can't play mp4 converted file - JavaFX 2.1

查看:106
本文介绍了无法播放mp4转换的文件-JavaFX 2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将我的.mov视频转换为.mp4,但是当我在javafx应用程序中播放视频时,它无法正常工作,我可以听到视频文件的音频,但是没有图片!也许.mp4不在正确的编解码器中!有什么想法或解决方案吗?

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?

更新

使用相同的代码,我在相同的条件下(平台,JFX版本,...)播放了另一个mp4视频,因此我断定这个mp4转换文件存在问题,即我所使用的软件用于转换mov文件的m不会生成JFX 2.1支持的适当的mp4格式.

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.

推荐答案

以下代码示例演示了如何使用JavaFX播放h.264编码的mp4视频.

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

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

Sample program output: (JavaFX 8u72, OS X 10.9.5).

这篇关于无法播放mp4转换的文件-JavaFX 2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆