如何使用 vlcj 在 java 中制作视频播放器?

How to make video player in java using vlcj?

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;

public class VLCPlayer {

private final EmbeddedMediaPlayerComponent mediaPlayerComponent;

//This is the path for libvlc.dll
public static void main(String[] args) {
 NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\Program Files\VideoLAN\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
SwingUtilities.invokeLater(() -> {
    VLCPlayer vlcPlayer = new VLCPlayer();
});

}
private VLCPlayer() {

//MAXIMIZE TO SCREEN
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

JFrame frame = new JFrame();

mediaPlayerComponent = new EmbeddedMediaPlayerComponent();

frame.setContentPane(mediaPlayerComponent);

frame.setLocation(0, 0);
frame.setSize(300,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

mediaPlayerComponent.getMediaPlayer().playMedia("D:\centaur_1_xvid.avi");//Movie name which want to play
}
}

它 运行 现在很好,因为我正在使用 3.0.1 版本的 vlcj 和 jna 3.5.2 我想在视频播放器中添加速度(播放速率)功能我该怎么做,我想知道视频暂停和播放的时间。

很简单。 要获得时间或剩余时间,您可以使用 $T 表示时间,使用 $L 表示剩余时间。
在设置速度的情况下,您可以简单地使用 api 函数即

/**
 * Set the video play rate.
 * <p>
 * Some media protocols are not able to change the rate.
 *
 * @param rate rate, where 1.0 is normal speed, 0.5 is half speed, 2.0 is double speed and so on
 * @return -1 on error, 0 on success
 */
int setRate(float rate);

如果您想了解更多信息,请查看文档here

更新
要获得时间,请使用此

    /**
 * Get the current play-back time.
 *
 * @return current time, expressed as a number of milliseconds
 */
 long getTime();

查看上面的文档 link。

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
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;

public class VLCPlayer {

    private static JFileChooser filechooser = new JFileChooser();
//This is the path for libvlc.dll

    public static void main(String[] args) {
        NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\Program Files\VideoLAN\VLC");
        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
        SwingUtilities.invokeLater(() -> {
            VLCPlayer vlcPlayer = new VLCPlayer();
        });

    }

    private VLCPlayer() {

//MAXIMIZE TO SCREEN
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

        JFrame frame = new JFrame();
        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);

        JButton playbutton = new JButton("play");

        //playbutton.setIcon(new ImageIcon("C:/Users/biznis/Desktop/Newspaper/sangbadpratidin/d/download.png"));
        playbutton.setBounds(50, 50, 150, 100);
        // playbutton.addActionListener((ActionListener) this);
        p1.add(playbutton);

        JButton pausebutton = new JButton("pause");

        //  pausebutton.setIcon(new ImageIcon("pics/pausebutton.png"));
        pausebutton.setBounds(80, 50, 150, 100);

        p1.add(pausebutton);
        File ourfile;

        filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        filechooser.showSaveDialog(null);
        ourfile = filechooser.getSelectedFile();
        String mediaPath = ourfile.getAbsolutePath();

        EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
        mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
        frame.setLocation(100, 100);
        frame.setSize(1050, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        mediaPlayer.playMedia(mediaPath);
        pausebutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                mediaPlayer.pause();
                // or mediaPlayer.pause() depending on what works.
                final long time = mediaPlayer.getTime();
                System.out.println(time);
            }
        });
        playbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                mediaPlayer.play();
            }
        });
    }
}

这就是我解决问题的方法谢谢。