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
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM