javaswingvlcj

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.


Solution

  • 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.