1、环境配置
(1)下载VLC VlC官网http://www.videolan.org/ 各个版本的下载地址http://download.videolan.org/pub/videolan/vlc/last/ 里面有32位和64位的区分,按照自己的系统下载对应的vlc版本,下载解压后提取;
(2)下载VLCJ (http://capricasoftware.co.uk/#/projects/vlcj)把下载文件解压后将其目录下的jna-3.5.2.jar、platform-3.5.2.jar、vlcj-3.8.0.jar(不同版本后缀数字可能会不同)三个文件复制到对应的java工程目录(新建 lib文件夹)下;
(3)下载slf4j(http://www.slf4j.org/),下载文件后解压将其目录下的slf4j-api-1.7.13.jar、slf4j- nop-1.7.13.jar(不同版本后缀数字可能会不同)两个文件复制到对应的java工程目录(新建 lib文件夹)下;
(4)将vlc安装目录下的libvlc.dll、libvlccore.dll 两个文件以及plugins问佳佳复制到对应的java工程目录下;
将lib文件中的jar包导入到工程项目中。配置好的项目目录如下图所示
(5)具体代码如下
播放器主类代码
package deng.test;
import java.awt.EventQueue;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.SwingWorker;
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 PlayerMain {
static Window frame;
private static int times=1;
private static boolean flag=true;
// private static final String NATIVE_LIBRARY_SEARCH_PATH = "D:\\Program
// Files\\VideoLAN\\VLC\\sdk\\lib";
public static void main(String[] args) {
// 环境配置,将vlc sdk导入到eclipse
// if(RuntimeUtil.isWindows()){ }
// NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),
// "D:\\vlc\\vlc-2.2.6\\sdk\\lib"); //导入的路径是vlc的安装路径
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "vlcj//lib"); // 导入的路径是vlc的安装路径
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
// System.out.println(LibVlc.INSTANCE.libvlc_get_version());
// 创建主程序界面运行窗体
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
frame = new Window();
frame.setVisible(true);
frame.getMediaPlayer().playMedia("video//style.avi"); // 直接播放视屏,参数是视屏文件的绝对路径
// frame.getMediaPlayer().prepareMedia("video//test.mp4");
// //控制即将播放的视频
new SwingWorker<String, Integer>() {
@Override
protected String doInBackground() throws Exception {
// TODO Auto-generated method stub
while (flag) { // 获取视频播放进度并且按百分比显示
long total = frame.getMediaPlayer().getLength();
if(total==-1){//如果视频播放完,循环播放视频
play();
times++;
System.out.println(times+"******************");
}
long curr = frame.getMediaPlayer().getTime();
float percent = (float) curr / total;
publish((int) (percent * 100));
Thread.sleep(100);
}
return null;
}
protected void process(java.util.List<Integer> chunks) {
for (int v : chunks) {
frame.getProgressBar().setValue(v);
}
}
}.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//关闭播放器释放资源
public static void dispose(){
flag=false;
frame.getMediaPlayer().stop();
frame.dispose();
}
// 打开文件
public static void openVideo() {
JFileChooser chooser = new JFileChooser();
int v = chooser.showOpenDialog(null);
if (v == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
frame.getMediaPlayer().playMedia(file.getAbsolutePath());
}
}
// 退出播放
public static void exit() {
frame.getMediaPlayer().release();
System.exit(0);
}
// 实现播放按钮的方法
public static void play() {
frame.getMediaPlayer().play();
}
// 实现暂停按钮的方法
public static void pause() {
frame.getMediaPlayer().pause();
}
// 实现停止按钮的方法
public static void stop() {
frame.getMediaPlayer().stop();
}
// 实现点击进度条跳转的方法
public static void jumpTo(float to) {
frame.getMediaPlayer().setTime((long) (to * frame.getMediaPlayer().getLength()));
}
// 实现控制声音的方法
public static void setVol(int v) {
frame.getMediaPlayer().setVolume(v);
}
}
播放器窗体类:
package deng.test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
@SuppressWarnings("serial")
public class Window extends JFrame{
private JPanel contentPane; //顶层容器,整个播放页面的容器
private JMenuBar menuBar; //菜单栏
private JMenu mnFile,mnSetting,mnHelp; //文件菜单
private JMenuItem mnOpenVideo,mnExit; //文件菜单子目录,打开视屏、退出
private JPanel panel; //控制区域容器
private JProgressBar progress; //进度条
private JPanel progressPanel; //进度条容器
private JPanel controlPanel; //控制按钮容器
private JButton btnStop,btnPlay,btnPause; //控制按钮,停止、播放、暂停
private JSlider slider; //声音控制块
EmbeddedMediaPlayerComponent playerComponent; //媒体播放器组件
public static void main(String[] args) {
}
//MainWindow构造方法,创建视屏播放的主界面
public Window(){
setTitle(" 小婷婷专属播放器 Copyright@dengchaoqun");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(200,80,900,600);
contentPane=new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0,0));
setContentPane(contentPane);
menuBar=new JMenuBar();
setJMenuBar(menuBar);
mnFile=new JMenu("文件"); //设置菜单名
menuBar.add(mnFile);
mnSetting=new JMenu("设置");
menuBar.add(mnSetting);
mnHelp=new JMenu("帮助");
menuBar.add(mnHelp);
mnOpenVideo =new JMenuItem("打开文件"); //设置文件菜单子目录打开文件
mnFile.add(mnOpenVideo);
mnExit =new JMenuItem("退出"); //设置文件菜单子目录退出
mnFile.add(mnExit);
//打开文件
mnOpenVideo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
PlayerMain.openVideo();
}
});
//退出
mnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
PlayerMain.exit();
}
});
JPanel videoPane=new JPanel();
contentPane.add(videoPane, BorderLayout.CENTER);
videoPane.setLayout(new BorderLayout(0,0));
playerComponent=new EmbeddedMediaPlayerComponent();
videoPane.add(playerComponent);
panel=new JPanel(); //实例化控制区域容器
videoPane.add(panel,BorderLayout.SOUTH);
progressPanel=new JPanel(); //实例化进度条容器
panel.add(progressPanel, BorderLayout.NORTH);
//添加进度条
progress=new JProgressBar();
progressPanel.add(progress);
//panel.add(progress,BorderLayout.NORTH);
progress.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e){ //点击进度条调整视屏播放进度
int x=e.getX();
PlayerMain.jumpTo((float)x/progress.getWidth());
}
});
progress.setStringPainted(true);
controlPanel=new JPanel(); //实例化控制按钮容器
panel.add(controlPanel,BorderLayout.SOUTH);
//添加停止按钮
btnStop=new JButton("停止");
btnStop.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
PlayerMain.stop();
}
});
controlPanel.add(btnStop);
//添加播放按钮
btnPlay=new JButton("播放");
btnPlay.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
PlayerMain.play();
}
});
controlPanel.add(btnPlay);
//添加暂停按钮
btnPause=new JButton("暂停");
btnPause.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
//PlayerMain.dispose();
PlayerMain.pause();
}
});
controlPanel.add(btnPause);
//添加声音控制块
slider=new JSlider();
slider.setValue(80);
slider.setMaximum(100);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
PlayerMain.setVol(slider.getValue());
}
});
controlPanel.add(slider);
}
//获取播放媒体实例(某个视频)
public EmbeddedMediaPlayer getMediaPlayer() {
return playerComponent.getMediaPlayer();
}
//获取进度条实例
public JProgressBar getProgressBar() {
return progress;
}
}
效果如下:
(6)在视频播放面板上操作视频接口方法:
playerComponent = new EmbeddedMediaPlayerComponent();
panel.add(playerComponent);
//视频表面的鼠标事件监听
playerComponent.getVideoSurface().addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("**************");
}
});
(7)项目总结:应用了开源vlc,运用了jna技术,实现了多媒体视频播放。
到此这篇关于Java利用VLC开发简易视屏播放器的文章就介绍到这了,更多相关Java视屏播放器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!