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包導入到工程項目中。配置好的項目目錄如下圖所示

(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視屏播放器內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: