2

I’ve trimmed down the code to only the relevant parts and posted it below. The code works fine. The video plays when you run it but it doesn’t have a seekbar.

public class Screen {
//JFrmae
private JFrame frame;

// Panel which I add the canvas to
private JPanel pVid = new JPanel();

// Canvas
Canvas canvas = new Canvas();

// Embedded Media Player
EmbeddedMediaPlayer emp;


/**
 * Create the application.
 */
public Screen() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    //Frame
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    //Adding the panel to the frame
    frame.getContentPane().add(pVid);

    //Adding the canvas to the panel
    pVid.add(canvas);

    //Setting canvas size
    canvas.setSize(715, 402);

    //Loading the VLC native library
    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "lib");
    Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);

    //Initializing the media player
    MediaPlayerFactory mpf = new MediaPlayerFactory();

    //Misc
    emp = mpf.newEmbeddedMediaPlayer(new Win32FullScreenStrategy(frame));
    emp.setVideoSurface(mpf.newVideoSurface(canvas));

    //Video file name and playing
    String file = "video.mp4";
    emp.prepareMedia(file);
    emp.play();

    //pack method
    frame.pack();
}

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Screen window = new Screen();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

}

I’ve looked for an answer online for the last 4 days. Finally I decided to ask here. The official website for vlcj has pictures of a vlcj player they’ve made. There is a seekbar in those pictures. Link to the webpage which has the pics: http://capricasoftware.co.uk/#/projects/vlcj

They have a number of useful tutorials there but they don’t have any instructions for adding the seekbar.

Then I tried downloading their vlcj-player project from their GitHub page. It showed an error because it couldn’t resolve the “com.google.common.collect.ImmutableList” which is supposed to be imported. (At the moment I’m reading about ImmutableList and stuff and see if there’s a way to fix it.) Since I couldn’t figure that out yet, I looked for a class named seekbar or the like in their project. I couldn’t find any.

I also searched elsewhere online for the answer but I just couldn’t find it. I’d really appreciate any help. Thank you.

Edit:

(This edit is in response to the suggestion given to me by @caprica. Read their comment to this question and my reply to that in the comment to understand what I'm talking about here in this edit. I think it'll be useful for others in the future.)

All right, there must have been some problem with my Eclipse or computer. (I’ll type out how I fixed it at the end of this comment.) It’s working now. I’ll type out what I did step by step so that may be it’ll be useful to others in the future to download and install the project.

  1. Download the project.
  2. Import it as a Maven project. (Import > Maven > Existing Maven Project)
  3. Now in Eclipse right click the imported project and select Run As > Maven Install

And that’s it. Now you can just run the project normally. If you don’t know how to run the project, do it like this. Right click the project and select Run As > Java Application and then Select VlcjPlayer – uk.co.caprica.vlcplayer.

Alternatively you can open the class where the main method is and run it. VlcjPlayer class is where the main method is located. The class is in the package uk.co.caprica.vlcplayer.

The problem I faced was that somehow all the necessary files didn’t get downloaded when I ran it as Maven Install. But it worked fine in another computer. Since I knew where the files are downloaded to, I just copied the folder from that PC and put it in the same place in my PC. The folder name is ‘repository’. It’s location is C:\Users\User Name\ .m2. Perhaps Eclipse in this PC has some problem. I’ll reinstall it later to avoid problems in the future.

And this may be useful, the VLC that’s installed in this PC is 64 bit. Not sure if that makes a difference but mentioning it just in case.

Now that the app is working fine I will see the code and see how the seekbar is made. Thanks a lot @caprica for telling me that I should import it as a Maven project. :)

Dan
  • 95
  • 11
  • 1
    Everything you need is in that vlcj-player project, import the project from Github into your IDE as a Maven project and you should be fine (your Google collections library issue should be automagically resolved). There are a few tricks to implementing a seekbar properly, you can start in that project by looking for the PositionPane class – caprica Aug 08 '17 at 11:33
  • @caprica, I imported the project into Eclipse as a Maven project. I still get the exact error. I tried it a few times just to be sure. Not really sure why I still get the error. – Dan Aug 08 '17 at 17:56
  • I welcome @caprica's perspective; my goal was to cite the basic tutorial in the context of Swing design. – trashgod Aug 09 '17 at 01:01
  • @caprica, it seems there was a problem with my Eclipse. The project doesn't show any errors now. I've typed out how it was fixed as an edit in my question for others to see. Thanks a lot. :) – Dan Aug 09 '17 at 14:13

2 Answers2

1

The Basic Controls tutorial shows the essential approach: Add a panel of buttons to the frame and give each button an ActionListener that invokes the relevant media player command. As an example, this notional Rewind button would "skip backwards 10 seconds (-10,000 milliseconds)."

JPanel controlsPane = new JPanel();
JButton rewindButton = new JButton("Rewind");
rewindButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        mediaPlayerComponent.getMediaPlayer().skip(-10000);
    }
});
controlsPane.add(rewindButton);
frame.add(controlsPane, BorderLayout.SOUTH);

The software design is up to you, but you should at least be aware of

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • One big hurdle is that I don't know the name of component I'd have to create. I had actually figured out how to make the pause and skip buttons. Because I knew I'd have to create JButtons and use ActionListeners and the like. But for the seekbar, I don't really know what I should create. – Dan Aug 08 '17 at 18:01
  • @caprica may have something better, but I'd let a `Timer` invoke `skip`; skipping a second avery 250 ms would be 4x. – trashgod Aug 09 '17 at 00:59
  • that's pretty neat. Thanks. I'll follow your advice if I can't figure out how to make the seekbar from the project. Thanks. – Dan Aug 09 '17 at 14:15
  • Yeah, absolutely. I'll do that if I can't figure out how to make the seekbar. Thanks. :) – Dan Aug 10 '17 at 05:41
0

All right, guys. I’ve figured out how to do it. I’m not sure how it was done in the official Vlcj project but I’ve figured out my own simple way by learning from the official project.

It just takes a few lines of code. It’s very simple. These are the steps you have to follow:

  1. Create a JSlider.
  2. To the JSlider, add a mouseMotionListener (‘mouseDragged’ to be exact).
  3. Inside that put in the code which would update the video position based on the change in the JSlider.
  4. Create a Timer.
  5. Put the code inside it to set the value of the JSlider based on the position of the video.

And that’s it!

This is the code. It comes inside the initialize() method which you can see in the code I’ve given in the question. (And of course you'll also have to create the JSlider and add it to the panel. I haven't shown the code since it's simple.)

js.addMouseMotionListener(new MouseMotionAdapter() {
    @Override
        public void mouseDragged(MouseEvent e) {
        if (js.getValue() / 100 < 1) {
        emp.setPosition((float) js.getValue() / 100);
        }
    }
});


Timer timer = new Timer(100, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        js.setValue(Math.round(emp.getPosition() * 100));
    }

});

timer.start();

Some explanation.

The value you get when you use emp.getPosition always seems to be in decimals. It’s something like 0.1334344 at the start of the video and it’s something like 0.998988 at the end. But the value of JSlider is in int. From 0 to 100. So in the mouseMotionListener added to the JSlider I’ve converted the int value of the JSlider to float by dividing it by 100.

And in the action listener inside the timer I’ve multiplied the value of the video position by 100 and then rounded it off to make it an int value. So that value can be set in the JSlider to make it move in sync with the video.

I’m sure the code is rudimentary and there could be some best practices which I may not have followed. Sorry about that but I’m just getting into java by learning the stuff which I find interesting. Those who are good at java and have used such code in an actual project can comment below with how it can be improved.

Dan
  • 95
  • 11
  • Listening to "position changed" events from the media player, then updating your slider on the UI thread, would be better than polling the media player every 100ms IMO. – caprica Aug 15 '17 at 21:35
  • That sounds much better. So for that I'll have to create a custom listener right? I've looked for and found some tutorials on that now. I'll learn how to do it and update it in the answer here. Thanks again! – Dan Aug 16 '17 at 05:09