java media player_MediaPlayerForJava(创建一个视频播放器)
利用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)
1.2.1
官網在: http://capricasoftware.co.uk/#/projects/vlcj ,往下翻可以看到最新的 3.10.1版本,但我不推薦這個,在后來的實踐中,我發現使用這個,會只能以本地發現的方式 new NativeDiscovery().discover() (后面會講) 創建播放器(即必須安裝正確版本的exe文件),而不能用設置庫路徑的方式創建(至少我的電腦不行.)
2.創建項目
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
問題就解決了~
3.2雖然還有許多功能沒實現,但程序已經跑起來了。控制功能之后再寫~
總結
以上是生活随笔為你收集整理的java media player_MediaPlayerForJava(创建一个视频播放器)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 停止一个线程_Java如何停止
- 下一篇: mysql mail_vpopmail+