FXMLController initialize method:
@FXML
private VBox vbContainer;
MediaPlayerVLC m_mediaPlayer;
public void initialize(URL url, ResourceBundle rb) {
final SwingNode swingNode = new SwingNode();
m_mediaPlayer = new MediaPlayerVLC();
createAndSetSwingContent(swingNode, m_mediaPlayer);
vbContainer.getChildren().add(0, swingNode);
}
And createAndSetSwingContent():
private void createAndSetSwingContent(final SwingNode swingNode, JComponent jComponent) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
swingNode.setContent(jComponent);
}
});
}
MediaPlayerVLC class:
package javafxswing;
import javax.swing.JPanel;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
public class MediaPlayerVLC extends JPanel {
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
public MediaPlayerVLC() {
setSize(350, 320);
setVisible(true);
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
mediaPlayerComponent.setSize(350, 320);
add(mediaPlayerComponent);
}
public MediaPlayer getMediaPlayer() {
return mediaPlayerComponent.getMediaPlayer();
}
}
And I play the media by clicking a button, in FXMLController:
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
m_currentVideo = "video.mov";
m_mediaPlayer.getMediaPlayer().playMedia(m_currentVideo);
}
Now, the problem: The video starts to play but I can see no image. I say it starts to play because I can hear the audio. What am I doing wrong?
Any help highly appreciated.
In short, it won't work this way.
From the Javdoc for SwingNode
here: http://docs.oracle.com/javase/8/javafx/api/javafx/embed/swing/SwingNode.html
It states, in part:
The hierarchy of components contained in the JComponent instance should not contain any heavyweight components, otherwise SwingNode may fail to paint it.
In the case of vlcj, the EmbeddedMediaPlayerComponent
extends Panel
, a heavyweight AWT component.
To use with vlcj with JavaFX you will likely have to render the video data directly yourself. This is what vlcj's so-called DirectMediaPlayerComponent
is for. The essence of this approach is that the DirectMediaPlayerComponent
receives each frame of video data to render, and you would then render this yourself using a PixelWriter
or some other means that you come up with.
There is a vlcj-javafx project here https://github.com/caprica/vlcj-javafx, and this approach works up to Java7. It does not work with Java8 due to wrong threading, documented here: https://github.com/caprica/vlcj-javafx/issues/3
来源:https://stackoverflow.com/questions/24798120/display-vlcj-in-javafx-using-swingnode