Search code examples
javaswingapivlcjsubtitle

Add subtitle and basic operation panel in VLCJ @ Java


Good day colleagues!

I had several trouble using VLCJ and other Java media API-s.

1) I'd add a simple *.srt file to my EmbeddedMediaPlayerCompononent, bot how is this possible?

2) Also, how can I configurate the VLC lib in an x64 Windows OS?

NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files (x86)\\VideoLAN\\VLC");
                    Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(),libVlc.class);

This not works well.

3) How could I add basic operation interface to my EmbeddedMediaPlayerCompononent like pause/play button?

Thank you, best regards! :)

My "VideoPlayer" class

    package GUI.MediaPlayer;
    import java.awt.BorderLayout;
    import java.io.IOException;

    import javax.swing.JFrame;
    import javax.swing.JOptionPane;

    import uk.co.caprica.vlcj.binding.LibVlc;
    import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
    import uk.co.caprica.vlcj.runtime.RuntimeUtil;
    import StreamBean.UIBean;

    import com.sun.jna.Native;
    import com.sun.jna.NativeLibrary;

    public class VideoPlayer extends JFrame{
        private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
        public VideoPlayer(String videoURL) {

            String os = System.getProperty("os.name").toLowerCase();

            if(os.startsWith("win")){
                String registrytype = System.getProperty("sun.arch.data.model");
                System.out.println("a rendszered : " +os+" - " +registrytype+ " bites");
                if(registrytype.contains("32")){
                    //Windows 32 bites verzió
                    System.out.println("Belépett a 32-be");
                    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
                    Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
                }else if(registrytype.contains("64")){
                    //Windows 64 bites verzió
                    System.out.println("Belépett a 64-be");
                    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files (x86)\\VideoLAN\\VLC");
                    Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
                }else{
                    JOptionPane.showMessageDialog(null, "Kérem előbb telepítse a VLC lejátszót.");
                }

            }
            if(os.startsWith("mac")){
                //Mac OSX kiadáshoz  
                NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "/Applications/VLC.app/Contents/MacOS/lib/");
                Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
            }

            this.setTitle("Aktuális videó");
            this.setLayout(new BorderLayout());

            mediaPlayerComponent = new EmbeddedMediaPlayerComponent();

            this.add(mediaPlayerComponent,BorderLayout.CENTER);

            this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
                        //set the Jframe - this - resolution to the screen resoltuion
            new UIBean().setWindowSize(this);
            this.setVisible(true);

            mediaPlayerComponent.getMediaPlayer().playMedia(videoURL);
        }
    }

Solution

  • To set an external subtitle file:

    mediaPlayerComponent.getMediaPlayer().setSubTitleFile("whatever.srt");
    

    How you add a pause/play button is entirely up to you, it requires standard Swing code that is not particular to vlcj. You add buttons to your user interface, and link those buttons up with the media player by using event listeners. For example, this is one way:

    JButton playButton = new JButton("Play/Pause");
    playButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            mediaPlayerComponent.getMediaPlayer.pause();
        }
    });
    

    There are numerous reasons why the native library might not be found, but NativeLibrary.addSearchPath(...) certainly does work. You have to be sure you match the CPU architectures of your JVM and your VLC installation (32-bit JVM requires 32-bit VLC, 64-bit JVM requires 64-bit VLC). In most cases you should just use:

    new NativeDiscovery().discover();
    

    There are a whole bunch of step-by-step tutorials at http://capricasoftware.co.uk/#/projects/vlcj/tutorial