Using VLCJ to draw video onto a texture

Whilst looking for a way to draw a video onto a texture using jMonkey, I couldn’t find a working example, so after a bit of work I managed to come up with something relatively simple that works and shows a video painted onto a texture.

There are problems - as listed below - but it will at least give you a head start to work with something.

Caveats

  • VLCJ is GPL - probably going to restrict you if you don’t want to publish your code as GPL.
  • VLC Player must be installed on the client machine - or alternatively you can ship the required files with your built project.
  • The 64bit version of VLC player is a bit “dodgy” - It is recommended that you use the 32bit version, and thus compile against the 32bit version of java because wont work cross-platform.

Links:
VLC Player: Official download of VLC media player, the best Open Source player - VideoLAN
VLCJ: http://capricasoftware.co.uk/#/projects/vlcj

Probably a good idea to read through the tutorials as well while you’re in a reading mood :sunny:

Useage:

// the path to your video file - network paths work too.
String filePath = "\\\\NSA310\\video\\Movies\\Shrek 2 (2004).avi";

// the width and height of the movie...
final int width = 704, height = 384;

VLCMediaPlayer vlc = new VLCMediaPlayer(filePath, width, height);
        
// a quad sized in the correct proportion to the video...
Quad quad = new Quad(7.04f, 3.84f);

Geometry geom = new Geometry("quad", quad);

Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", vlc.getTexture());
geom.setMaterial(mat);

rootNode.attachChild(geom);

vlc.play();

Feel free to use, abuse, etc…

9 Likes

You know that jme3-jfx is capable of using the jfx media system to play videos onto a texture?

From Caprica Software Limited

Why?

Support for playing media, especially video media, is generally poor on the Java platform. The Java Media Framework can be used to play some media, but it seems to be a neglected framework and has not been updated to play media encoded with modern codecs. JavaFX also provides the ability to play some media, but not all. In addition to playing media it is often desirable to be able to publish and consume streams of media across the network or the Internet and this too is not well supported by existing Java frameworks.

For these reasons, the usual solution is to use a native media framework and bind to it using native libraries by using the Java Native Interface (JNI), or the Java Native API (JNA) to simplify JNI.

There are a number of different native media players that could be used in this way, two such widely used media players are VLC and mplayer. The vlcj project was created to integrate VLC into Java applications.

Doesn’t hurt to have alternatives :slight_smile:

True, alternatives are always a good thing.

Small, but to me important, correction:

  • VLCJ is GPL - probably going to restrict you if you don’t want to publish your code as GPL.

…it’s not just publishing that is required… you have to GPL license your whole project, also.

Thanks @pspeed - ammended the original post with the corrected note.

Hi,
I followed your sample code but I keep on getting an error on line 77

public void play()
{ mediaPlayerComponent.getMediaPlayer().playMedia(this.videoPath);
}

I get an error on playMedia(). The error says " Cannot find Symbol"

Do you have vlc player installed? What is your operating system? Please post the entire stacktrace.

I haven’t used this in over 4 years. I’ll give it a whirl if I can’t resolve it for you.

@jayfella
Thank you so much for replying fast. We have installed a VLC player and the OS is Windows 10.

onStartScreen

java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at de.lessvoid.nifty.NiftyMethodInvoker.callMethod(NiftyMethodInvoker.java:157)

at de.lessvoid.nifty.NiftyMethodInvoker.performInvoke(NiftyMethodInvoker.java:115)

at de.lessvoid.nifty.Nifty$DelayedMethodInvoke.perform(Nifty.java:1481)

at de.lessvoid.nifty.Nifty.invokeMethods(Nifty.java:1457)

at de.lessvoid.nifty.Nifty.handleDynamicElements(Nifty.java:439)

at de.lessvoid.nifty.Nifty.access$1600(Nifty.java:87)

at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processEvent(Nifty.java:1727)

at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processMouseEvent(Nifty.java:1665)

at com.jme3.niftygui.InputSystemJme.handleMouseEvent(InputSystemJme.java:123)

at com.jme3.niftygui.InputSystemJme.onMouseButtonEventQueued(InputSystemJme.java:231)

at com.jme3.niftygui.InputSystemJme.forwardEvents(InputSystemJme.java:295)

at de.lessvoid.nifty.Nifty.update(Nifty.java:368)

at com.jme3.niftygui.InputSystemJme.endInput(InputSystemJme.java:112)

at com.jme3.input.InputManager.processQueue(InputManager.java:850)

at com.jme3.input.InputManager.update(InputManager.java:914)

at com.jme3.app.LegacyApplication.update(LegacyApplication.java:725)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:227)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:197)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:232)

at java.lang.Thread.run(Thread.java:748)

Caused by: java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: mygame.VLCMediaPlayer

at mygame.MyStartScreen.controlGame(MyStartScreen.java:157)

… 25 more

Hi @jayfella, we tried the solutions provided in the stackoverflow link, but it still doesn’t work.

In our source code…

In this picture, when we hover the “getMediaPlayer().” and there’s a list of possible methods to use like play() which opens up a description popup, and there’s no error when we use that.

However, in this picture, when we click on playMedia(String), there’s no java doc description popup which makes us think the function may no longer exist and that may be why we’re unable to run the program?

Thanks

For uploading screen shots, please use an image hosting service. Right now imgur.com is the best one, I think.

Wow. You used an actual camera to take a picture of your computer screen. That’s awesome.

Anyway. Lack of java doc just means the method wasn’t documented by the programmer. I’ll take a look in the morning for you.

Great! Thank you so much for your help in advance :slight_smile:

For what its worth, I also did this (but one year later, so I should have used the search function on the forum before attempting it). I haven’t used it either since then, but maybe it works out of the box:

1 Like

I checked it over. It worked for me. The steps I took were:

  • Make sure I’m using version 3.x (and not 4.x) of the VLCJ library.
 compile group: 'uk.co.caprica', name: 'vlcj', version: '3.7.0'

If you try to mix 32bit/64bit VLC player/java it won’t work - for me it spits out a huge descriptive error log.

If memory serves - and this was 4 years ago - I think version 3 of VLCJ had issues with the 64bit player. I’m not sure if version 4 resolves this but it doesn’t seem backwards compatible - as expected - since the major version changed.

It may be that upgrading to VLCJ 4 makes all of this a non-isse.

I took it upon myself to make a VLCJ 4 variant. You might want to amend the data on line 50, 51 and 52.

This version is using version 4.1.0 of VLCJ.

compile group: 'uk.co.caprica', name: 'vlcj', version: '4.1.0'

https://gist.github.com/jayfella/5757cb64dedb9c41cfb4d8be4bb778cd

3 Likes

Thank you

2 Likes

HI,I have a Ultra Video(2560*1440)
I try to load video to the TestVlcPlayer4.java code.
I only modify width(2560) and height(1440).
But the video cannot play in this code.

Do you have VLC player 64bit installed? Did you set the path to the video? Does the video play in VLC player? Have you tried a different video/format? What does it say in the logs?