Sunday, November 15, 2015

Media Player API using vlcj

About vlcj
The vlcj API provides a Java framework to allow an instance of native vlc media player to be embedded in a Java AWT window or a Swing frame. Basically if you want to create a media player in AWT or Swing, you can use vlcj API to develop your own media player. However the requirement for using this API is to have VLC player installed in your system, so that it can use it's libraries.

MediaPlayer API
The mediaplayer API uses vlcj internally and also provides swing controls such as a pause button (play button), a stop button and a video slider. It's very easy to use for developers who don't want to re invent the wheel of again writing the code for  adding basic Swing components (media player options) to their media player. You just add a single line of code and you are done. For instance -

new MediaPlayer("/home/reets/Videos/TestVideo.mp4", "/usr/bin/vlc/").launchMediaPlayer();

This will launch a media player and will play your media file passed to it as an argument. I will explain what are the constructors and publicly available methods for this API.

Constructors-

1) new MediaPlayer( 'Location of your Media File', 'Location of your VLC Player Installation')

This constructor takes two parameters -
a) The location of your media file to be played as a String
b) The location of your VLC player installation as a String

For example -
new MediaPlayer("/home/reets/Videos/TestVideo.mp4", "/usr/bin/vlc/");

2) new MediaPlayer('File object of your Media File', 'Location of your VLC Player Installation')

This constructor takes two parameters-
a) The file object of your media file to be played
b) The location of your VLC player installation as a String

For example -
File mediaFile = new File("/home/reets/Videos/TestVideo.mp4");
new MediaPlayer(mediaFile, "/usr/bin/vlc/");

3) new MediaFile('Input Stream of the media file to be played','Location of your VLC Player Installation')

This constructor takes two parameters-
a) The input stream of the media file to be played as an InputStream
b) The location of your VLC player installation as a String

For example-

try {
            InputStream inputStream = new FileInputStream(new File("/home/reets/Videos/TestVideo.mp4"));
            new MediaPlayer(inputStream, "/usr/bin/vlc/");
        } catch (FileNotFoundException e) {
                       e.printStackTrace();
        }


How to launch the media player-

In order to launch the video player you will just have to call the method 'LaunchMediaPlayer()' and that's it, you are done!!!

new MediaPlayer(inputStream, "/usr/bin/vlc/").launchMediaPlayer();

This single line of code will launch your media player and play the video you want it to play.



Word of Caution-
You need to have VLC player installed in order to use this API since internally it uses VLC libraries. Generally the default installation paths for VLC player are -

For Linux - '/usr/bin/vlc'
For Windows - 'C:/Program Files (x86)/VideoLAN/VLC/'

Download-
https://www.dropbox.com/s/b95vyceixe9k30a/MediaPlayerApi-1.0.jar?dl=0

The source code will be released shortly. The API will be constantly updated with features. Your comments are most welcome so that I can update the API with the necessary features. If you face any problem while using this API please post your problem in the comment section. I will try to resolve it as soon as I can.

No comments:

Post a Comment