系统相关
首页 > 系统相关> > java – 尝试将vlcj媒体播放器嵌入到JPanel中的WindowsCanvas中

java – 尝试将vlcj媒体播放器嵌入到JPanel中的WindowsCanvas中

作者:互联网

我正在尝试在JPanel中使用vlcj播放视频,但它对我不起作用.我得到的消息异常是“java.lang.IllegalStateException:视频表面组件必须是可显示的”,这与Keep getting an Error “Component must be displayable”中的问题相同.

包含画布和vlcj播放器的JPanel代码如下:

import javax.swing.JPanel;

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;

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

import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import uk.co.caprica.vlcj.runtime.windows.WindowsCanvas;

public class MyJPanel extends JPanel {
private EmbeddedMediaPlayer player;
private WindowsCanvas canvas;

public MyJPanel() {
    canvas = new WindowsCanvas();
    add(canvas);
    revalidate();
    repaint();

    canvas.setVisible(true);

    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
    player = mediaPlayerFactory.newEmbeddedMediaPlayer();

    CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas);

    player.setVideoSurface(videoSurface);
    player.playMedia("v.avi");   // This sentence throws the exception.
}
}

MyJFrame扩展了JFrame,只包含MyJPanel JPanel.我认为这根本不重要.

import javax.swing.JFrame;

public class MyJFrame extends JFrame {
protected MyJPanel myJPanel;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MyJFrame frame = new MyJFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public MyJFrame() {
    myJPanel = new myJPanel();
    add(myJPanel);
}   
}

提前致谢.

解决方法:

在包含画布的帧已设置为可见之前,您正尝试播放媒体.您需要将playMedia()调用放在一个单独的方法中,并在创建整个帧并将其设置为可见后调用它.

编辑:

如果你仍然希望它直接播放,只需在创建后调用相关方法并使框架可见:

MyJFrame frame = new MyJFrame();
frame.setVisible(true);
frame.startPlaying();

…显然你需要在MyJFrame上定义startPlaying(),但是它应该直接开始播放.你只需要首先设置框架可见.

标签:java,jpanel,video,vlcj
来源: https://codeday.me/bug/20190530/1183248.html