简体   繁体   English

JavaFX视频无法播放

[英]JavaFX video not playing

I followed some tutorials about combining JavaFX with Swing (JFrame) to play a video, however all I get is a black screen where the video is supposed to be without any actual content playing, No errors are reported either. 我遵循了一些关于将JavaFX与Swing(JFrame)结合起来播放视频的教程,但是我得到的只是一个黑屏,其中视频应该没有任何实际内容播放,也没有报告错误。

What am I doing wrong here and why wont the video play? 我在这里做错了什么,为什么视频不播放?

I tried several .flv videos, none of them will start playing (they do play when I open them in my browser) 我尝试了几个.flv视频,其中没有一个会开始播放(当我在浏览器中打开它们时它们会播放)

I'm running jre7 and jdk1.7.0_45 on windows 8.1 N Pro with the K-lite full codec pack installed 我在安装了K-lite完整编解码器包的Windows 8.1 N Pro上运行jre7和jdk1.7.0_45

EDIT: updated my code after the comment of jewelsea, nothing has changed, the black box still appears without content playing, the console doesn't show any text coming up 编辑:更新我的代码后jewelsea的评论,没有任何改变,黑匣子仍然出现没有内容播放,控制台没有显示任何文字出现

package com.example.test;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.media.Media;
import javafx.scene.media.MediaErrorEvent;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;

import javax.swing.*; 

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initAndShowGUI();
            }
        });
    }

    private static void initAndShowGUI() {
        // This method is invoked on the EDT thread
        JFrame frame = new JFrame("Test");
        final JFXPanel fxPanel = new JFXPanel();
        frame.add(fxPanel);
        frame.setSize(640, 480);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initFX(fxPanel);
            }
       });
    }

    private static void initFX(JFXPanel fxPanel) {
        // This method is invoked on the JavaFX thread
        Scene scene = createScene();
        fxPanel.setScene(scene);
    }

    private static Scene createScene() {
        String source;
        Media media;
        MediaPlayer mediaPlayer;
        MediaView mediaView = null;
        try {
            media = new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv");
            if (media.getError() == null) {
                media.setOnError(new Runnable() {
                    public void run() {
                        // Handle asynchronous error in Media object.
                        System.out.println("Handle asynchronous error in Media object");
                    }
                });
                try {
                    mediaPlayer = new MediaPlayer(media);
                    mediaPlayer.setAutoPlay(true);

                    if (mediaPlayer.getError() == null) {
                        mediaPlayer.setOnError(new Runnable() {
                            public void run() {
                                // Handle asynchronous error in MediaPlayer object.
                                System.out.println("Handle asynchronous error in MediaPlayer object");
                            }
                        });
                        mediaView = new MediaView(mediaPlayer);
                        mediaView.setOnError(new EventHandler() {
                            public void handle(MediaErrorEvent t) {
                                // Handle asynchronous error in MediaView.
                                System.out.println("Handle asynchronous error in MediaView: "+ t.getMediaError());
                            }

                            @Override
                            public void handle(Event arg0) {
                                // TODO Auto-generated method stub
                                System.out.println("Handle asynchronous error in MediaView arg0: "+arg0.toString());
                            }
                        });
                    } else {
                        // Handle synchronous error creating MediaPlayer.
                        System.out.println("Handle synchronous error creating MediaPlayer");
                    }
                } catch (Exception mediaPlayerException) {
                    // Handle exception in MediaPlayer constructor.
                    System.out.println("Handle exception in MediaPlayer constructor: "+ mediaPlayerException.getMessage());
                }
            } else {
                // Handle synchronous error creating Media.
                System.out.println("Handle synchronous error creating Media");
            }
        } catch (Exception mediaException) {
            // Handle exception in Media constructor.
            System.out.println("Handle exception in Media constructor: "+mediaException.getMessage());
        }

        Group  root  =  new  Group();
        Scene  scene  =  SceneBuilder.create().width(640).height(480).root(root).fill(Color.WHITE).build();

        if(mediaView != null) {
            root.getChildren().add(mediaView);
        }

        return scene;
    }
}

So I installed the windows media feature pack in order to get adobe premiere pro working (because it required a dll file from windows media player (which I didn't had installed because I run an N version of windows) and now the video does play for me. 所以我安装了windows media功能包以便让adobe premiere pro工作(因为它需要一个来自windows media player的dll文件(我没有安装,因为我运行的是N版本的Windows)现在视频确实播放了为了我。

I can't say with 100% confirmation the cause was not having WMP installed as the media feature pack might as well have installed something else that solved my problem, nonetheless, problem solved :) 我不能说100%确认原因是没有安装WMP,因为媒体功能包可能已经安装了其他解决了我的问题,但问题解决了:)

I want to thank the other answers for trying, I really appreciate it. 我要感谢其他尝试的答案,我真的很感激。

Please do not mind if i am writing this answer. 如果我正在写这个答案,请不要介意。 I know this is a very old question but this answer might help others. 我知道这是一个非常古老的问题,但这个答案可能有助于其他人。 I am currently developing a JavaFX application which needs to execute a file depending upon its type. 我目前正在开发一个JavaFX应用程序,它需要根据其类型执行文件。 My application played the video for the first time but when i clicked on another mp4 video file it didn't play. 我的应用程序第一次播放视频,但当我点击另一个mp4视频文件时,它没有播放。 Here is my initial code. 这是我的初始代码。

private void playVideo(String fileLocation) {
        System.out.println("VideoProcesser Thread = " + Thread.currentThread().getName());
        media = new Media(new File(fileLocation).toURI().toString());
        mediaPlayer = new MediaPlayer(media);
        mediaView = new MediaView(mediaPlayer);
        runnable = () -> {
            System.out.println("Inside runnable VideoProcesser Thread = " + Thread.currentThread().getName());
            mediaPlayer.play();
        };
        mediaPlayer.setOnReady(runnable);
        setVideoMediaStatus(PLAYING);
        pane.getChildren().add(mediaView);
    }

Then since the video player screen was dark, i thought the problem was with media view, so i added the following two line, 然后由于视频播放器屏幕很暗,我认为问题出在媒体视图上,所以我添加了以下两行,

 if(mediaView == null) {
            mediaView = new MediaView(mediaPlayer);
        }
        mediaView.setMediaPlayer(mediaPlayer);

Now, when i click on different videos my application just plays fine. 现在,当我点击不同的视频时,我的应用程序就可以了。 Here is the complete code. 这是完整的代码。

Media media;
MediaPlayer mediaPlayer;
MediaView mediaView;   

private void playVideo(String fileLocation) {
            System.out.println("VideoProcesser Thread = " + Thread.currentThread().getName());
            media = new Media(new File(fileLocation).toURI().toString());
            mediaPlayer = new MediaPlayer(media);
            mediaPlayer.setAutoPlay(true);
            if(mediaView == null) {
                mediaView = new MediaView(mediaPlayer);
            }
            mediaView.setMediaPlayer(mediaPlayer);
            mediaPlayer.play();
            mediaPlayer.setOnError(() -> System.out.println("Current error: "+mediaPlayer.getError()));
            setVideoMediaStatus(PLAYING);
            pane.getChildren().add(mediaView);
        }

Note that if you are using FXML to instantiate mediaView, then do not instantiate it again. 请注意,如果您使用FXML实例化mediaView,则不要再次实例化它。 Instantiating it again might make mediaView loose the reference of original node. 再次实例化可能会使mediaView失去原始节点的引用。 Refer to this post and answer by itachi, javafx mediaview only the audio is playing 请参阅此帖并通过itachi回答, javafx mediaview只播放音频

try this, it works for me: 试试这个,它对我有用:

package de.professional_webworkx.swing;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

import javax.swing.JFrame;

public class MyFrame extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * Create a new Frame, set title, ...
     */
    public MyFrame() {

        this.setTitle("Swing and JavaFX");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(1024, 768);

        // create a JFXPanel
        final JFXPanel jfxPanel = new JFXPanel();

        // add the jfxPanel to the contentPane of the JFrame
        this.getContentPane().add(jfxPanel);
        this.setVisible(true);

        Platform.runLater(new Runnable() {

            @Override
            public void run() {
                jfxPanel.setScene(initScene());
            }
        });
    }

    public static final void main (String[] args) {
        new MyFrame();
    }

    /**
     * init the JFX Scene and 
     * @return scene
     */
    private Scene initScene() {

        Group root = new Group();
        SceneBuilder<?> sb = SceneBuilder.create().width(640).height(400).root(root);
        Media video = new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv");
        MediaPlayer mediaPlayer = new MediaPlayer(video);
        mediaPlayer.setAutoPlay(true);
        mediaPlayer.play();

        MediaView view = new MediaView(mediaPlayer);

        root.getChildren().add(view);
        Scene scene = sb.build();


        return scene;

    }
}

Patrick 帕特里克

I took your code and tried running it on my machine (Win7 JDK 1.7.0_25) and got the same results. 我拿了你的代码并尝试在我的机器上运行它(Win7 JDK 1.7.0_25)并得到了相同的结果。 Black box and no video. 黑匣子,没有视频。

I noticed you aren't setting mediaPlayer.setAutoPlay(true) so I added that call to the createScene() right before mediaPlayer is passed to MediaView . 我注意到你没有设置mediaPlayer.setAutoPlay(true)所以我在将mediaPlayer传递给MediaView之前将该调用添加到createScene() Now the playback seems to work for me. 现在播放似乎对我有用。

// ... prior code omitted

    // added this to OP's code
    mediaPlayer.setAutoPlay(true);

    mediaView = new MediaView(mediaPlayer);
    mediaView.setOnError(new EventHandler() {
        public void handle(MediaErrorEvent t) {
            // Handle asynchronous error in MediaView.
            System.out.println("Handle asynchronous error in MediaView: "+ t.getMediaError());
        }

// ... additional code omitted

Edit: The autoPlay property defaults to false - you can call mediaPlayer.isAutoPlay() to check this. 编辑: autoPlay属性默认为false - 您可以调用mediaPlayer.isAutoPlay()来检查它。 Without either calling mediaPlayer.setAutoPlay(true) or mediaPlayer.play() the video will never start playing. 如果不调用mediaPlayer.setAutoPlay(true)mediaPlayer.play() ,视频将永远不会开始播放。

Edit 2: I just noticed in the comments on another answer that you are having trouble playing the video outside of JavaFX. 编辑2:我刚刚在另一个答案的评论中注意到您在JavaFX之外播放视频时遇到问题。 If you don't already have it installed, try downloading VLC to see if the video can play using that. 如果您还没有安装它,请尝试下载VLC以查看视频是否可以使用该视频播放。 I believe installing ffdshow tryouts will provide the necessary codecs to play FLV in Windows Media Player. 我相信安装ffdshow试用版将提供必要的编解码器来在Windows Media Player中播放FLV。 (Although I thought all versions of K-lite codec pack included FLV support) (虽然我认为所有版本的K-lite编解码器包都包含FLV支持)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM