java - 更新视频以在桌面应用程序中显示

标签 java swing vlcj

我有一个使用 vclj 和 swing 的桌面应用程序。我有一个框架(JFrame 组件),其中包含使用 vclj (VideoPanel 的实例)创建的视频。问题是我需要更新视频以在运行时在此 jframe 中显示。当我尝试这样做时,我不知道它有效。相反,视频 vclj 的组件以黑色显示,而不显示任何视频。

视频组件代码:

public class VideoPanel extends JPanel {

    private EmbeddedMediaPlayerComponent mymediaPlayer;
    private EmbeddedMediaPlayer mediaPlayer;

    private Canvas canvas;

   public VideoPanel() {

       setLayout(new BorderLayout(10, 10)); 

       Canvas canvas_1 = new Canvas();
       add(canvas_1, BorderLayout.CENTER);

       NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcPath);
       Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);

       MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
       CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas_1);
       mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
       mediaPlayer.setVideoSurface(videoSurface);

   }

   public void startPlayer() {
       mediaPlayer.playMedia(mediaPath);    
   }

}

每次我尝试更新 jframe 中显示的视频时都会调用代码:

// Remove last video shown
getContentPane().removeAll();

// Create new video vclj        
video = new VideoPanel();

// add video to jframe
add(video, BorderLayout.CENTER);

setVisible(true);

// Update hierarchy of components and repaint
revalidate();
repaint();

// Start the player
video.startPlayer();

// Update hierarchy of components and repaint
revalidate();
repaint();

setVisible(true);

pack();

如您所见,我在启动播放器之前和之后调用 revalidate 和 repaint 方法,以便在 jframe 中更新内容。

顺便说一句,当我按下包含视频的窗口的关闭按钮时,它会显示视频的一帧。

提前致谢!!

最佳答案

我用硬编码的 MediaPaths 制作了一个简单的小示例工作示例,通过您的 mediaPath 变量替换它:

VideoFrame.java

package de.professional_webworkx.vlcj;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class VideoFrame extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private VideoPanel videoPanel;

    public VideoFrame() {
        initializeGUI();
    }

    private void initializeGUI() {

        JPanel buttonPanel = createButtonPanel();

        this.setTitle("MyVideoApp");
        this.setSize(1024, 768);
        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        videoPanel = new VideoPanel("/home/ottp/Videos/Test.ogv");
        this.getContentPane().add(buttonPanel, BorderLayout.NORTH);
        this.getContentPane().add(videoPanel, BorderLayout.CENTER);
        this.setVisible(true);
        videoPanel.startPlayer();
    }

    private JPanel createButtonPanel() {

        JPanel buttonPanel  = new JPanel(new GridLayout(1, 2));
        JButton nextVideo   = new JButton("Next Video");
        nextVideo.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                updateVideoPanel();
            }


        });
        buttonPanel.add(nextVideo);
        JButton prevVideo   = new JButton("Prev Video");
        buttonPanel.add(prevVideo);
        return buttonPanel;
    }

    private void updateVideoPanel() {
        this.remove(videoPanel);
        videoPanel = new VideoPanel("/home/ottp/Videos/Example.ogv");
        this.getContentPane().add(videoPanel, BorderLayout.CENTER);
        videoPanel.startPlayer();
        revalidate();
    }

}

VideoPanel.java

package de.professional_webworkx.vlcj;

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

import javax.swing.JPanel;

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 com.sun.jna.Native;

public class VideoPanel extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private EmbeddedMediaPlayer mediaPlayer;
    private Canvas _canvas;
    private String mediaPath;

    public VideoPanel(final String mediaPath) {

        this.mediaPath  = mediaPath;
        setLayout(new BorderLayout(10, 10));
        _canvas = new Canvas();
        add(_canvas, BorderLayout.CENTER);

        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
        MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
        CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(_canvas);
        mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
        mediaPlayer.setVideoSurface(videoSurface);
    }

    public void startPlayer() {
                mediaPlayer.playMedia(mediaPath);
    }


}

应用程序.java

package de.professional_webworkx.vlcj;

import javax.swing.SwingUtilities;

import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaListPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;

import com.sun.jna.Native;

/**
 * Hello world!
 *
 */
public class App 
{
    private static EmbeddedMediaListPlayerComponent component;
    public static void main( String[] args )
    {
        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new App();

            }
        });
    }

    private App() {
        new VideoFrame();
    }
}

这是一个 Maven 项目,我将 vlcj 作为 Maven 依赖项添加到我的 pom.xml 中,所以我不必添加搜索路径。并且还处理了对下一个视频按钮的点击,因此在updateVideoPanel()方法中更新您的视频内容,您可以在其中包含您想要显示的所有视频的列表或数组。 希望这有帮助..

关于java - 更新视频以在桌面应用程序中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21551694/

相关文章:

java - 为什么通过 bufferedreader 获取输入会在我的系统中抛出垃圾值?

swing - JTable TableCellRenderer背景与NimbusLookAndFeel颜色问题

java - 如何限制用户键入时 JTextPane 中的字符数 (Java)

java - 无法加载libvlc : Unable to load library 'vlc'

Java:如何将图像转换为字节[]

c# - ushort 等价物

Java 7 使用 Web Start 破坏了 OS X 上的 SWT 应用程序

java - 设置用作 JTable RowFilter 时 JComboBox 中显示的选项数量

java - 将 MouseListener 与 vlcj 一起使用

java - 为什么我的 libVLC 程序在尝试绑定(bind)捕获设备时会死锁?