JavaFX视频无法播放

我遵循了一些关于将 JavaFX与Swing(JFrame)结合起来播放视频的教程,但我得到的只是一个黑屏,其中视频应该没有任何实际内容播放,也没有报告任何错误.

在这里做错了什么,为什么视频不播放?

我尝试了几个.flv视频,其中没有一个会开始播放(当我在浏览器中打开它们时它们会播放)

我在安装了K-lite完整编解码器包的Windows 8.1 N Pro上运行jre7和jdk1.7.0_45

编辑:更新我的代码后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;
    }
}

解决方法

所以我安装了windows media功能包以便adobe premiere pro正常工作(因为它需要一个来自windows media player的dll文件(我没有安装,因为我运行N版本的Windows)现在视频确实播放了为了我.

我不能说100%确认原因是没有安装WMP,因为媒体功能包可能已经安装了其他解决了我的问题的东西,但问题解决了:)

我要感谢其他尝试的答案,我真的很感激.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...