Display VlcJ in JavaFX using SwingNode

只谈情不闲聊 提交于 2019-12-08 03:24:17

问题


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.


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!