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:
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):
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:
The rewind listener invokes a media player method to skip, in this case we skip backwards 10 seconds (-10,000 milliseconds):
The skip listener invokes a media player method to skip, in this case we skip forwards 10 seconds (10,000 milliseconds):
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:
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.