Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

API VLCj: Creating Java Audio/Video Players

See in this article how to work with VLCj API to create your own audio/video players for Windows/Linux in Java based on VLC.

VLC is a famous open-source video player, which runs on multiple platforms and plays various types of audio/video format.



For this article, we will use version 2.2.1, which can be downloaded from: http://download.videolan.org/pub/videolan/vlc/2.2.1/. Download it and unzip the folder in an easy to find directory, like D or C disks root.

Note: The operating system will be Windows 8.1 - 64 bits.

At the site http://code.google.com/p/vlcj/, we can find the VLCj API, which lets you interact with the player VLC, accessing almost all of its features. The version used is 3.8.0, which can be downloaded from the following link: https://github.com/caprica/vlcj/archive/master.zip.

Note: The source code of this article also works on Windows 32-bit. However, you must download the 32-bit Oracle JDK (even if your system is 64-bit), and install the Win32 version of VLC (link above) for the examples to work correctly.

Setting dependencies

We'll also use Eclipse IDE as the tool to develop the example shown here in the article. Make sure to download a version compatible with JEE features, including Maven plugin. So, create a new Maven project and, at the first window opened, check the option "Create a simple project (skip archetype selection)" and click Next. Then fill the fields as shown in Figure 1.

Figure 1. Configuring maven project settings.

As dependencies, you'll need to have a JDK 1.6 or 1.7 configured in your machine. All the other dependencies of the VLC API will be loaded by Maven itself. So, open pom.xml file and update it with the code shown in Listing 1.

Listing 1. XML of needed dependencies for the example.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>mrbool.vlc</groupId>
	<artifactId>mrbool.vlc.example</artifactId>
	<version>0.0.1-SNAPSHOT</version>


	<dependencies>
		<dependency>
			<groupId>uk.co.caprica</groupId>
			<artifactId>vlcj</artifactId>
			<version>3.8.0</version>
		</dependency>
	</dependencies>
</project>

After this, if you access your Java Build Path (via project menu Properties > Java Build Path > Libraries) you must see the same as in Figure 2.

Figure 2. Libs added to the Java Classpath.

Note: JNA is an API that allows you to access native code from Java.

Testing LibVlc

Let's create a new class called InformationLib and put the code present in Listing 2 on it.

Listing 2. Test class to check vcl information.

package mrbool.vlc.example;


import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import uk.co.caprica.vlcj.runtime.x.LibXUtil;


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


public class InformationLib {


    public static void main(String[] args) throws Exception {
    	NativeLibrary.addSearchPath(
                RuntimeUtil.getLibVlcLibraryName(), "d:/vlc-2.2.1");
        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
        LibXUtil.initialise();
    	
        System.out.println("  version: {}" + LibVlc.INSTANCE.libvlc_get_version());
        System.out.println(" compiler: {}" + LibVlc.INSTANCE.libvlc_get_compiler());
        System.out.println("changeset: {}" + LibVlc.INSTANCE.libvlc_get_changeset());
    }
}

In Listing 2 you can see the output.

Listing 2. Output of previous code.

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  version: {}2.2.1 Terry Pratchett (Weatherwax)
 compiler: {}gcc version 4.9.2 (GCC)
changeset: {}2.2.1-0-ga425c42

LibVlc is an interface that represents some of the native features of VLC (exposed by libvlc.dll and libvlc.so on Windows and Linux, respectively). This interface has an INSTANCE attribute, which is a concrete implementation of LibVlc interface, allowing access to all the methods defined therein. Javadoc with the listing of all available methods of this interface, as well as the rest of the API, can be downloaded from the following link: https://github.com/caprica/vlcj.

Creating the audio/video player

Let's start by the class MinimalPlayerTest, which will save all the main code of the implementation. Create it and add code in Listing 3 to it.

Listing 3. Code of main class - MinimalTestPlayer

package mrbool.vlc.example;


import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;


import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import uk.co.caprica.vlcj.runtime.x.LibXUtil;


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


public class MinimalTestPlayer {


	public MinimalTestPlayer() {
		registerLibrary();
	}


	/**
	 *  Runs the audio/video file
	 */
	public void play(final String filename) {
		final Canvas videoSurface = new Canvas();
		final Frame frame = buildFrame(videoSurface);
		final List<String> vlcArgs = new ArrayList<String>();


		configureParameters(vlcArgs);


		final EmbeddedMediaPlayer mediaPlayer = createPlayer(vlcArgs, videoSurface);
		mediaPlayer.playMedia(filename);
	}


	/**
	 *  Important: Notice where is the libvlc, which contains all native functions to manipulate the player
	 * 
	 *  Windows: libvlc.dll
	 *  Linux: libvlc.so
	 */
	private void registerLibrary() {
		NativeLibrary.addSearchPath(
				RuntimeUtil.getLibVlcLibraryName(), "d:/vlc-2.2.1");
		Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
		LibXUtil.initialise();
	}


	/**
	 * Criate the frame where movie will be played
	 */
	private Frame buildFrame(final Canvas videoSurface) {
		final Frame f = new Frame("Test Player");
		f.setSize(800, 600);
		f.addWindowListener(new WindowAdapter() {


			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		f.setLayout(new BorderLayout());
		f.add(videoSurface, BorderLayout.CENTER);
		f.setVisible(true);
		return f;
	}


	/**
	 * Configure VLC parameters
	 */
	private void configureParameters(final List<String> vlcArgs) {
		vlcArgs.add("--no-plugins-cache");
		vlcArgs.add("--no-video-title-show");
		vlcArgs.add("--no-snapshot-preview");


		// Important, if this parameter would not be set on Windows, the app won't work
		if (RuntimeUtil.isWindows()) {
			vlcArgs.add("--plugin-path=D:\\vlc-2.2.1\\plugins");
		}
	}


	/**
	 * Build the player
	 */
	private EmbeddedMediaPlayer createPlayer(final List<String> vlcArgs, final Canvas videoSurface) {
		EmbeddedMediaPlayerComponent mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
		EmbeddedMediaPlayer embeddedMediaPlayer = mediaPlayerComponent.getMediaPlayer();


		MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(vlcArgs.toArray(new String[vlcArgs.size()]));
		mediaPlayerFactory.setUserAgent("vlcj test player");
		embeddedMediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(videoSurface));
		embeddedMediaPlayer.setPlaySubItems(true);


		return embeddedMediaPlayer;
	}


	public static void main(String[] args) throws InterruptedException {
		MinimalTestPlayer player = new MinimalTestPlayer();
		// Could be MP4, AVI, MOV, MKV, WMA, MPG, MP3, WAV, etc.
		player.play("D:\\videos\\video.mp4");


		// Waits until the player window be closed
		Thread.currentThread().join();
	}
}

The listing is fully commented, so you can understand it better by reading the code. Please, make sure to place a video called "video.mp4" into the video folder at D disk, or change the path into main method. Do the same with VLC unzipped folder in method configureParameters. Otherwise, the example won't work. Run the class and you'll see the same result as in Figure 3.

Figure 3. Test Player executing.

Conclusion

Although the examples were run on Windows, the same is valid for Linux (runs very well too). Now, you can create your own audio/video player, customize it and even control it via internet, mobile, etc.

I hope this article assists developers to explore this interesting API.

Thank you and see you soon!



Web developer and passioned for web design, SEO and front end technologies.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login