Java利用VLC开发简易视屏播放器功能

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包导入到工程项目中。配置好的项目目录如下图所示
Java利用VLC开发简易视屏播放器功能
文章图片

(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() {@Overridepublic void run() {try {frame = new Window(); frame.setVisible(true); frame.getMediaPlayer().playMedia("video//style.avi"); // 直接播放视屏,参数是视屏文件的绝对路径// frame.getMediaPlayer().prepareMedia("video//test.mp4"); // //控制即将播放的视频new SwingWorker() {@Overrideprotected String doInBackground() throws Exception {// TODO Auto-generated method stubwhile (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 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() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubPlayerMain.openVideo(); }}); //退出mnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubPlayerMain.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() {@Overridepublic 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() {@Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stubPlayerMain.stop(); }}); controlPanel.add(btnStop); //添加播放按钮btnPlay=new JButton("播放"); btnPlay.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stubPlayerMain.play(); }}); controlPanel.add(btnPlay); //添加暂停按钮btnPause=new JButton("暂停"); btnPause.addMouseListener(new MouseAdapter() {@Overridepublic 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() {@Overridepublic void stateChanged(ChangeEvent e) {// TODO Auto-generated method stubPlayerMain.setVol(slider.getValue()); }}); controlPanel.add(slider); }//获取播放媒体实例(某个视频)public EmbeddedMediaPlayer getMediaPlayer() {return playerComponent.getMediaPlayer(); }//获取进度条实例public JProgressBar getProgressBar() {return progress; }}

效果如下:
Java利用VLC开发简易视屏播放器功能
文章图片

(6)在视频播放面板上操作视频接口方法:
playerComponent = new EmbeddedMediaPlayerComponent(); panel.add(playerComponent); //视频表面的鼠标事件监听playerComponent.getVideoSurface().addMouseListener(new MouseListener() {@Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mousePressed(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseEntered(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stubSystem.out.println("**************"); }});

(7)项目总结:应用了开源vlc,运用了jna技术,实现了多媒体视频播放。
【Java利用VLC开发简易视屏播放器功能】到此这篇关于Java利用VLC开发简易视屏播放器的文章就介绍到这了,更多相关Java视屏播放器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读