视频播放器|Java 视频播放器制作,包含代码流程和资源需求。

  • 简单说明
技术方面还请各位海涵,代码和资源引用还有下载方式全部在这里了,这是一个利用Java Swing实现一个简单的视频播放器。由于视频播放器需要解码,这里引用的Java媒体框架(JMF)完成视频解码任务。
【视频播放器|Java 视频播放器制作,包含代码流程和资源需求。】
  • 代码部分
package cn.video.play; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.JSpinner; import javax.swing.SpinnerNumberModel; import javax.swing.SwingWorker; import javax.swing.border.EmptyBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import com.sun.jna.Native; import com.sun.jna.NativeLibrary; import uk.co.caprica.vlcj.binding.LibVlc; import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent; import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer; import uk.co.caprica.vlcj.runtime.RuntimeUtil; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.FlowLayout; import javax.swing.JLabel; public class VideoPlayer extends JFrame { private static final long serialVersionUID = 1L; private static boolean flag = true; private JPanel contentPane; private EmbeddedMediaPlayerComponent playerComponent; private JSlider sirVideoProgressBar; private JMenuItem mnOpenFile; private JMenuItem mnExit; private JButton btnPlay; private JButton btnStop; private JButton btnPause; private String currentPlayFilePath; private JLabel lblVideoProgress; public static void main(String[] args) { // 加载解码文件 NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "E:\\MyTool\\VLC"); // 导入的路径是vlc的安装路径 Native.load(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class); // 创建主程序界面运行窗体 EventQueue.invokeLater(new Runnable() { @Override public void run() { try { //初始化播放器 VideoPlayer frame = new VideoPlayer(); frame.setVisible(true); frame.getMediaPlayer().playMedia("video\\1.mp4"); // 直接播放视屏,参数是视屏文件的绝对路径 // 初始化音量 frame.setVol(70); //初始化解码工具 frame.playExecute(); } catch (Exception e) { e.printStackTrace(); } } }); } public VideoPlayer() { initPlay(); } // 获取播放媒体实例(某个视频) public EmbeddedMediaPlayer getMediaPlayer() { return playerComponent.getMediaPlayer(); } // 获取进度条实例 public JSlider getsirVideoProgressBar() { return sirVideoProgressBar; } // 关闭播放器释放资源 public void dispose() { flag = false; this.getMediaPlayer().stop(); this.dispose(); } // 打开文件 public void openVideo() { JFileChooser chooser = new JFileChooser(); int v = chooser.showOpenDialog(null); if (v == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); String name = file.getName().toLowerCase(); // 文件过滤 if (name.indexOf("avi")>0 || name.indexOf("mp4")>0 || name.indexOf("rmvb")>0 || name.indexOf("rmvb")>0 || name.indexOf("rmvb")>0) { currentPlayFilePath = file.getAbsolutePath(); this.getMediaPlayer().playMedia(currentPlayFilePath); } System.out.println("请选择正确视频文件"); return; } } // 退出播放 public void exit() { this.getMediaPlayer().release(); System.exit(0); } // 实现播放按钮的方法 public void play() { this.getMediaPlayer().play(); } // 实现暂停按钮的方法 public void pause() { this.getMediaPlayer().pause(); } // 实现停止按钮的方法 public void stop() { this.getMediaPlayer().stop(); } // 实现点击进度条跳转的方法 public void jumpTo(float to) { this.getMediaPlayer().setTime((long) (to * this.getMediaPlayer().getLength())); } // 实现控制声音的方法 public void setVol(int v) { this.getMediaPlayer().setVolume(v); } class ButtonActionListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JButton item = (JButton) e.getSource(); if (item == btnPlay) { play(); } else if (item == btnStop) { stop(); } else if (item == btnPause) { pause(); } } } class PlayTool extends SwingWorker{ @Override protected String doInBackground() throws Exception { while (flag) { // 获取视频播放进度并且按百分比显示 long total = getMediaPlayer().getLength(); long curr = 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) { getsirVideoProgressBar().setValue(v); lblVideoProgress.setText(v+"%"); } } } //初始化播放器 public void initPlay() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 1200, 800); setLocationRelativeTo(null); // 容器 contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); // 播放界面 playerComponent = new EmbeddedMediaPlayerComponent(); playerComponent.setCursorEnabled(true); contentPane.add(playerComponent); // 设置一个菜单栏 JMenuBar menuBar = new JMenuBar(); this.setJMenuBar(menuBar); this.setContentPane(contentPane); JMenu mnFile = new JMenu("文件"); // 设置菜单名 menuBar.add(mnFile); mnOpenFile = new JMenuItem("打开文件"); // 设置文件菜单子目录打开文件 mnFile.add(mnOpenFile); mnExit = new JMenuItem("退出"); // 设置文件菜单子目录退出 mnFile.add(mnExit); // 打开文件 mnOpenFile.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { openVideo(); } }); // 退出 mnExit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dispose(); exit(); } }); // 添加控制栏容器 JPanel pnlControllerBar = new JPanel(); contentPane.add(pnlControllerBar, BorderLayout.SOUTH); sirVideoProgressBar = new JSlider(); sirVideoProgressBar.setValue(0); sirVideoProgressBar.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { // 点击进度条调整视屏播放进度 int x = e.getX(); jumpTo((float) x / sirVideoProgressBar.getWidth()); lblVideoProgress.setText(x+"%"); } }); // 创建监听器 ButtonActionListener buttonAtionListener = new ButtonActionListener(); // 暂停按键 btnPause = new JButton("暂停"); btnPause.addActionListener(buttonAtionListener); // 停止按键 btnStop = new JButton("停止"); btnStop.addActionListener(buttonAtionListener); // 播放按键 btnPlay = new JButton("播放"); btnPlay.addActionListener(buttonAtionListener); // 创建音量控制 JSpinner snrVolumeControl = new JSpinner(); snrVolumeControl.setModel(new SpinnerNumberModel(70, 0, 100, 10)); snrVolumeControl.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int temp = (int) snrVolumeControl.getValue(); setVol(temp); } }); pnlControllerBar.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); pnlControllerBar.add(sirVideoProgressBar); lblVideoProgress = new JLabel("0%"); pnlControllerBar.add(lblVideoProgress); pnlControllerBar.add(btnPlay); pnlControllerBar.add(btnStop); pnlControllerBar.add(btnPause); pnlControllerBar.add(snrVolumeControl); } //执行视频解码 public void playExecute() { new PlayTool().execute(); } }



  • 视频播放器资源引用部分(项目必备)
VCL官网下载地址:https://www.videolan.org/
VCL用作于视频解码库,使用前,先从安装目录下拷贝plugins、libvlc.dll、libvlccore.dll到项目下
vlcj官网下载地址:http://capricasoftware.co.uk/projects/vlcj
jna、vlcj包都在下载地址
slf4j官网下载地址:https://www.slf4j.org/download.html

Java视频播放器 完整代码下载地址:https://download.csdn.net/download/q74269665/11752487

视频播放器|Java 视频播放器制作,包含代码流程和资源需求。
文章图片




    推荐阅读