java創(chuàng)建簡易視頻播放器
最近有個多媒體的作業(yè),要求使用visualC++和OpenCV編寫一個簡易的視頻播放器,對于C/C++殘疾者而言是不可能的,于是萌生了用Java編寫的想法。具體經(jīng)驗分享一下。
目標(biāo):制作簡易視頻播放器
開發(fā)工具:eclipse4.5.1;VLC2.2.1
具體內(nèi)容:完成了視頻的加載、播放、退出的功能;實現(xiàn)了視頻播放過程中控制播放進程;實現(xiàn)播放過程中控制暫停。
最終程序效果圖如下:
開發(fā)過程參考學(xué)習(xí)資源:
Java framework for the vlc media player
GitHub
一、環(huán)境配置,也是最重要的一步,如果開發(fā)環(huán)境沒有配置好的話,其他的事都是瞎扯淡。
1.下載vlc(點擊打開下載vlc鏈接),下載自己電腦系統(tǒng)對應(yīng)的版本并安裝;
2.下載vlcj(點擊打開下載vlcj鏈接),把下載文件解壓后將其目錄下的
jna-3.5.2.jar、platform-3.5.2.jar、vlcj-3.8.0.jar(不同版本后綴數(shù)字可能會不同)三個文件復(fù)制到對應(yīng)的java工程目錄(新建 lib 文件夾)下;
3.下載slf4j(點擊打開slf4j下載),下載文件后解壓將其目錄下的slf4j-api-1.7.13.jar、
slf4j- nop-1.7.13.jar(不同版本后綴數(shù)字可能會不同)兩個文件復(fù)制到對應(yīng)的java工程目錄(新建 lib 文件夾)下;
4. 將vlc安裝目錄下的libvlc.dll、libvlccore.dll 兩個文件以及plugins問佳佳復(fù)制到對應(yīng)的java工程目錄下;
至此,環(huán)境文件的導(dǎo)入已經(jīng)基本完成。進入eclipse將lib文件夾下面的5個 .jar 文件添加到工作路徑。完成后java工程的目錄結(jié)構(gòu)如下:
java工程下的包里是具體的程序?qū)崿F(xiàn)代碼。
二.各種環(huán)境文件導(dǎo)入java工程之后還需要配置一下才可能夠配置到最終的工作環(huán)境。
1.在java工程下新建兩個包,分別存放程序主文件和窗體文件。在在主程序包下建一個主類,主類的main函數(shù)作如下配置:
public static void main(String[] args) { //環(huán)境配置,將vlc sdk導(dǎo)入到eclipse //if(RuntimeUtil.isWindows()){ } NativeLibrary.addSearchPath( RuntimeUtil.getLibVlcLibraryName(), "D:\\Program Files\\VideoLAN\\VLC\\sdk\\lib"); //導(dǎo)入的路徑是vlc的安裝路徑 Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(),LibVlc.class); //System.out.println(LibVlc.INSTANCE.libvlc_get_version()); }
b保存后運行一下改程序,如果不報錯的話則說明開發(fā)環(huán)境已經(jīng)配置完成。如果報錯的話可以參照上文提到的參考學(xué)習(xí)資源解決問題。
三.接下來就是具體的編碼環(huán)節(jié)了,有問題可以參照java學(xué)習(xí)工具。
具體代碼如下:
主文件類PlayerMain:
/*主程序*/ package VideoPlayer.Main; 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 VideoPlayer.Window.Window; import uk.co.caprica.vlcj.binding.LibVlc; import uk.co.caprica.vlcj.runtime.RuntimeUtil; public class PlayerMain { static Window frame; //private static final String NATIVE_LIBRARY_SEARCH_PATH = "D:\\Program Files\\VideoLAN\\VLC\\sdk\\lib"; public static void main(String[] args) { //環(huán)境配置,將vlc sdk導(dǎo)入到eclipse //if(RuntimeUtil.isWindows()){ } NativeLibrary.addSearchPath( RuntimeUtil.getLibVlcLibraryName(), "D:\\Program Files\\VideoLAN\\VLC\\sdk\\lib"); //導(dǎo)入的路徑是vlc的安裝路徑 Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(),LibVlc.class); //System.out.println(LibVlc.INSTANCE.libvlc_get_version()); //創(chuàng)建主程序界面運行窗體 EventQueue.invokeLater(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try{ frame=new Window(); frame.setVisible(true); //frame.getMediaPlayer().playMedia("F:\\Videos\\Let.mp4"); //直接播放視屏,參數(shù)是視屏文件的絕對路徑 frame.getMediaPlayer().prepareMedia("F:\\Videos\\Let_It_Go.mp4"); //控制播放視屏 new SwingWorker<String, Integer>() { @Override protected String doInBackground() throws Exception { // TODO Auto-generated method stub while (true) { //獲取視頻播放進度并且按百分比顯示 long total=frame.getMediaPlayer().getLength(); 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 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); } //實現(xiàn)播放按鈕的方法 public static void play() { frame.getMediaPlayer().play(); } //實現(xiàn)暫停按鈕的方法 public static void pause() { frame.getMediaPlayer().pause(); } //實現(xiàn)停止按鈕的方法 public static void stop() { frame.getMediaPlayer().stop(); } //實現(xiàn)點擊進度條跳轉(zhuǎn)的方法 public static void jumpTo(float to) { frame.getMediaPlayer().setTime((long)(to*frame.getMediaPlayer().getLength())); } //實現(xiàn)控制聲音的方法 public static void setVol(int v) { frame.getMediaPlayer().setVolume(v); } }
窗體文件類Window:
/*視屏播放器主界面*/ package VideoPlayer.Window; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; 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 VideoPlayer.Main.PlayerMain; import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent; import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer; public class Window extends JFrame{ private JPanel contentPane; //頂層容器,整個播放頁面的容器 private JMenuBar menuBar; //菜單欄 private JMenu mnFile,mnSetting,mnHelp; //文件菜單 private JMenuItem mnOpenVideo,mnExit; //文件菜單子目錄,打開視屏、退出 private JPanel panel; //控制區(qū)域容器 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構(gòu)造方法,創(chuàng)建視屏播放的主界面 public Window(){ setTitle(" VideoPlayer Copyright@2015 by 南柯一夢"); 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("文件"); //設(shè)置菜單名 menuBar.add(mnFile); mnSetting=new JMenu("設(shè)置"); menuBar.add(mnSetting); mnHelp=new JMenu("幫助"); menuBar.add(mnHelp); mnOpenVideo =new JMenuItem("打開文件"); //設(shè)置文件菜單子目錄打開文件 mnFile.add(mnOpenVideo); mnExit =new JMenuItem("退出"); //設(shè)置文件菜單子目錄退出 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(); //實例化控制區(qū)域容器 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){ //點擊進度條調(diào)整視屏播放進度 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.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; } }
至此使用java開發(fā)簡易的視頻播放器就已經(jīng)完成了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在SpringBoot當(dāng)中使用Thymeleaf視圖解析器的詳細教程
Thymeleaf是一款開源的模板引擎,它允許前端開發(fā)者使用HTML與XML編寫動態(tài)網(wǎng)頁,hymeleaf的主要特點是將表達式語言嵌入到HTML結(jié)構(gòu)中,它支持Spring框架,使得在Spring MVC應(yīng)用中集成非常方便,本文給大家介紹了在SpringBoot當(dāng)中使用Thymeleaf視圖解析器的詳細教程2024-09-09淺談SpringBoot在使用測試的時候是否需要@RunWith
本文主要介紹了淺談SpringBoot在使用測試的時候是否需要@RunWith,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01基于Spring接口集成Caffeine+Redis兩級緩存
這篇文章主要介紹了基于Spring接口集成Caffeine+Redis兩級緩存,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07SpringBoot通過Filter實現(xiàn)整個項目接口的SQL注入攔截詳解
這篇文章主要介紹了SpringBoot通過Filter實現(xiàn)整個項目接口的SQL注入攔截詳解,SQL注入是比較常見的網(wǎng)絡(luò)攻擊方式之一,在客戶端在向服務(wù)器發(fā)送請求的時候,sql命令通過表單提交或者url字符串拼接傳遞到后臺持久層,最終達到欺騙服務(wù)器執(zhí)行惡意的SQL命令,需要的朋友可以參考下2023-12-12Jpa中Specification的求和sum不生效原理分析
這篇文章主要為大家介紹了Jpa中Specification的求和sum不生效原理示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08詳解SpringBoot結(jié)合策略模式實戰(zhàn)套路
這篇文章主要介紹了詳解SpringBoot結(jié)合策略模式實戰(zhàn)套路,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10SpringBoot使用?Sleuth?進行分布式跟蹤的過程分析
Spring Boot Sleuth是一個分布式跟蹤解決方案,它可以幫助您在分布式系統(tǒng)中跟蹤請求并分析性能問題,Spring Boot Sleuth是Spring Cloud的一部分,它提供了分布式跟蹤的功能,本文將介紹如何在Spring Boot應(yīng)用程序中使用Sleuth進行分布式跟蹤,感興趣的朋友一起看看吧2023-10-10