尝试将vlcj媒体播放器嵌入JPanel内的WindowsCanvas中 [英] Trying to embed vlcj media player in a WindowsCanvas inside a JPanel

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

问题描述

我正在尝试在JPanel中使用vlcj播放视频,但这对我不起作用.我收到的消息异常是"java.lang.IllegalStateException:视频表面组件必须是可显示的",与

I'm trying to play a video using vlcj inside a JPanel but it doesn't work for me. The message exception I am getting is "java.lang.IllegalStateException: The video surface component must be displayable" which is the same problem as in Keep getting an Error "Component must be displayable".

包含画布和vlcj播放器的JPanel的代码是这样的:

The code of the JPanel which contains the canvas and the vlcj player is this:

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.我认为这根本不重要.

MyJFrame extends JFrame and only contains the MyJPanel JPanel. I think it's not important at all.

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()调用放在单独的方法中,并在创建整个帧并将其设置为可见后,在 之后调用它.

You're trying to play the media before the frame containing the canvas has been set to be visible. You'll need to put the playMedia() call in a separate method, and call it after the entire frame has been created and set as visible.

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

If you still want it to play straight off, just call the relevant method after you've created and made your frame visible:

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

...显然,您需要在MyJFrame上定义startPlaying(),但随后它应该立即开始播放.您只需要先将框架设置为可见即可.

...obviously you'll need to define startPlaying() on MyJFrame, but then it should start playing straight off. you just need to set the frame visible first.

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

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