您好我正在尝试在jpanel中添加vlcj播放器。
我正在使用this post和this post来实现这一目标。我也在使用有关如何使用embeddedMediaPlayerComponent的教程。我已经将这个视频在JFrame中播放了。我现在想把vlcj-player放在JPanel中,但我仍然会出现错误的行为。
这是我在
中使用jframe的主类的代码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();
}
}
这是嵌入媒体播放器的代码:
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)
非常感谢任何帮助!
非常感谢。
答案 0 :(得分:0)
已解决:忘记将embeddedMediaPlayerComponent添加到JPanel。