我创建了一个java swing应用程序,我使用VLCJ在画布上运行视频。一切都很好,但现在我需要添加播放/暂停按钮和滑块。所以我的问题是如何添加一个可以暂停我的视频的actionlistener。这是我的代码
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
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.runtime.RuntimeUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import com.sun.media.MediaPlayer;
public class Player implements ActionListener{
JButton b1;
Player vid = null;
JButton playbutton, pausebutton;
public static void main(final String[] args) {
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files (x86)\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Player(args);
}
});
}
private Player(String[] args) {
JFrame frame = new JFrame("vlcj Tutorial");
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
Canvas c = new Canvas();
c.setBackground(Color.black);
JPanel p = new JPanel();
c.setBounds(100, 500, 1050, 500);
p.setLayout(new BorderLayout());
p.add(c, BorderLayout.CENTER);
p.setBounds(100, 50, 1050, 600);
frame.add(p, BorderLayout.NORTH);
JPanel p1 = new JPanel ();
p1.setBounds(100, 900, 105, 200);
frame.add(p1, BorderLayout.SOUTH);
playbutton =new JButton();
playbutton.setIcon(new ImageIcon("pics/playbutton.png"));
playbutton.setBounds(50, 50, 150, 100);
playbutton.addActionListener((ActionListener) this);
p1.add(playbutton);
pausebutton=new JButton();
pausebutton.setIcon(new ImageIcon("pics/pausebutton.png"));
pausebutton.setBounds(80, 50, 150, 100);
p1.add(pausebutton);
EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
// c.addComponentListener((ComponentListener) mediaPlayer);
mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
frame.setLocation(100, 100);
frame.setSize(1050, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
mediaPlayer.playMedia("D:\\Facebook.mp4");
//NativeLibrary.addSearchPath("libvlc", "C:\\Program Files (x86)\\VideoLAN\\VLC");
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
关于你的行动:
@Override
public void actionPerformed(ActionEvent ae) {
Object obj = ae.getSource();
if(obj == btnPlayButton){
//Play Your Video
}
if(obj == btnPauseVideo){
//PAuse your Video
}
}
答案 1 :(得分:0)
您需要将mediaPlayer
引用声明为类字段而不是现在的自动堆变量。
这样做的主要原因是确保它不是垃圾收集 - 否则当你的构造方法退出时它就有资格进行垃圾收集。这是必要的,因为媒体播放器组件正在包装本机媒体播放器组件,如果您的Java对象实例消失,本机媒体播放器将在一段时间后崩溃 - 当它尝试回调到您的Java对象时,它不存在。
这同样适用于您的mediaPlayerFactory
参考。
将mediaPlayer
变量移动到类字段也可以轻松实现您的动作侦听器:
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == btnPlayButton) {
mediaPlayer.getMediaPlayer().play();
}
else if (obj == btnPauseVideo) {
mediaPlayer.getMediaPlayer().pause();
}
}
有许多方法可以实现这些侦听器,例如:每个按钮可以有一个监听器,而不是像这样共享监听器。
vlcj测试源中有 lot 示例,精确地显示了如何执行此操作。
https://github.com/caprica/vlcj/tree/master/src/test/java/uk/co/caprica/vlcj/test