vlc入门

利用VLC自制一个java视频播放器

  • 1.下载相关文件
    • 1.1 下载vlc播放器
    • 1.1.1
      必须匹配Java 虚拟机的CPU体系结构和本机LibVLC库 - 如果使用32位JVM,则必须使用32位版本的VLC;如果使用64位JVM,则必须使用64位版本的VLC。你不能混合CPU架构,它不会工作。对于所有平台,至少需要Java 1.6版本。对于Linux和Windows平台,也完全支持Java 1.7和1.8版本。
    • 1.1.2
      查看JVM版本的方法:在cmd中输入 java -version, 没写是64位的就是32位的.
       
      JVM_version.png
    • 1.1.3
      www.videolan.org 中下载相应的版本。exe版本或者zip版本都可以,但exe版本一定要记住安装目录,我用的是vlc-2.2.4-win32.zip.
  • 1.2 下载控制vlc播放器的jar包(vlcj)
  • 2.1打开Eclipse创建项目MediaPlayerForJava,在根目录下创建连个文件夹 lib,vlc. lib文件夹用来放vlcj下的jar包,vlc文件夹用来放vlc播放器的组件。
     
    0.png
  • 2.2将下载下来的vlcj解压,选中这四个文件,复制粘贴到 项目中的lib文件夹下,并构建路径(选中 lib下的四个jar包,右击 - BuildPath-AddToBuildPath)
     
    1.PNG
  • 2.3 如果下载的vlc播放器是zip版的,解压后复制根目录下的
    文件libvlc.dll,文件libvlccore.dll,文件夹piugins,到项目中的vlc文件夹。如果是exe版的,找到安装目录,同样找到这两个文件和一个文件夹复制粘贴到vlc中。
     
    1.png
  • 2.4在src中创建一个包com.feihuang.main,创建一个类Main,创建一个包com.feihuang.view,创建一个类View.写以下代码,运行一下。

Main.java

package com.feihuang.main;

import javax.swing.SwingUtilities;

import com.feihuang.view.View;

public class Main { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { View frame = new View(); frame.setTitle("MediaPlayer"); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } } 

View.java

package com.feihuang.view;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder; public class View extends JFrame { private JPanel contentPane; public View() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); } } 
 
e.png
  • 2.5 要创建播放器先要找到播放器的组件,即文件libvlc.dll,文件libvlccore.dll,文件夹piugins;一共有两种方法实现找到播放器组件。
  • 2.5.1 本地发现的方法:boolean found = new NativeDiscovery().discover();如果查找成功返回true,否则返回false。此方法想要成功,必须安装vlc,即使用的是exe版vlc。
  • 2.5.2 设置库路径:
 NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),
            "vlc"); 

即之前我们将播放器组件放在了vlc文件夹下,让程序到vlc文件夹下去找

  • 2.6在View.java的构造方法中创建播放器组件的对象并添加到容器中去。并创建公共方法,获得播放器。
         //创建一个容器放播放器组件对象     
       JPanel player = new JPanel();
       contentPane.add(player, BorderLayout.CENTER);
       player.setLayout(new BorderLayout(0, 0)); //创建播放器组件并添加到容器中去 mediaPlayerComponent = new EmbeddedMediaPlayerComponent(); player.add(mediaPlayerComponent); 
public EmbeddedMediaPlayer getMediaPlayer(){
        return mediaPlayerComponent.getMediaPlayer(); } 
  • 2.7在Main.java中的程序入口main方法添加文件路径frame.getMediaPlayer().playMedia("c:\\\mysourse\\\1.mp4");
  • 2.8修改后的两个类为下:
Main.java
package com.feihuang.main;

import javax.swing.SwingUtilities;

import uk.co.caprica.vlcj.runtime.RuntimeUtil;

import com.feihuang.view.View;
import com.sun.jna.NativeLibrary; public class Main { private static View frame; public static void main(String[] args) { //new NativeDiscovery().discover(); NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "vlc"); SwingUtilities.invokeLater(new Runnable() { public void run() { try { frame = new View(); frame.setTitle("MediaPlayer"); frame.setVisible(true); frame.getMediaPlayer().playMedia("c:\\mysourse\\1.mp4"); } catch (Exception e) { e.printStackTrace(); } } }); } } 
View.java
package com.feihuang.view;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder; import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent; import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer; public class View extends JFrame { private JPanel contentPane; private EmbeddedMediaPlayerComponent mediaPlayerComponent; public View() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BorderLayout(0, 0)); JPanel player = new JPanel(); contentPane.add(player, BorderLayout.CENTER); player.setLayout(new BorderLayout(0, 0)); //创建播放器组件并添加到容器中去 mediaPlayerComponent = new EmbeddedMediaPlayerComponent(); player.add(mediaPlayerComponent); } public EmbeddedMediaPlayer getMediaPlayer(){ return mediaPlayerComponent.getMediaPlayer(); } } 
  • 3.点击运行,终于可以放出视频了~~
 
P.png
  • 3.1如果播放的视频名是中文,程序就挂掉了。可以右击项目-属性,修改文本编码为utf-8.
 
Paste_Image.png
 
Paste_Image.png

问题就解决了~



作者:FeiHuang
链接:https://www.jianshu.com/p/2c61e3c6aa70
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自www.cnblogs.com/code-fly-blogs/p/9902308.html