第一步:初步的配置过程
1.到vlcj官网下载最新版的vlcj
官网地址:http://capricasoftware.co.uk/#/projects/vlcj/tutorial,
最好在这个网址里看一下最新版的说明,看看vlcj支持的VLC版本;
2.到VLC官网下载对应版本的VLC:http://www.videolan.org/
3.到https://www.slf4j.org/下载最新版本的slf4j
**注:采用jna-4的版本的时候,可能会出现找不到vlc的报错的兼容性问题,可采用换为低版本的jna解决;**
4.在Eclipse下新建Java项目,将下载的vclj下的三个个jar包:jna-3.5.2.jar,platform-3.5.2.jar,vlcj-3.0.1.jar(我下载的版本是vlcj3.0.1的)和slf4j下的 slf4j-api-1.7.25.jar 、slf4j-nop-1.7.25.jar放到工程目录的l新建的lib文件夹中,并添加到你项目的BuildPath中;
(我采用最新版本的时候出现了找不到VLC报错问题,故换成了低版本的了)
5、两种主要且最可靠的方法来帮助vlcj发现LibVLC本机库的位置,若打印出了版本信息,则成功获取了
第一种方式 - 自动本机发现
package tutorial;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.discovery.NativeDiscovery;
public class Tutorial {
public static void main(String[] args) {
boolean found = new NativeDiscovery().discover();
System.out.println(found);
System.out.println(LibVlc.INSTANCE.libvlc_get_version());
}
}
第二种方式- 显式库路径
package tutorial;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.sun.jna.NativeLibrary;
public class Tutorial {
private static final String NATIVE_LIBRARY_SEARCH_PATH = "/home/vlc";
public static void main(String[] args) {
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), NATIVE_LIBRARY_SEARCH_PATH);
System.out.println(LibVlc.INSTANCE.libvlc_get_version());
}
}
二、程序的编写
说明:创建了两个类一个为主程序,一个为窗体类
主程序:
package com.jike.myvideoplayer.main;
import java.awt.EventQueue;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.SwingWorker;
import javax.swing.filechooser.FileNameExtensionFilter;
import com.jike.myvideoplayer.view.PlaymainJframe;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class Playmain {
static PlaymainJframe frame;
private static final String NATIVE_LIBRARY_SEARCH_PATH = "D:\\java\\VLC";
public static void main(String[] args) {
//判断系统,设置vlc的使用路径
// if(RuntimeUtil.isWindows()) {
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), NATIVE_LIBRARY_SEARCH_PATH);
//System.out.println(LibVlc.INSTANCE.libvlc_get_version());
// }else if(RuntimeUtil.isMac()) {//如果是苹果系统
// NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "D:\\java\\VLC\\sdk\\lib");
// //}else if(RuntimeUtil.isNix()) {
// NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "D:\\java\\VLC\\sdk\\lib");
// }
//加载VLC播放器的系统库
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new PlaymainJframe();
frame.setVisible(true);
//设置字幕编码
//String option[]= {"--subsdec-encoding=GB18030"};
//自动播放
//frame.getPlay().playMedia("Airplane!.1980..双语字幕.HR-HDTV.AC3.1024X576.x264-人人影视制作.mkv");
//不自动播放,只是放置了视频文件
frame.getPlay().prepareMedia("Airplane!.1980..双语字幕.HR-HDTV.AC3.1024X576.x264-人人影视制作.mkv");
//全屏播放
//frame.getPlay().toggleFullScreen();
//利用进度条来显示当前进度
new SwingWorker() {
@Override
protected String doInBackground() throws Exception {
while(true) {//持续获取。不断更新
//获取视频的全部长度
long totall=frame.getPlay().getLength();
//获取视频当前的长度
long curr=frame.getPlay().getTime();
//获取长度的百分比
float prent=(float)curr/totall;
publish((int)(prent*100));//获得的值才能使0-100
Thread.sleep(100);//每隔一毫秒获取一次
}
//return null;
}
protected void process(java.util.List chunks) {
for(int v:chunks) {
frame.getprogress().setValue(v);//设置进度条当前显示的进度
}
};
}.execute();//启动进程
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void play() {//播放
frame.getPlay().play();
}
public static void pause() {//暂停
frame.getPlay().pause();
}
public static void stop() {//停止
frame.getPlay().stop();
}
public static void timeto(float to) {//跳转到某一个时间点
frame.getPlay().setTime((long) (to*frame.getPlay().getLength()));
//System.out.println(frame.getPlay().getLength());
}
public static void setvol(int v) {//设置播放器的声音
frame.getPlay().setVolume(v);
}
public static void openfile() {//打开视频文件
JFileChooser chooser=new JFileChooser();
//只能选择的视频文件类型
FileNameExtensionFilter filter=new FileNameExtensionFilter("Allowed file", "mvk","ogg","mp4");
chooser.setFileFilter(filter);
int v=chooser.showOpenDialog(null);
if(v==JFileChooser.APPROVE_OPTION) {
File file=chooser.getSelectedFile();
frame.getPlay().playMedia(file.getAbsolutePath());
}
}
public static void openstutitle() {//打开视频字幕文件
JFileChooser chooser=new JFileChooser();
int v=chooser.showOpenDialog(null);
if(v==JFileChooser.APPROVE_OPTION) {
File file=chooser.getSelectedFile();
frame.getPlay().setSubTitleFile(file);
}
}
public static void exit() {//退出
frame.getPlay().release();//释放视频资源
System.exit(0);//退出系统
}
}
窗体类
package com.jike.myvideoplayer.view;
import java.awt.BorderLayout;
//import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.jike.myvideoplayer.main.Playmain;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import javax.swing.JButton;
import javax.swing.JProgressBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
public class PlaymainJframe extends JFrame {
private JPanel contentPane;
//设置播放器界面的对象
EmbeddedMediaPlayerComponent playerComponent;
private JPanel panel;
private JButton button;
private JButton button_1;
private JButton button_2;
private JPanel panel_1;
private JProgressBar progressBar;
private JMenuBar menuBar;
private JMenu mnFile;
private JMenuItem mntmOpenFile;
private JMenuItem mntmOpenSt;
private JMenuItem mntmExit;
private JSlider slider;
/**
* Launch the application.
*/
public static void main(String[] args) {
}
/**
* Create the frame.
*/
public PlaymainJframe() {
setTitle("This my video");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 682, 422);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
mnFile = new JMenu("file");
menuBar.add(mnFile);
mntmOpenFile = new JMenuItem("open file");
mnFile.add(mntmOpenFile);
mntmOpenSt = new JMenuItem("open stutitle");
mnFile.add(mntmOpenSt);
mntmExit = new JMenuItem("EXIT");
mnFile.add(mntmExit);
//打开文件
mntmOpenFile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Playmain.openfile();
}
});
//打开字幕文件
mntmOpenSt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Playmain.openstutitle();
}
});
//退出操作
mntmExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Playmain.exit();
}
});
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel videopanel = new JPanel();
contentPane.add(videopanel);
videopanel.setLayout(new BorderLayout(0, 0));
playerComponent=new EmbeddedMediaPlayerComponent();
videopanel.add(playerComponent);
panel = new JPanel();
videopanel.add(panel, BorderLayout.SOUTH);
panel.setLayout(new BorderLayout(0, 0));
panel_1 = new JPanel();
panel.add(panel_1);
button = new JButton("\u6682\u505C");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//调用暂停方法
Playmain.pause();
}
});
panel_1.add(button);
button_1 = new JButton("\u5F00\u59CB");
button_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//调用播放方法
Playmain.play();
}
});
panel_1.add(button_1);
button_2 = new JButton("\u505C\u6B62");
button_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//调用停止方法
Playmain.stop();
}
});
panel_1.add(button_2);
slider = new JSlider();
slider.setValue(100);
slider.setMaximum(120);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
Playmain.setvol(slider.getValue());//获取滑块的值,来赋值给播放器的音量
}
});
panel_1.add(slider);
progressBar = new JProgressBar();
progressBar.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//通过点击进度条的当前位置来控制视频的播放进度
//获取鼠标的位置
int x=e.getX();
Playmain.timeto((float)x/progressBar.getWidth());
//System.out.println((float)x/progressBar.getWidth());
}
});
progressBar.setStringPainted(true);
panel.add(progressBar, BorderLayout.NORTH);
//videopanel.add(playerComponent);
}
public EmbeddedMediaPlayer getPlay(){
return playerComponent.getMediaPlayer();
}
public JProgressBar getprogress() {
return progressBar;
}
}
四、项目遇到的问题以及解决方式
下载的vlcj的最新版本可能不能兼容VLC,所以可以选择下载低版本的
:其他的配置方式
以下是我看的网上的一些配置方式方式,但我试过的是没有用的,但或许对大家 有用,就收集在这里的,仅供参考
参考资料: