更新要在桌面应用程序中显示的视频

时间:2014-02-04 11:49:56

标签: 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();

正如您所看到的,我呼吁在启动播放器内容之前和之后重新验证和重新绘制方法,以便在jframe中更新内容。

BTW,当我按下带有视频的窗口的关闭按钮时,它会显示一段视频。

提前致谢!!

1 个答案:

答案 0 :(得分:1)

我用硬编码的MediaPath做了一个简单的小例子工作示例,通过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);
    }


}

App.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添加为我的pom.xml的maven依赖项,所以我没有添加搜索路径。此外,还会处理Next Video Button上的点击,因此请在updateVideoPanel()方法中更新您的视频内容,在那里您可以拥有一个列表或数组,其中包含您要显示的所有视频。 希望这会有所帮助..