I have created a java swing application where i am running a video in canvas using VLCJ. Everything does fine but now i need add play/pause buttons and also sliders. So my question is how to add an actionlistener which would pause my video. Here is my code
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
}
}
On your Action Performed:
@Override
public void actionPerformed(ActionEvent ae) {
Object obj = ae.getSource();
if(obj == btnPlayButton){
//Play Your Video
}
if(obj == btnPauseVideo){
//PAuse your Video
}
}
You need to have your mediaPlayer
reference declared as a class field rather than an automatic heap variable as you have now.
The main reason for doing that is to make sure it is not garbage collected - otherwise it becomes eligible for garbage collection when your constructor method exits. This is necessary because that media player component is wrapping a native media player component, and if your Java object instance disappears the native media player will crash some time later - when it tries to call back into your Java object, it's not there.
The same applies to your mediaPlayerFactory
reference.
Moving the mediaPlayer
variable to a class field also makes it possible to easily implement your action listener:
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == btnPlayButton) {
mediaPlayer.getMediaPlayer().play();
}
else if (obj == btnPauseVideo) {
mediaPlayer.getMediaPlayer().pause();
}
}
There are many ways to implement those listeners, eg you could have one listener per button rather than sharing the listener like this.
There are lots of examples in the vlcj test sources that show precisely how to do this.
https://github.com/caprica/vlcj/tree/master/src/test/java/uk/co/caprica/vlcj/test
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.