7

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;
    }
}
xorinzor
  • 6,140
  • 10
  • 40
  • 70
  • Catching media errors in JavaFX is finicky. There is a [error catching template](http://docs.oracle.com/javafx/2/api/javafx/scene/media/package-summary.html) in the JavaFX media javadoc. Implement the template in your program and update the question with your results. Also check that your non-oracle flv files are vp6 encoded. – jewelsea Dec 17 '13 at 19:41
  • I added the template, unfortunately no exceptions are thrown – xorinzor Dec 19 '13 at 14:29
  • Did you try to play that video in a pure-FX application? – Mikle Garin Dec 20 '13 at 11:43
  • not that I can recall, do you have any suggestions (or demos) to try? I'm not sure it would help though since my code seems to be running fine for others and just not for me – xorinzor Dec 21 '13 at 14:16

4 Answers4

2

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.

xorinzor
  • 6,140
  • 10
  • 40
  • 70
  • Same for me - no sound, no errors, nothing ... until I installed the windows media feature pack at http://www.microsoft.com/en-us/download/details.aspx?id=40744 – Chris Walshaw Oct 28 '15 at 16:04
1

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. My application played the video for the first time but when i clicked on another mp4 video file it didn't play. 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. Instantiating it again might make mediaView loose the reference of original node. Refer to this post and answer by itachi, javafx mediaview only the audio is playing

lambad
  • 1,046
  • 10
  • 21
0

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

Patrick
  • 4,532
  • 2
  • 26
  • 32
  • Unfortunatly that also just gives a black box within the window, it seems like as if the video just won't load but no Exceptions or such are returned – xorinzor Dec 17 '13 at 14:07
  • You work with JDK >= 1.7 Update 10 and added jfxrt.jar to your Classpath manually? – Patrick Dec 17 '13 at 14:25
  • 1
    It just takes 0.5s to load the flv from oracle, but then the video starts to play. – Patrick Dec 17 '13 at 14:28
  • not for me, left the program running for a few minutes but nothing happened – xorinzor Dec 17 '13 at 14:39
  • I don't think the problem is the video itself either, I have a couple of local flv files which it won't play either, but since the application isn't throwing any exceptions and eclipse not reporting any errors I don't have any idea what the cause might be. – xorinzor Dec 17 '13 at 15:03
  • Sorry, i tried it on my Intel Atom based netbook and it also played the small oracle flv, with larger mp4 files the netbook also has probs, but i always get a picture. – Patrick Dec 18 '13 at 18:21
0

I took your code and tried running it on my machine (Win7 JDK 1.7.0_25) and got the same results. 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. 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. Without either calling mediaPlayer.setAutoPlay(true) or mediaPlayer.play() the video will never start playing.

Edit 2: I just noticed in the comments on another answer that you are having trouble playing the video outside of JavaFX. If you don't already have it installed, try downloading VLC to see if the video can play using that. I believe installing ffdshow tryouts will provide the necessary codecs to play FLV in Windows Media Player. (Although I thought all versions of K-lite codec pack included FLV support)

Jason Braucht
  • 2,358
  • 19
  • 31
  • I added the autoplay (must have slipped somewhere, I did had it originally) and installed the ffdshow tryouts (I already had VLC installed) unfortunatly there is still no playback – xorinzor Dec 21 '13 at 14:13