使用JAVA播放媒体文件 [英] Playing media files using JAVA

查看:65
本文介绍了使用JAVA播放媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个胎面安全的JAVA应用程序,该应用程序:

I want to build a tread safe JAVA application which:

  1. 播放* .mp4或其他格式的高清媒体文件(全屏模式1920x1080)
  2. 将事件绑定添加到applet(我将使用触摸屏监视器)

我尝试了很多搜索,但是只发现了JMF的过时示例(VLCJ等).所以我想请您问从哪里开始构建这个applet.我可以包括哪些库.

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

我在这里找到了一个类似的项目: Media Shuffle 但是我希望我的媒体文件位于一个文件夹中,并且在应用程序中以图标的形式显示,这些图标可以启动选定视频(VLC全屏或其他跨平台媒体播放器)的第一次触摸播放.第二次触摸必须停止播放器并转到主页.

I found a similar project here: Media Shuffle But I want my media files to be located in one folder and they appear in applications as icons which start selected video (VLC fullscreen or other cross-platform media player) to play on 1st touch. The second touch have to stop player and go to the main page.

请分享您的想法,我将如何做到这一点.任何代码示例都很棒.

Please, share your ideas how I can do that. Any code examples would be great.

推荐答案

我会推荐vlcj,因为我确信它具有您可能需要或拥有的所有格式.以图形方式显示视频,我认为您找不到像您提到的那样简单的说明,但是如果您组织所有我将提及并更深入解释的步骤,您将看到vlcj是处理此类媒体的最佳选择(因为它我也喜欢并推荐JavaFx,因为这样做可以产生效果,但是对我来说很难在其中进行设置和编码.

I would recommend vlcj because i am sure it has all the formats you could need or have.Its not outdated at all and easy to start with.If you want to display a video graphically i dont think you could ever find so simple instructions like you mention but if you organize all steps that i will mention and explain in more depth you will see that vlcj is the best option for handling media like that (as it goes in my opinion).I also like and recommend JavaFx because of the effects you can make with that but its too difficult for me to setup and code in that stuff.

所以,让我们开始吧.首先,我想说我已经在基于Swing的应用程序(在Windows中)中实现了vlcj,但这不应该让您感到难过,因为对于您提到的即时弹出播放器,我们可以制作一个jdialog并将视频表面放在其contentPane中.

So, lets begin.Firstly, i would like to say that i have implemented the vlcj in a Swing-based application (in Windows) but that should not make you sad because for an immediate popup player that you mention we could just make a jdialog and place the video-surface in its contentPane.

步骤

1)所以首先要做的是下载vlc媒体播放器(我们需要在32位或64位计算机环境中均可播放的32位版本).成功加载所需的库,发现当您运行jar可执行文件时,它在32位jvm上运行(eclipse在64位jvm上运行,一切正常,这就是为什么我们需要32位版本),而我有64位要加载的本机库.无论如何,如果您下载并安装32位vlc媒体播放器,请在MyProject中的某个位置创建一个文件夹以包含您的项目(让我们说"C:/MyProject"),然后创建另一个文件夹,并将其命名为"Needed"(在这里,将会放置vlcj的所有必需的库,以使其正常工作.)现在从C:\ Program Files(x86)\ VideoLAN \ VLC的内容复制plugins目录和4个dll(axvlc.dll,libvlc.dll,libvlccore.dll,npvlc.dll)并将其粘贴到您所需的文件夹中

1)So the first thing that should be done is to download vlc media player (we will need the 32 bit version that plays in both 32bit or 64bit computer environments).I had a terrible month trying to configure why my app wasnt loading the required libraries succesfully and found out that when you run a jar exetutable file it runs on 32-bit jvm(in eclipse was running in 64bit jvm and everything was ok.Thats why we need 32bit version), while i had 64-bit native libraries to load from.Anyways if you download and install 32bit vlc media player make somewhere a folder to include your project(lets say "C:/MyProject") inside MyProject create another folder and call it for instance "Needed"(here we will place all the required libraries for vlcj in order to work properly).Now from the contents of C:\Program Files (x86)\VideoLAN\VLC copy the plugins directory and the 4 dlls (axvlc.dll,libvlc.dll,libvlccore.dll,npvlc.dll) and paste them inside your Needed folder

2)现在,如果您在Eclipse IDE或类似环境中工作,则需要在项目中创建一个文件夹(让我们说"lib"),并在其中创建另一个文件夹(将其命名为"jars").在jars文件夹中放置以下罐子:jna-3.5.1.jar,平台-3.5.1.jar,vlcj-2.2.0.jar.您可以从vlcj谷歌项目中找到这些罐子,然后将它们包括到您的类路径中(选择它们并右键单击->添加到构建路径或转到项目属性-> Java构建路径并添加这3个jar).在开始使用播放器设置进行任何编码之前,所有这些都已设置好.

2)Now if you work in Eclipse IDE or similar you will need to make a folder in your project (lets say "lib") and inside that create another folder( name it "jars" ).In jars folder place the following jars: jna-3.5.1.jar, platform-3.5.1.jar,vlcj-2.2.0.jar.You can find these jars from vlcj google project.And then just include them to your classpath(either select them and right-click->add to build path or go to project properties->Java build path and add those 3 jars).Thats all for setup before we begin any coding with player setup.

3)您必须先加载vlcj才能开始使用它.我只是使用这段代码来实现这一点(我很快会解释的,不用担心).

3) You have to load now vlcj before starting using it.I just use this code to make that possible(i will explain it shortly dont 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();

}

所以我要做的是为基于Swing的应用程序创建线程,因为如果无法显示媒体画布并且代码完成后构造器中的所有内容都已完全构建,则您将无法播放媒体,这意味着在制作播放器之前我们应该首先为构造函数创建一个延迟(我的时间是2秒)以结束他的工作,并使我们的JFrame(或jWindow或jDialog等)变得可显示.从Eclipse内部的工作区开始),然后进入Needed文件夹以实现所需的本机库.在if语句中,我告诉系统在我计算出的特定路径中查找libvlc.dll,然后加载它,从而向前播放了一步媒体文件.在if-else语句之外,我实际上创建了播放器并将画布放置为其VideoSurface(canvas是Canvas java.awt对象,我使用WindowsCanvas,因为我仅在Windows中工作,对于Linux或Mac,您可以找到类似的画布(不用担心!))在Swing-Worker字段(线程)之外,我只是告诉要执行的线程(作为调用函数指令很重要).

So what i do is to make a thread for Swing-based apps because you cannot play media if your media canvas is not displayable and everything in the constructor is fully built when its code is done.That means that before making the player we should first create a delay(mine is 2 seconds) for our constructor to end his job and our JFrame(or jWindow or jDialog etc) to become displayable.Next i calculate my path dynamically taking the path of my runnable jar(attention: not from workspace inside Eclipse) and entering the Needed folder to implement the required native libraries.Inside the if statement i tell the system to look for the libvlc.dll in the specific path i calculated and then load it and thus make one step forward to play media files.Outside if-else statement i actually create my player and place the canvas for its VideoSurface(canvas is a Canvas java.awt Object I use WindowsCanvas because i work only in windows, you could find for linux or mac a similar canvas(dont worry about that!)) Outside the Swing-Worker field(the thread) i just telling the thread to be executed (important as a call-function instruction).

4)要播放文件,我只需在应用程序中的某个位置使用按钮即可在每次按下该事件时调用一个动作事件,以便我们在其中进行某些操作,例如制作一个JFileChooser从中选择媒体文件.可以轻松地搜索它,但这是我的代码:

4)To play a file i just use a button somewhere in my app to call an action event each time it is pressed so that we do something inside it.I for example make a JFileChooser to choose a media file from.You can easily search for it 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();     

这些文件过滤器是vlcj可以播放给您的某些媒体文件类型.我要做的是打开一个文件对话框以从中选择文件,如果我选择一个文件,我会保留其路径(用完整的方式表示vlcj在哪里寻找它.)

Those file filters are some of the media file types that vlcj can play for u.What i do is opening a file dialog to choose a file from and if i choose a file i hold its path(usefull for saying vlcj where to look for it).

5)现在要播放文件,您只需键入以下代码:

5)Now to play the file u just have to type the following code :

player.play();

也许在另一个Button的另一个动作事件之内.

Maybe inside another action event of another Button.

6)如果完成这些操作,则只需将项目导出到可运行的jar文件中,然后将其导出到首先创建的MyProject文件夹中并运行它即可(注意,请双击它(而不是从控制台(否则它将是用64位jvm运行,并且您不希望这样,因为您有32位本机,而vlcj不接受这些冲突)))

6) if you finish writing those all you have to do is to export your project to a runnable jar file into your MyProject folder first created and run it (attention by double-clicking it (not from console(else it will be runned with 64bit jvm and you dont want that cause you have 32 bit natives and vlcj dont accept those conflicts)))

最后,我必须说这些步骤对我有用.我希望它们将帮助您进一步发展应用程序.

In Conclusion,i have to say that these steps worked for me.I hope they will help you go further in your app developement.

关于,PeGiannOS

Regards, PeGiannOS

这篇关于使用JAVA播放媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆