Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

java - 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.

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)

I'm running jre7 and jdk1.7.0_45 on windows 8.1 N Pro with the K-lite full codec pack installed

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

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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 :)

I want to thank the other answers for trying, I really appreciate it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...