简体   繁体   English

如何将动作侦听器添加到视频应用程序

[英]How to add Actionlisteners to a Video application

I have created a java swing application where i am running a video in canvas using VLCJ. 我创建了一个Java Swing应用程序,使用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. 您需要将mediaPlayer引用声明为类字段,而不是像现在一样声明为自动堆变量。

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. 这是必需的,因为该媒体播放器组件包装了一个本地媒体播放器组件,并且如果您的Java对象实例消失了,则本地媒体播放器将在一段时间后崩溃-当它尝试回调您的Java对象时,它就不存在了。

The same applies to your mediaPlayerFactory reference. 这同样适用于您的mediaPlayerFactory参考。

Moving the mediaPlayer variable to a class field also makes it possible to easily implement your action listener: mediaPlayer变量移动到类字段也可以轻松实现操作侦听器:

@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. vlcj测试源中有很多示例,它们精确地显示了如何执行此操作。

https://github.com/caprica/vlcj/tree/master/src/test/java/uk/co/caprica/vlcj/test https://github.com/caprica/vlcj/tree/master/src/test/java/uk/co/caprica/vlcj/test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM