Playing multimedia files using JAVA

I want to create a secure JAVA application that:

  • Plays * .mp4 or other HD format files (1920x1080 full screen)
  • Add event bindings to applet (I'll be using a touchmonitor)

I tried searching a lot but only found outdated JMF examples (VLCJ, etc.). So I want you to ask where to start creating this applet. What libraries can I include.

I found a similar project here: Media Shuffle But I want my media files to be in the same folder and they appear in applications as icons that start the selected video (VLC full screen or other cross platform media player) to play on the first touch ... The second touch should stop the player and go to the main page.

Please share your ideas on how I can do this. Any code examples would be great.

+3


source to share


3 answers


I would recommend vlcj because I am sure it has all the formats you might need or have. It is not deprecated at all and is easy to start with. If you want to display the video graphically, I don't think you will ever be able to find the instructions as easy as you mention, but if you organize all the steps that I go through and explain in more detail, you will see that vlcj is the best option. for processing such media (as it seems to me). I also like and recommend JavaFx because of the effects you can do with this, but too difficult for me to customize and code in this stuff.

So let's get started. First, I would like to say that I have applied vlcj in a Swing based application (on Windows), but that shouldn't upset you, because for the immediate popup player you are talking about, we could just do a jdialog and put video surface to its content area.

Steps

1) So, the first thing to do is download the vlc media player (we will need a 32-bit version that will play in both 32-bit and 64-bit computer environments). I had a dreadful month trying to tweak why my application was not quickly loading the required libraries and finding out that when the jar of the exetutable file is run, it runs on a 32-bit jvm (in eclipse it runs on a 64-bit jvm, and that's ok. So we need a 32 bit version), while I had a 64 bit load from native libraries. When you download and install vlc 32bit media player, create somewhere folder to include your project (say "C: / MyProject") inside MyProject create another folder and call it like "Needed"(here we will put all the necessary libraries for vlcj to work properly). Now from the contents of C: \ Program Files (x86) \ VideoLAN \ VLC copy the plugins directory and 4 dlls (axvlc.dll, libvlc.dll, libvlccore.dll, npvlc.dll) and paste them into your desired folder

2) Now if you are working in the Eclipse IDE or similar, you will need to create a folder in your project (say "lib") and inside to create another folder (call it "jars"). The jars folder contains the following jars: jna-3.5.1.jar, platform-3.5.1.jar, vlcj-2.2.0.jar. You can find these jars from the vlcj google project. Then just include them in your classpath (either select them or right click -> add build path or go to project properties -> Java build path and add these 3 jars). All of this is for customization before we start coding with the player setup.

3) You need to download vlcj before you can start using it. I'm just using this code to make it possible (I'll explain it shortly, don't worry).

public void LoadLibrary(){
    SwingWorker loadWorker;     


    loadWorker = new SwingWorker(){

        @Override
        protected Object doInBackground() throws Exception {
            // TODO Auto-generated method stub

            Thread.sleep(2000);

            path = new File("").getAbsolutePath().toString();
            path = path.replace(".", "");
            path = path.replace("\\", "//");
            path = path+"//Needed";

            if(RuntimeUtil.isWindows()){
                NativeLibrary.addSearchPath(
                        "libvlc",path



                            );
                            Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);




                    }
            else if(RuntimeUtil.isNix()){
                    NativeLibrary.addSearchPath(
                            "libvlc",path

                            );
            }



            mediaPlayerFactory = new MediaPlayerFactory();
            player = mediaPlayerFactory.newEmbeddedMediaPlayer();
            CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
            player.setVideoSurface(videoSurface);


            return null;
        }

    };
    loadWorker.execute();

}

      

So what I am doing is to create a stream for Swing based applications because you cannot play the media unless your media canvas is displayed and everything in the constructor is fully built when its code is done. This means that before making the player we must first create a delay (mine is 2 seconds) for our constructor to finish, and our JFrame (or jWindow or jDialog, etc.) to become rendered. Next I figure out my path by dynamically navigating my runnable jar path (note: not from workspace inside Eclipse) and enter the correct folder to implement the required native libraries. Inside the if statement, I will tell the system to search for libvlc.dll in a specific path that I figured out and then download and thus take one step forward to play the media files.Outside if-else statement I actually create my player and put a canvas for my VideoSurface (canvas is Canvas java.awt Object I am using WindowsCanvas because I only work in windows, you can find a similar canvas for linux or mac (for example not worry about it!)) Outside of the Swing-Worker (thread) field, I'm just saying the thread being executed (important as a call command).

4) To play the file, I just use a button somewhere in my application to fire an action event every time it is clicked so that we can do something inside it. I, for example, make a JFileChooser to select a media file. can find it easily, but here is my code:



final JFileChooser chooser = new JFileChooser();

            FileNameExtensionFilter filter0 = new FileNameExtensionFilter(".wav", "wav");
            FileNameExtensionFilter filter1 = new FileNameExtensionFilter(".mp3","mp3");
            FileNameExtensionFilter filter2 = new FileNameExtensionFilter(".mpg","mpg");
            FileNameExtensionFilter filter3 = new FileNameExtensionFilter(".mp4","mp4");
            FileNameExtensionFilter filter4 = new FileNameExtensionFilter(".avi","avi");
            FileNameExtensionFilter filter5 = new FileNameExtensionFilter(".flv","flv");
            FileNameExtensionFilter filter6 = new FileNameExtensionFilter(".wmv","wmv");
            FileNameExtensionFilter filter7 = new FileNameExtensionFilter(".3gp", "3gp");
            FileNameExtensionFilter filter8 = new FileNameExtensionFilter(".swf", "swf");
            FileNameExtensionFilter filter9 = new FileNameExtensionFilter(".mkv", "mkv");
            FileNameExtensionFilter filter10 = new FileNameExtensionFilter(".flac", "flac");
            FileNameExtensionFilter filter11 = new FileNameExtensionFilter("Music & Videos","wav","mp3","mpg","mp4","avi","flv","wmv","3gp","swf","mkv","flac","VOB");
            FileNameExtensionFilter filter12 = new FileNameExtensionFilter("Music","wav","mp3","flac");
            FileNameExtensionFilter filter13 = new FileNameExtensionFilter(".VOB", "VOB");
            FileNameExtensionFilter filter14 = new FileNameExtensionFilter("Videos","mpg","mp4","avi","flv","wmv","3gp","swf","mkv","VOB");


            chooser.setFileFilter(filter14);
            chooser.setFileFilter(filter2);
            chooser.setFileFilter(filter3);
            chooser.setFileFilter(filter4);
            chooser.setFileFilter(filter5);
            chooser.setFileFilter(filter6);
            chooser.setFileFilter(filter13);
            chooser.setFileFilter(filter7);
            chooser.setFileFilter(filter8);
            chooser.setFileFilter(filter9);
            chooser.setFileFilter(filter12);
            chooser.setFileFilter(filter0);
            chooser.setFileFilter(filter1);
            chooser.setFileFilter(filter10);
            chooser.setFileFilter(filter11);   



            int returnVal = chooser.showOpenDialog(getParent());
            if(returnVal == JFileChooser.APPROVE_OPTION) {
                System.out.println("You chose to open this file: " +
                chooser.getSelectedFile().getName());
                File myfile1 = chooser.getSelectedFile();
                myfilepath1 = chooser.getSelectedFile().getAbsolutePath();
}

        player.startMedia("file:///"+myfilepath1);
                player.pause();     

      

These file filters are some types of media files that vlcj can play for u. What I do is open a file dialog to select a file and if I select a file I keep its path (useful for expressing vlcj where look for it).

5) Now, to play the file, you just need to enter the following code:

player.play();

      

Perhaps inside another event the actions of another button.

6) if you're done writing, all you have to do is export the project to an executable jar file in the MyProject folder first created and launched (note by double clicking on it (not from the console (otherwise it will be launched from 64- bit jvm and you don't want you to have 32-bit natives and vlcj doesn't accept these conflicts)))

In conclusion, I must say that these steps worked for me. Hopefully they will help you move further in application development.

Best regards, PeGiannOS

+3


source


You must first

// create a player to play media files specified in the URL

Player mediaPlayer = Manager.createRealizedPlayer( mediaURL );

      



Now

mediaPlayer.start(); // start playing the media clip

      

I hope this works!

+2


source


VLCJ is not obsolete, actively evolving, and extremely flexible and powerful in what it can achieve. I use it in my application to display multiple video streams at the same time inside the application, as well as to perform operations such as text overlays at the same time. This is sometimes difficult to do, but certainly possible.

There are a number of basic (modern) examples to get you started with VLCJ here.

0


source







All Articles