简体   繁体   中英

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
}
}

its running fine now as i'm using 3.0.1 version of vlcj and jna 3.5.2 i want to add speed(playbackrate) functionality in video player how can i do that and i want to know the timing when video paused and when played.

Very simple. To get the time or time left, You can use $T for time and $L for time left.
And in case of setting speed, You can simply use the api function namely

/**
 * 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);

If You want to know more, check the documentation here

UPDATE
To get the time , use this

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

check docs link above.

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();
            }
        });
    }
}

This is how i solved the problem Thanks.

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.

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