Embed vlcj player in JPanel

2019-09-01 15:42发布

Hi I am trying to add a vlcj player in a jpanel.

I am using this post and this post to realise this. I am also using a tutorial on how to use embeddedMediaPlayerComponent for this. I already got this working where the video is played in a JFrame. I want to place the vlcj-player in a JPanel now, but I am still getting erroneous behavior.

Here is my code of the main class with the jframe in

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Observable;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.sun.jna.NativeLibrary;

import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;

public class VlcPlay extends JFrame {

public static void main(final String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new VlcPlay();
        }
    });
}

private VlcPlay() {
    this.setTitle("Test");

    /* Initialize the JFrame */
    this.setLocation(100, 100);
    this.setSize(1050, 600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    TestClass p = new TestClass(); // The panel

    this.add(p, BorderLayout.CENTER); // Add the panel

    this.setVisible(true);

    p.play();


}
}

And this is the code where the mediaplayer is embedded :

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;

import javax.swing.JPanel;

import com.sun.jna.NativeLibrary;

import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;


public class TestClass extends JPanel {
private EmbeddedMediaPlayerComponent ourMediaPlayer;
TestClass(){
    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");

    ourMediaPlayer = new EmbeddedMediaPlayerComponent();



    /* Set the canvas */
    Canvas c = new Canvas();
    c.setBackground(Color.black);
    c.setVisible(true);

    /* Set the layout */
    this.setLayout(new BorderLayout());

    /* Add the canvas */
    this.add(c, BorderLayout.CENTER);
    this.setVisible(true);



}
public void play() {
    /* Play the video */
    ourMediaPlayer.getMediaPlayer().playMedia("Movie on 25-04-14 at 11.41.mp4");

}
}

EDIT : the errors i get are the following :

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: The video surface component must be displayable
    at uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface.attach(CanvasVideoSurface.java:75)
    at uk.co.caprica.vlcj.player.embedded.DefaultEmbeddedMediaPlayer.attachVideoSurface(DefaultEmbeddedMediaPlayer.java:156)
    at uk.co.caprica.vlcj.player.embedded.DefaultEmbeddedMediaPlayer.onBeforePlay(DefaultEmbeddedMediaPlayer.java:315)
    at uk.co.caprica.vlcj.player.DefaultMediaPlayer.play(DefaultMediaPlayer.java:705)
    at uk.co.caprica.vlcj.player.DefaultMediaPlayer.playMedia(DefaultMediaPlayer.java:222)
    at TestClass.play(TestClass.java:41)
    at VlcPlay.<init>(VlcPlay.java:42)
    at VlcPlay.<init>(VlcPlay.java:28)
    at VlcPlay$1.run(VlcPlay.java:23)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Any help would be greatly appreciated!

Thanks ahead.

1条回答
聊天终结者
2楼-- · 2019-09-01 16:43

Solved : forgot to add the embeddedMediaPlayerComponent to the JPanel.

查看更多
登录 后发表回答