Caprica Software

vlcj 3.x Tutorial

Basic Audio Player

So far we have created media players that can play audio and video. Here we build a media player for playing audio only. An audio player has no video surface and is a little bit simpler to use than a full-blown audio and video player.


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.

This tutorial will not have a user interface, it will be a command-line application that plays one audio file and exits.

An audio media player can of course be part of an application with a user interface, but we've seen how to integrate a vlcj media player into such an application already so there's no point repeating it here.


Audio Media Player Component

The first thing to do is to create the audio media player component...

mediaPlayerComponent = new AudioMediaPlayerComponent();

Here we simply create the component - there is no user interface or video surface to think about.


Add Listeners

Media player event listeners are exactly the same for audio media players as they are for the media players we've seen already.

mediaPlayerComponent.getMediaPlayer().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
@Override
public void stopped(MediaPlayer mediaPlayer) {
exit(0);
}

@Override
public void finished(MediaPlayer mediaPlayer) {
exit(0);
}

@Override
public void error(MediaPlayer mediaPlayer) {
exit(1);
}
});

In this tutorial we simply exit the application in response to various events.


Playing an Audio File

Playing audio works just the same as playing any other media file...

audioMediaPlayerComponent.getMediaPlayer().playMedia(mrl);

Summary

Here is the finished code, it is very simple:

Tutorial.java
package tutorial;

import uk.co.caprica.vlcj.component.AudioMediaPlayerComponent;
import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerEventAdapter;

public class Tutorial {

private final AudioMediaPlayerComponent mediaPlayerComponent;

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

tutorial.start(args[0]);
try {
Thread.currentThread().join();
}
catch(InterruptedException e) {
}
}

private Tutorial() {
mediaPlayerComponent = new AudioMediaPlayerComponent();

mediaPlayerComponent.getMediaPlayer().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
@Override
public void stopped(MediaPlayer mediaPlayer) {
exit(0);
}
@Override
public void finished(MediaPlayer mediaPlayer) {
exit(0);
}
@Override
public void error(MediaPlayer mediaPlayer) {
exit(1);
}
});
}

private void start(String mrl) {
mediaPlayerComponent.getMediaPlayer().playMedia(mrl);
}

private void exit(int result) {
mediaPlayerComponent.release();
System.exit(result);
}
}

We assume that the name of the audio file is passed as the first command-line argument.

Of note in this code, but not directly related to vlcj, in the main method we join() the current thread - i.e. effectively we wait forever. Without this the application would exit immediately after the main method terminates. If this application had a user interface, then the user interface thread keeps the application alive even after the main method terminates. This application will exit when one the media player events we're listening for is raised.

This tutorial has shown how to create a very simple audio player. There is really very little vlcj code in here. This can be used as the foundation to create a full-blown application with a user interface.