Caprica Software

vlcj 4.x Tutorial

Basic Controls

Building on previous tutorials, we will now add some basic playback controls.


Let's Get Started

You should now already have a basic template for how to create a vlcj application, so this tutorial will no longer duplicate all of the code each time - instead we'll just show the new code fragments.


New Layout

In the earlier tutorials, we set the media player component directly to be the content pane of the frame. But now we want to add some buttons, we need to improve the layout so we'll create a new JPanel to be the content pane, this panel will contain both the media player component and a separate panel for the buttons.

We change the code in the existing frame constructor:

JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());

mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);

JPanel controlsPane = new JPanel();

pauseButton = new JButton("Pause");
controlsPane.add(pauseButton);

rewindButton = new JButton("Rewind");
controlsPane.add(rewindButton);

skipButton = new JButton("Skip");
controlsPane.add(skipButton);

contentPane.add(controlsPane, BorderLayout.SOUTH);

frame.setContentPane(contentPane);
frame.setVisible(true);

So far we have only added standard Swing code, there is no new vlcj code added yet.

If you run this code now you will see the buttons. They are not yet hooked up to any actions so they won't actually do anything.


Add Listeners

To make the buttons do something useful, we add action listeners (before we show the frame):

pauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});

rewindButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});

skipButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});

Again, this is standard Swing code, there is still no new vlcj code added yet.


Add Media Player Behaviours

Now we have everything in place to make the buttons do something useful. The implementation of each listener is very simple.

The pause listener invokes a media player method that toggles the paused state of the media:

pauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.getMediaPlayer().controls().pause();
}
});

The rewind listener invokes a media player method to skip, in this case we skip backwards 10 seconds (-10,000 milliseconds):

rewindButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.getMediaPlayer().controls().skip(-10000);
}
});

The skip listener invokes a media player method to skip, in this case we skip forwards 10 seconds (10,000 milliseconds):

skipButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.getMediaPlayer().controls().skip(10000);
}
});

You now have the basics for adding controls to your user interface that can interact with the media player. Other controls can be added in the same way.


Summary

Here is the finished code:

package tutorial;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;

public class Tutorial {

private final Tutorial thisApp;

private final JFrame frame;

private final EmbeddedMediaPlayerComponent mediaPlayerComponent;

private final JButton pauseButton;

private final JButton rewindButton;

private final JButton skipButton;

public static void main(String[] args) {
thisApp = new Tutorial();
}

public Tutorial() {
frame = new JFrame("My First Media Player");
frame.setBounds(100, 100, 600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
mediaPlayerComponent.release();
System.exit(0);
}
});

JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());

mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
contentPane.add(mediaPlayerComponent, BorderLayout.CENTER);

JPanel controlsPane = new JPanel();

pauseButton = new JButton("Pause");
controlsPane.add(pauseButton);

rewindButton = new JButton("Rewind");
controlsPane.add(rewindButton);

skipButton = new JButton("Skip");
controlsPane.add(skipButton);

contentPane.add(controlsPane, BorderLayout.SOUTH);

pauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.mediaPlayer().controls().pause();
}
});

rewindButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.mediaPlayer().controls().skip(-10000);
}
});

skipButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.mediaPlayer().controls().skip(10000);
}
});

frame.setContentPane(contentPane);

mediaPlayerComponent.getMediaPlayer().media().play(args[0]);
}
}

This tutorial has shown how to hook up user interface controls to interact with the native media player. The actual vlcj code is minimal and very simple. Most of the code deals with the creation of the user interface and the hooking up of event listeners.