lördag 3 december 2016

Playing video in jMonkeyEngine 3 using vlcj

A little while ago I made a basic example of how to play video on a texture in jMonkeyEngine 3. I used vlcj as the backing video player due to its relative ease of use and good tutorials. I'm making this companion post to describe the method (code) and how to set it up (config).

This describes how to set it up in Netbeans on Windows (should be the same for other Maven projects).
First, set up vlcj and build it:
  1. Download vlcj.
  2. Set it up as a new project in Netbeans.
  3. Change config to 'release'
  4. Download suitable VLC.
  5. Place libvlc and libvlccore in project root/win32-x86-64
  6. Distribution ends up as a zip the project root/target folder.
Now you can set up a project that will use the vlcj build and point it to use the .jars in the target folder as libraries.

I created a JmeRenderCallbackAdapter class based on this Direct Rendering tutorial. The class takes a Texture2D as an input and will update its ImageBuffer on every onDisplay call.
The method is very basic and there's an improvement to be made by using the native buffer directly.

The TestVlcj class shows how to display the result.
The setupView method creates a Quad and an Unshaded material to display the video on.
It also creates a Texture2D (which is passed to JmeRenderCallbackAdapter) and an Image the same size as the application.
The way BufferFormatCallback and DirectMediaPlayerComponent work are again taken directly from the Direct Rendering tutorial.
TestVlcj takes a path to a video as input argument. The input argument can be overridden and mediaFile can be hard coded for testing.