在JPanel中嵌入vlcj播放器 [英] Embed vlcj player in JPanel

查看:275
本文介绍了在JPanel中嵌入vlcj播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在jpanel中添加vlcj播放器.

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.

这是我在jframe中的主类代码

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");

}
}

我得到的错误如下:

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)

任何帮助将不胜感激!

谢谢.

推荐答案

已解决:忘记了将EmbeddedMediaPlayerComponent添加到JPanel.

Solved : forgot to add the embeddedMediaPlayerComponent to the JPanel.

这篇关于在JPanel中嵌入vlcj播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆