How to Create A Video Player in 5 Steps



For most java beginners, one of the coolest projects you might want to begin with is your own video player .How cool is that? Well  the thing is ,unless you are willing to switch javafx  which has native classes for playing videos (Mp4 only) ,Java Media Framework (JMF)is pretty outdated .JMF is an API created by java to play media files and sorry to say, it has been updated in a long time and as such it is crap but fear not VLC to the rescue .VLC has this amazing API ,called VLCJ ,that allows you to play virtually any media file VLC can and it’s amazing. There aren’t a lot of requirements for using this API ;Make sure you have VLC installed and download  VLCJ here .

  • Now let’s get to the important stuff, I am assuming you have downloaded VLCJ and you have added the API to you project if not check out a post on how to add an API to you project. First you have to instruct the JVM to find the Native API   in your existing vlc installation folder, LibVLC. There three ways of doing this and believe me.I have had difficulty making any of them work except one but I shall post all.

  1. ·            NativeLibrary.addSearchPath (RuntimeUtil.getLibVlcLibraryName (),"File path"); //Replace file path with directory of vlc · Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class); 
  2.   new NativeDiscovery().discover();///Finds the Api easily

·          
  • Personally I like this method better its totally hustle free but should you encounter any problems it might be because you have a 32-bit JVM installed and a 64-bit vlc installed that wont work.

  • ·         Create a JFrame and set its size.


JFrame frame =new JFrame ();//Initialize Window
frame.setSize(600,500);//Set Size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//End Program when window is closed

  • ·         Next We create a Canvas which will display the video output and We also initialize the media player which will play the video


EmbeddedMediaPlayerComponent Mcomp=new EmbeddedMediaPlayerComponent();//create mediaplayer component
MediaPlayer mplay=Mcomp.getMediaPlayer();//create mediaplayer
Canvas videoSurface=Mcomp.getVideoSurface();///create the video surface

  • ·         Lastly we add all our components to the frame and play a video. Note: the components must be all added before we play the video.


frame.add(videoSurface);//Add video Surface to window
frame.setVisible(true);///Make Window visible);
mplay.playMedia(file path”,null);////Replace File path with path to video

·         The file path is simple to obtain, open the location of the video you want to play  and follow this

·         Paste what you copied here and change all the  back slashes to forward slashes
mplay.playMedia(“C:/UsersHP/Downloads/”,null);////Replace File path with path to video
·          
·         Then right click and open the properties of the video you want to play and  do this

·         Next add the file name ,you copied, like this
mplay.playMedia(“C:/UsersHP/Downloads/ The.Grand.Tour.S01E04.480p.WEBrip.Tehmovies.biz.mp4”,null);////Replace File path with path to video

You should see this if you did it right



·         That’s it!!

No comments:

Interesting, Cool, Bad or any: Let's know below

Powered by Blogger.