JavaFX video not playing

I followed some tutorials on combining JavaFX with Swing (JFrame) to play the video, however all I get is a black screen on which it is assumed that the video should be without any actual playback of the content. Errors are also reported.

What am I doing wrong here and why is the video not working?

I tried several FLV videos, none of them start playing (they play when I open them in my browser)

I am running jre7 and jdk1.7.0_45 on Windows 8.1 N Pro with the full K-lite codec package installed

EDIT: updated my code after jewelsea comment, nothing has changed, the black box still appears without playing content, the console does not display text 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; } } 
+7
java swing javafx
source share
4 answers

So, I installed the Windows Media Feature Pack in order to make adobe premiere pro work (because it required a DLL file from the Windows media player (which I did not install because I am running the N version of windows) and now the video is playing for me .

I canโ€™t say with 100% confirmation that the reason is not that WMP was installed, as the multimedia package could install something else that solved my problem, nevertheless the problem was solved :)

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

+2
source share

Please do not mind if I write this answer. I know this is a very old question, but this answer may help others. I am currently developing a JavaFX application that should execute a file depending on its type. My application played the video for the first time, but when I clicked on another mp4 video file, it did not play. Here is my source 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 the media view, so I added the following two lines:

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

Now, when I click on different videos, my application just plays perfectly. 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); } 

Please note that if you use FXML to create a media player, do not create it again. Re-creating this file may cause mediaView to lose the link to the original node. Refer to this post and reply to itachi, javafx mediaview only plays audio

+1
source share

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

0
source share

I took your code and tried to run it on my machine (Win7 JDK 1.7.0_25) and got the same results. Black box and video.

I noticed that you are not setting mediaPlayer.setAutoPlay(true) , so I added that the call to createScene() right before the mediaPlayer is passed to MediaView . Now playback works for me.

 // ... prior code omitted // added this to OP 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 default autoPlay property is false - you can call mediaPlayer.isAutoPlay() to check this. Without calling mediaPlayer.setAutoPlay(true) or mediaPlayer.play() video will never start playing.

Edit 2: I only noticed in the comments to another answer that you were having problems playing videos outside of JavaFX. If you haven't installed it yet, try downloading the VLC to see if the video can play with this. I believe that installing ffdshow tryouts will provide the necessary codecs for playing FLV in Windows Media Player. (Although I thought that all versions of the K-lite codec contain the FLV package)

0
source share

All Articles