I'm working on a cross-platform swing application that involves playing videos and I decided to use JavaFX's media framework to do so. I followed the tutorials on how to embed JavaFX components in Swing applications and created a solution that works on Windows 7. But when I tried to run the application on Mac OS X, the audio played but no video would showed up while playing mp4 videos. No errors or exceptions were thrown. FLV videos work fine. I used the debugger in NetBeans and was able to discover that the video frames were being decoded, but not rendered (the MediaView property for framesDecoded was incrementing, but framesRendered was not).
In NetBeans, I created two projects with identical code to try to replicate my problem. One was a JavaFxSwing Application and the other was just a JavaApplication. The JavaFxSwing Application played the video fine, the JavaApplication replicated the problem in my application. In JavaFxSwing Applications, NetBeans uses javafx ant tasks to create an executable jar file with com.javafx.Main.main as its entry point, whereas in Java Applications, the jar file's entry point is the main class I wrote. This leads me to believe that I am missing some initialization steps that are taken care of in the com.javafx.Main.main class (maybe searching the system for the video codecs?). I've compared runtime System Properties of the two applications and they are identical.
Here is the application code:
package fxtest;
import java.nio.file.Paths;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class FXTest {
private static final String MEDIA_URL = Paths.get("/path/to/video.mp4").toUri().toString();
private static void initFx(JFXPanel fxPanel) {
Media media = new Media(MEDIA_URL);
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
MediaView mediaView = new MediaView(mediaPlayer);
Group root = new Group();
Scene scene = new Scene(root,800,600);
root.getChildren().add(mediaView);
fxPanel.setScene(scene);
}
private static void initAndShowGui(){
JFrame jFrame = new JFrame("FX Test");
jFrame.setSize(800, 600);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JFXPanel jFXPanel = new JFXPanel();
jFrame.add(jFXPanel);
Platform.runLater(new Runnable(){
@Override
public void run() {
initFx(jFXPanel);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initAndShowGui();
}
});
}
}
I'm using JDK 1.7.0_25 on a 2011 MacBook Pro running Mac OS X 10.7.5. JavaFx Runtime version 2.2.25 (bundled with the JRE)
Am I missing an initialization step or doing something wrong? Is there anything in my environment that needs to be set that could cause this behavior?
Thanks in advance.