B4J Library B4JVlcj (embed VLC MediaPlayer in your program/app)

moster67

Expert
Licensed User
I am pleased to publish a B4J-wrapper/library of Vlcj (version 3.10.1) which in turn is a Java-wrapper of VLC (VideoLAN). With this wrapper, we can use VLC as a MediaPlayer in B4J.

I guess most of you are already familiar with VLC. Its strength is that it plays (local and in streaming) nearly all available formats/codecs. You might be less familiar with Vlcj - if so, you can find more information about Vlcj here.

In this library, I have wrapped the most important methods, properties and events which are required to create a functional MediaPlayer in B4J. I have tested it on Windows 10 and on my MAC and it works fine on both.

I am attaching the MediaPlayer as a demo-project. I also attach the required B4J-libraries. You also need to install some extra Jars that are necessary to make this work. I also attach the Java-code of the B4J-wrapper.

Vlcj (and VLC) is huge and therefore I have left out some functionality such as subtitles, audio-tracks, embedding logos, video-adjustments etc. You can add missing functionality by using JavaObject (see the GetFps-method in the demo-project), InLineJava or by modifying the attached Java-sources. Please refer to the API JavaDocs. Should you prefer me to do it for you, please then contact me in PM here in the forum and we can discuss it.

Requirements:
-VLC (VideoLAN) must be installed on the computer.
-You must match the CPU architecture of the Java Virtual Machine and the native LibVLC libraries. If you use a 32-bit JVM you must use a 32-bit release of VLC; if you use a 64-bit JVM you must use a 64-bit release of VLC. You can not mix CPU architectures, it will not work.
-Add the extra jars in your B4J Extra/Additional Library folder. They can be downloaded from here.

How to run the demo-project:
-Add the extra jars (jna-4.1.0, jna-platform-4.1.0, slf4j-api-1.7.10,vlcj-3.10.1) in your B4J Extra/Additional Library folder.
-Add the B4JVlcj and B4JDragToMe libraries in your B4J Extra/Additional Library folder.
-Make sure to refresh the Library-pane in the B4J-IDE and tick the libraries.
-Open the demo-project in B4J and read the code-notes.
-Methods and Properties are explained in the IDE-help (see also next post).

License:
-My B4J-wrapper is free to use and Java source code is attached. I am only enabling users to use Vlcj with my B4J-wrapper (B4JVlcj).
-Vlcj is provided under the terms of the gpl3 license. Check their web-site for further information.

Please remember that creating libraries and maintaining them takes time and so does supporting them. Please consider a donation if you use my free libraries as this will surely help keeping me motivated. Thank you!

A compiled jar of the demo-project can be downloaded from this thread.

Here is video of the demo-app in execution:

Enjoy!
 

Attachments

Last edited:

moster67

Expert
Licensed User
B4JVlcj Methods/Properties:

B4JVlcj
Author:
Moster67 aka Mikael Osterhed
Version: 1.1
  • B4JVlcj
    Events:
    • Error
    • Finished
    • Prepared
    Fields:
    • player As VideoPlayer
    Methods:
    • CanPause As Boolean
      Can the current media be paused?
      returns true if the current media can be paused, otherwise false
    • GetAspectRatio As String
      Get the video aspect ratio.
      Returns aspect ratio ONLY if you have set the AspectRatio yourself.
    • GetLength As Long
      Get the length of the current media item.
      Returns: length, in milliseconds
    • GetPlaySpeedRate As Float
      Get the current video play speed rate.
      speedRate, where 1.0 is normal speed, 0.5 is half speed, 2.0 is double speed and so on
    • GetPosition As Float
      Get the current play-back position.
      Returns current position, expressed as a percentage (e.g. 0.15 is returned for 15% complete)
    • GetScale As Float
      Get the current video scale (zoom factor).
    • GetTime As Long
      Get the current play-back time.
      Returns current time, expressed as a number of milliseconds
    • GetVolume As Int
      Get the current volume.
      Returns volume, a percentage of full volume in the range 0 to 200
    • Initialize (EventName As String)
      Initializes B4JVlcj. You must set an event-name if you want to monitor events.
      Important: Always check if VLC is installed before initializing using
      the IsVLCInstalled-method.
      Important: When exiting your app, always make sure to use the release-method!
    • IsInitialized As Boolean
    • IsPlayable As Boolean
      Is the current media playable?
      returns true if the current media is playable, otherwise false
    • IsPlaying As Boolean
      Is the media player playing?
      returns true if the media player is playing, otherwise false
    • IsSeekable As Boolean
      Is the current media seekable?
      returns true if the current media is seekable, otherwise false
    • IsVLCInstalled As Boolean
      This method should be the first code you run before initializing B4JVlcj.
      Returns if VLC has been installed on the computer or not. If VLC has been
      installed in a non-standard directory, VLC might not be found.
    • Mute
      Mutes volume
    • Pause
      Pauses media
    • Play (source As String)
      Plays media (can be a local file or a streaming source)
    • SetAspectRatio (AspectRatio As String)
      Set the video aspect ratio
      Param: aspectRatio - aspect ratio, e.g. "16:9", "4:3", "185:100" for 1:85.1 and so on
    • SetPlaySpeedRate (speedRate As Float) As Int
      Set the video play speed rate.
      Some media protocols are not able to change the speed rate.
      speed rate - rate, where 1.0 is normal speed, 0.5 is half speed, 2.0 is double speed and so on
      Returns: -1 on error, 0 on success
    • SetPosition (position As Float)
      Jump to a specific position. If the requested position
      is less than zero, it is normalised to zero.
      Param: position - position value, a percentage (e.g. 0.15 is 15%)
    • SetScale (factor As Float)
      Set the video scaling factor
      Param: factor - scaling factor, or zero to scale the video the size of the container
    • SetTime (time As Long)
      Jump to a specific moment.
      If the requested time is less than zero, it is normalised to zero
      Param: time - time since the beginning, in milliseconds
    • SetVolume (volumeLevel As Int)
      Set the volume.
      The volume is actually a percentage of full volume, setting a volume
      over 100 may cause audible distortion.
      Param: volumeLevel - a percentage of full volume in the range 0 to 200
    • Skip (delta As Long)
      Skip forward or backward by a period of time.
      To skip backwards specify a negative delta.
      Param: delta - time period, in milliseconds
    • SkipPosition (delta As Float)
      Skip forward or backward by a change in position.
      To skip backwards specify a negative delta.
      Param: delta - amount to skip
    • Stop
      Stops playing media
    • Unmute (VolumeLevel As Int)
      Unmutes volume
    • release
      Use this on the b4j closing event. Final step to do is to release vlc.
    Properties:
    • Version As Double [read only]
    • VideoHeight As Double [read only]
      Returns Video Height
    • VideoWidth As Double [read only]
      Returns Video Width
 

moster67

Expert
Licensed User
Here is a video with B4JVlcj and two instances of VLC:
-one is playing a video
-the other one is capturing my WebCam

If you want, you can probably use B4JVlcj to stream your WebCam to a server (although not tested but considering the power of VLC this should be possible).
VLC is just as flexibile as FFmpeg.

 

techknight

Well-Known Member
Licensed User
There is a problem here.

If you take and create a new project with the existing scenebuilder.fxml it crashes. its looking for com.tillekesoft.B4JVlcj which doesnt exist in any project outside of yours so you might want to take a look into this.

Thanks.
 

moster67

Expert
Licensed User
Did you read my notes in the sample-code?

'If you rename this project, you must remember to change the scenegrid.fxml layout
'file accordingly. Open the layout file with Notepad++ and change the following
'line: fx:controller="com.tillekesoft.B4JVlcj.main" with your project name.
'If you create your own fxml layout file, then it should not be needed.
'Read the notes in the code and the IDE-description for further information.
 

rraswisak

Active Member
Licensed User
Hi @moster67 ,

Thank you for sharing this libs,

is there any reason of using fxml type in the layout file rather than using general bjl or pane ?

i have try to modify the sample code which vlc was added to pane, but the player dimension too small, i notice that there is no method or property to resize vlc to pane width and height, can you pleas help me ?

upload_2019-2-15_11-24-45.png


After media has been played, i add to vlc.play(MediaFile) in Sub vlc_Finished (for looping) but the media won't play automatically, don't know why

Thank you
 

moster67

Expert
Licensed User
is there any reason of using fxml type in the layout file rather than using general bjl or pane ?
i have try to modify the sample code which vlc was added to pane, but the player dimension too small, i notice that there is no method or property to resize vlc to pane width and height, can you pleas help me ?
Don't understand why you want to change the GridPane? I am using a GridPane on purpose, which is not available natively in B4J, because it is ideal for a situation like this where resizing takes place and since there is very little code to add. If you want to use a normal pane, maybe you can try Erel's GridManager and see if you can adapt the code so it works with VLC they way you wish.

After media has been played, i add to vlc.play(MediaFile) in Sub vlc_Finished (for looping) but the media won't play automatically, don't know why
If you look at the API-documentation linked in the first post, you can see that there is a "setRepeat-method" and which can be exposed by using JavaObject. I already tried it but it would not work for me. You would probably need to use Vlcj's MediaListPlayer for this. To do this, I guess you would either need to use inline Java code or modify the Java-sources which are attached in the first post.

I am aware that not everything is included in this wrapper (it is a big library) - this is the reason why I published the Java-sources too so anyone who wishes can add missing stuff. For certain things, you can also use JavaObject to access methods and fields but also inline-Java. If you cannot obtain what you want, contact me by PM.
 

androh

Member
Licensed User
I want to change "network-caching" value.
From the library source, I've changed some code and compile it with Simple Library Compiler.
But I receive a error while loading modified library on B4J.
How can I re-compile with Simple Library Compiler?

0.PNG

1.PNG

error.PNG
 

Hypnos

Active Member
Licensed User
I try to run it on raspberry pi 3 but I got the following error. can anyone help please!
(Checked VLC already installed.)

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.
VLC was not found on your computer. Please install it first and then retry.
VLC must be installed on the computer to run this program.
 

moster67

Expert
Licensed User
could you post the example of running 2 instances of vlc ?
Sorry, but I don't have that test-code any longer since it was just a quick test I did at the time.
I guess I used the sample app I provided and just added another instance of VLC.
 
Top