JavaSwing_基于VLC media player内核制作一个简单的视频播放器
Posted 温柔而已、
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaSwing_基于VLC media player内核制作一个简单的视频播放器相关的知识,希望对你有一定的参考价值。
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
一、VLC media player的下载与安装
提示:VideoLAN 的官网下载 VLC media player,制作的视频播放器内核需要调用 VLC media player
VideoLAN:http://www.videolan.org/
VLC command-line help:https://wiki.videolan.org/VLC_command-line_help/
二、下载相关的在线开源库:vlcj
提示:只需要加载必备的 5 个 jar 包就可以:
三、开发步骤
1.1、创建项目并引入所需jar包
项目结构如下:
1.2、核心代码
1.2.1、VideoPlayer.java(主类):
代码如下(示例):
package com.siwuxie095.view;
import java.awt.EventQueue;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.SwingWorker;
import com.siwuxie095.main.MainWindow;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.discovery.NativeDiscovery;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class VideoPlayer
/**
*
* VLC播放器系统库的路径:D:\\VLC media player\\VLC
*
* 注意需要将路径中的反斜杠改为斜杠,或 使用双反斜杠(即通过转义符进行转义)
*
* 系统库一般是在含有 libvlc.dll、libvlccore.dll 的路径
*
*/
private static final String NATIVE_LIBRARY_SEARCH_PATH = "D:/VLC media player/VLC";
// 将声明转移到类中,并设为 static
static MainWindow frame;
public static void main(String[] args)
// (1)法一:首先要找到本机库(VLC播放器的系统库),这是自动搜索本机库的位置
boolean found = new NativeDiscovery().discover();
System.out.println(found);
System.out.println(LibVlc.INSTANCE.libvlc_get_version());
// 判断当前的系统是否是Windows
// 还可以判断Mac和Linux,以传入不同的路径
if (RuntimeUtil.isWindows())
// (2)法二:手动设置本机库(VLC播放器的系统库)的路径
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), NATIVE_LIBRARY_SEARCH_PATH);
System.out.println(LibVlc.INSTANCE.libvlc_get_version());
// 加载VLC播放器的系统库
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
// 在主方法中创建窗体
EventQueue.invokeLater(new Runnable()
public void run()
try
frame = new MainWindow();
frame.setVisible(true);
// 字幕的编码
String options = "--subsdec-encoding=GB18030";
/**
* prepareMedia() 先准备而不是马上播放 可传入视频文件的路径(和播放参数,如:字幕编码) 这里使用双反斜杠(通过转义符进行转义)
* 关于播放参数,可以参考 VLC command-line
* 链接:https://wiki.videolan.org/VLC_command-line_help/ 进入后搜索 subsdec 设定字幕编码
* 还可以传入更多播放参数
*/
frame.getMediaPlayer().prepareMedia("C:\\\\Users\\\\siwux\\\\Desktop\\\\" + "testvideo\\\\test.mp4", options);
frame.getMediaPlayer().toggleFullScreen();
// 创建一个 SwingWorker 线程,用于实时调节进度
// 注意:创建完毕,最后要 execute() 将它运行起来
new SwingWorker<String, Integer>()
@Override
protected String doInBackground() throws Exception
while (true)
// 视频总时长,以毫秒计
long total = frame.getMediaPlayer().getLength();
// 当前所看时长,以毫秒计
long curr = frame.getMediaPlayer().getTime();
// 百分比,并强转为 float
float percent = (float) curr / total;
// 因为进度条不是按百分比进行计算,而是 0-100 的数值范围
// 所以要乘 100,并强转为 int,publish() 到 process()
// (进度条范围可设置,如果改为 0-120,就要乘 120)
publish((int) (percent * 100));
// 每隔 0.1 秒(100毫秒)更新一次进度条,如果不加则刷新过快
Thread.sleep(100);
protected void process(java.util.List<Integer> chunks)
// 创建int型变量 value 接收 chunks 中的值
for (int value : chunks)
frame.getProgressBar().setValue(value);
;
.execute();
catch (Exception e)
e.printStackTrace();
);
// 播放
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 openVideo()
// 创建文件选择器:JFileChooser
JFileChooser chooser = new JFileChooser();
// 将父级窗体设置成 null
int v = chooser.showOpenDialog(null);
if (v == JFileChooser.APPROVE_OPTION)
File file = chooser.getSelectedFile();
// 获取到文件的绝对路径,开始播放
frame.getMediaPlayer().playMedia(file.getAbsolutePath());
public static void openSubtitle()
JFileChooser chooser = new JFileChooser();
int v = chooser.showOpenDialog(null);
if (v == JFileChooser.APPROVE_OPTION)
File file = chooser.getSelectedFile();
// 设定字幕,直接传入 file 对象
frame.getMediaPlayer().setSubTitleFile(file);
public static void exit()
// 在退出之前,先释放播放器的资源
frame.getMediaPlayer().release();
System.exit(0);
public static void setVol(int vol)
// 设定音量
frame.getMediaPlayer().setVolume(vol);
1.2.2、MainWindow.java:
代码如下(示例):
package com.siwuxie095.main;
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.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;
import com.siwuxie095.view.VideoPlayer;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
@SuppressWarnings("serial")
public class MainWindow extends JFrame
private JPanel contentPane;
// 创建播放器的界面需要使用 EmbeddedMediaPlayerComponent
EmbeddedMediaPlayerComponent playerComponent;
private JPanel bottomPane;
private JButton btnPlay;
private JButton btnPause;
private JButton btnStop;
private JPanel controlPane;
private JProgressBar progress;
private JMenuBar menuBar;
private JMenu mnFile;
private JMenuItem mntmOpenvideo;
private JMenuItem mntmOpensubtitle;
private JMenuItem mntmExit;
private JSlider slider;
/**
*
* Create the frame.
*
*/
public MainWindow()
try
// 设置是否显示右上角设置按钮;
UIManager.put("RootPane.setupButtonVisible", false);
// 让JToolBar的UI不使用渐变图片而使用传统的纯色来填充背景
UIManager.put("ToolBar.isPaintPlainBackground", Boolean.TRUE);
// 开启/关闭窗口在不活动时的半透明效果
BeautyEyeLNFHelper.translucencyAtFrameInactive = true;
// 加载beautyeye的javaSwing风格,强立体半透明边框;
BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.translucencyAppleLike;
// 设置主题为BeautyEye
org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
catch (Exception e)
e.printStackTrace();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
mnFile = new JMenu("File");
menuBar.add(mnFile);
mntmOpenvideo = new JMenuItem("OpenVideo");
mntmOpenvideo.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
VideoPlayer.openVideo();
);
mnFile.add(mntmOpenvideo);
mntmOpensubtitle = new JMenuItem("OpenSubtitle");
mntmOpensubtitle.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
VideoPlayer.openSubtitle();
);
mnFile.add(mntmOpensubtitle);
mntmExit = new JMenuItem("Exit");
mntmExit.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
VideoPlayer.exit();
);
mnFile.add(mntmExit);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel videoPane = new JPanel();
contentPane.add(videoPane, BorderLayout.CENTER);
videoPane.setLayout(new BorderLayout(0, 0));
// 在 videoPane 创建之后实例化 playerComponent
playerComponent = new EmbeddedMediaPlayerComponent();
// 将 playerComponent 添加到 videoPane 中,并指定布局
videoPane.add(playerComponent, BorderLayout.CENTER);
bottomPane = new JPanel();
videoPane.add(bottomPane, BorderLayout.SOUTH);
bottomPane.setLayout(new BorderLayout(0, 0));
controlPane = new JPanel();
bottomPane.add(controlPane);
btnStop = new JButton("Stop");
controlPane.add(btnStop);
btnStop.setFocusable(false);
btnPlay = new JButton("Play");
controlPane.add(btnPlay);
btnPlay.setFocusable(false);
btnPause = new JButton("Pause");
controlPane.add(btnPause);
btnPause.setFocusable(false);
slider = new JSlider();
slider.setFocusable(false);
// 关于默认音量的设定要在 stateChanged 事件之前
// 如果要设定最大音量和最小音量也是如此
slider.setValue(20);
// 为音量调节 slider 添加 stateChanged 事件
slider.addChangeListener(new ChangeListener()
public void stateChanged(ChangeEvent e)
VideoPlayer.setVol(slider.getValue());
);
controlPane.add(slider);
progress = new JProgressBar();
progress.setFocusable(false);
// 为进度条 progress 添加 mouseClicked 事件
progress.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
// 获取鼠标点击在进度条上的位置
int x = e.getX();
// 计算点击位置占进度条总长的百分比
float per = (float) x / progress.getWidth();
VideoPlayer.jumpTo(per);
);
// 让进度条 progress 显示数字百分比
progress.setStringPainted(true);
bottomPane.add(progress, BorderLayout.NORTH);
// 为 Stop 按钮添加 mouseClicked 事件
btnStop.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
VideoPlayer.stop();
);
// 为 Play 按钮添加 mouseClicked 事件
btnPlay.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
VideoPlayer.play();
);
// 为 Pause 按钮添加 mouseClicked 事件
btnPause.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
VideoPlayer.pause();
);
// 返回媒体播放器的实例
public EmbeddedMediaPlayer getMediaPlayer()
return playerComponent.getMediaPlayer();
// 返回JProgressBar的实例
public JProgressBar getProgressBar()
return progress;
四、运行效果图
项目链接
基于vlc-Qt的视频播放器(支持添加视频列表单曲循环等)
基于libvlc和Qt实现了一个视频播放器,可实现列表循环播放,单曲播放等,效果好于Qt自带的视频播放库。网上已有诸多基于vlc库的视频播放器,但设计列表播放的资源较少,基于vlc实现列表播放主要利用了libvlc_media_list_player_t和libvlc_media_list_t两个类。
也可查看本人github项目,附链接:
github主页:https://github.com/qin11152
环境准备及接口实现
首先需要下载vlc相关库文件,可以在http://download.videolan.org/pub/videolan/vlc/下载对应的版本,安装后可在安装目录下找到对应的vlc库文件。在项目的包含目录中包含头文件,并链接libvlc和libvlccore两个动态库。之后便可进行相应开发。
相关编码实现
libvlc_instance_t* m_ptrVlcInstance = libvlc_new(0, NULL);
使用vlc库首先需要new一个libvlc的实例,类型为libvlc_instance_t,后续相关操作均基于此对象。
播放器实例
libvlc_media_player_t* m_ptrMediaPlayer= libvlc_media_player_new(m_ptrVlcInstance); //1媒体播放器
libvlc_media_player_set_hwnd(m_ptrMediaPlayer, (void *)ui.playWidget->winId()); //2设置播放窗口
libvlc_media_player_play(): //3播放
libvlc_media_player_pause(): //4暂停
libvlc_media_player_stop(); //5停止
libvlc_media_player_t代表一个vlc的媒体播放器,可用于播放视频,但其功能不仅用于播放视频。上述代码中的2则是指定了媒体播放的界面,通过传递句柄可以指定媒体在具体的某一个widget上进行播放。后续的几个接口可以进行播放,暂停和结束,但因本项目为列表播放,所以不使用上述控制接口,若进行单视频播放可使用上述接口。
媒体列表及媒体列表播放器
libvlc_media_list_t* m_ptrMediaList=libvlc_media_list_new(m_ptrVlcInstance); //1媒体列表
libvlc_media_list_player_t* m_ptrListMediaPlayer = libvlc_media_list_player_new(m_ptrVlcInstance); //2媒体列表播放器
libvlc_media_list_player_set_media_list(m_ptrListMediaPlayer, m_ptrMediaList); //3为列表播放器设置媒体列表
libvlc_media_list_player_set_media_player(m_ptrListMediaPlayer, m_ptrMediaPlayer); //4为列表播放器设置播放器实例
libvlc_media_list_player_set_playback_mode(m_ptrListMediaPlayer, libvlc_playback_mode_repeat); //5循环模式
libvlc_media_list_t(媒体列表)和libvlc_media_list_player_t(媒体列表播放器)两个对象是实现列表播放的重点。实例化两个对象后首先需要将媒体列表设定到媒体列表播放器中,如上述代码3所示。之后需要为媒体列表播放器设定播放器实体,如上述代码4所示,此播放器实体就是前面实例化的libvlc_media_player_t。可通过代码5设定列表播放器的循环方式(列表循环,单曲循环)。
libvlc_media_t* m_ptrMedia =libvlc_media_new_path(m_ptrVlcInstance,item.toUtf8().data()); //1实例化一个媒体对象
libvlc_media_list_add_media(m_ptrMediaList, m_ptrMedia); //2添加媒体对象到媒体列表
libvlc_media_parse(m_ptrMedia); //3释放资源
像媒体列表中传入媒体对象时,首先需要实例化一个媒体对象,实例化时需指定媒体所在的路径,然后调用上述代码中第2行接口将媒体对象塞到已有的媒体列表中即可。执行完后需要释放媒体对象。存在多个媒体时只需要循环的添加即可。媒体列表和媒体列表播放器也提供了诸多借口来进行控制,如下面代码所示,具体功能可参考注释。
libvlc_media_list_count( libvlc_media_list_t *p_ml ); //返回媒体列表中数量
libvlc_media_list_remove_index( libvlc_media_list_t *p_ml, int i_pos ); //根据index移除媒体列表中某一项
libvlc_media_list_insert_media( libvlc_media_list_t *p_ml, libvlc_media_t *p_md, int i_pos ); //像媒体列表中指定位置插入播放对象
libvlc_media_list_player_play(libvlc_media_list_player_t * p_mlp); //列表播放开始
libvlc_media_list_player_pause(libvlc_media_list_player_t * p_mlp); //列表播放暂停
libvlc_media_list_player_play_item_at_index(libvlc_media_list_player_t * p_mlp,int i_index); //播放指定位置视频
libvlc_media_list_player_previous(libvlc_media_list_player_t * p_mlp); //播放前一个视频
libvlc_media_list_player_next(libvlc_media_list_player_t * p_mlp); //播放下一个视频
回调事件管理
libvlc_event_manager_t* em = libvlc_media_player_event_manager(m_ptrMediaPlayer); //回到事件管理器
libvlc_event_attach(em, libvlc_MediaPlayerTimeChanged, vlcEvents, this); //注册关注的回调事件
void vlcEvents(const libvlc_event_t *ev, void *param)
Q_UNUSED(param);
switch (ev->type)
case libvlc_MediaPlayerTimeChanged:
//从回调事件中得到播放时间
auto tmpTime= ev->u.media_player_time_changed.new_time;
QString time = "";
m_ptrThis->transferTime(time, tmpTime);
m_ptrThis->ui.horizontalSlider->setValue(tmpTime);
m_ptrThis->ui.curTimeLabel->setText(time);
break;
default:
break;
在使用vlc库的过程中,可以通过注册相关回调。使用方法如上述代码前半部分所示,修改libvlc_event_attach中第二个参数值即可得到不同事件的回调。对于回调事件的处理则需要重写vlcEvents函数实现,如上述代码中后半部分所示,对于播放时间、播放进度、播放视频切换至下一个等事件均需这样获取,然后才可以进行下一步处理。
其它接口
libvlc_media_get_duration( libvlc_media_t *p_md ); //获取视频时长
libvlc_media_player_set_time( libvlc_media_player_t *p_mi, libvlc_time_t i_time ); //设置视频播放至某一位置
libvlc_audio_get_volume( libvlc_media_player_t *p_mi ); //获取音量
libvlc_audio_set_volume( libvlc_media_player_t *p_mi, int i_volume ); //设置音量
上述代码中列出了一些常用的接口,其作用如注释所示,如需要更详细介绍或其它接口,可查看文档或库文件。
实际工程
代码如下,也可在本人github中查看下载具体工程。github地址:https://github.com/qin11152
#include "MyMediaplayer.h"
#include <algorithm>
int MyMediaplayer::curTime = 0 ;
MyMediaplayer* MyMediaplayer::m_ptrThis = nullptr ;
MyMediaplayer::MyMediaplayer(QWidget *parent)
: QWidget(parent)
ui.setupUi(this);
initData();
initConnect();
m_ptrThis = this;
void MyMediaplayer::onPlayCurrentVedio()
if (m_ptrMediaPlayer == nullptr)
return;
if (bool(libvlc_media_list_player_is_playing(m_ptrListMediaPlayer)))
return;
libvlc_media_list_player_play(m_ptrListMediaPlayer);
int volumes = libvlc_audio_get_volume(m_ptrMediaPlayer);
ui.VolumeSlider->setValue(volumes);
void MyMediaplayer::onPauseCurrentVideo()
if (m_ptrListMediaPlayer == nullptr)
return;
if (bool(libvlc_media_list_player_is_playing(m_ptrListMediaPlayer)))
m_bIsPlay = false;
libvlc_media_list_player_pause(m_ptrListMediaPlayer);
void MyMediaplayer::onStopCurrentVideo()
if (m_ptrListMediaPlayer == nullptr)
return;
m_bIsPlay = false;
libvlc_media_list_player_stop(m_ptrListMediaPlayer);
freeVlc();
void MyMediaplayer::onOpenPushButtonClicked()
m_vecPlayList.clear();
if (m_ptrVlcInstance)
freeVlc();
QStringList tmp = QFileDialog::getOpenFileNames(this, u8"打开视频", ".", u8"视频文件(*.mp4 *.mp3 *.flv *.3gp *.rmvb)");
if (tmp.empty())
return;
for (auto& item : tmp)
if (std::find(m_vecPlayList.begin(), m_vecPlayList.end(), item) == m_vecPlayList.end())
m_vecPlayList.push_back(QDir::toNativeSeparators(item));
m_strCurPath = QDir::toNativeSeparators(tmp[0]);
setplayList();
void MyMediaplayer::onPausePushButtonClicked()
onPauseCurrentVideo();
void MyMediaplayer::onPlayPushButtonClicked()
onPlayCurrentVedio();
void MyMediaplayer::onStopPushButtonClicked()
onStopCurrentVideo();
void MyMediaplayer::onSliderRelease(int val)
int time = ui.horizontalSlider->value();
libvlc_media_player_set_time(m_ptrMediaPlayer, val);
void MyMediaplayer::onSingleButtonClicked()
if (m_bIsSingleCycle)
ui.pushButton->setText(u8"列表循环");
m_bIsSingleCycle = false;
if (nullptr != m_ptrListMediaPlayer)
libvlc_media_list_player_set_playback_mode(m_ptrListMediaPlayer, libvlc_playback_mode_loop);
else
ui.pushButton->setText(u8"单曲循环");
m_bIsSingleCycle = true;
if (nullptr != m_ptrListMediaPlayer)
libvlc_media_list_player_set_playback_mode(m_ptrListMediaPlayer, libvlc_playback_mode_repeat);
void MyMediaplayer::onSignalFreshButtonClicked()
/*if (!m_ptrVlcInstance)
return;
QString item = u8"D:/技改技措业务_产品技术.mp4";
item = QDir::toNativeSeparators(item);
m_ptrMedia = libvlc_media_new_path(m_ptrVlcInstance, item.toUtf8().data());
if (!m_ptrMedia)
return;
libvlc_media_list_add_media(m_ptrMediaList, m_ptrMedia);
libvlc_media_parse(m_ptrMedia);
libvlc_media_release(m_ptrMedia);*/
return;
void MyMediaplayer::initData()
ui.VolumeSlider->setMinimum(0);
ui.VolumeSlider->setMaximum(100);
m_ptrFileDialog = new QFileDialog(this);
m_ptrFileDialog->hide();
bool MyMediaplayer::initVlcData()
m_ptrVlcInstance = libvlc_new(0, NULL);
if (!m_ptrVlcInstance)
qDebug() << "qqqq create vlc failed";
qDebug() << libvlc_errmsg;
freeVlc();
return false;
m_ptrListMediaPlayer = libvlc_media_list_player_new(m_ptrVlcInstance);
if (!m_ptrListMediaPlayer)
qDebug() << "qqqq create m_ptrListMediaPlayer failed";
freeVlc();
return false;
m_ptrMediaList = libvlc_media_list_new(m_ptrVlcInstance);
if (!m_ptrMediaList)
qDebug() << "qqqq create m_ptrMediaList failed";
freeVlc();
return false;
m_ptrMediaPlayer = libvlc_media_player_new(m_ptrVlcInstance);
libvlc_video_set_mouse_input(m_ptrMediaPlayer, false);
libvlc_video_set_key_input(m_ptrMediaPlayer, false);
if (!m_ptrMediaPlayer)
freeVlc();
qDebug() << "qqqq my media player failed";
return false;
libvlc_event_manager_t* em = libvlc_media_player_event_manager(m_ptrMediaPlayer);
libvlc_event_attach(em, libvlc_MediaPlayerTimeChanged, vlcEvents, this);
libvlc_event_attach(em, libvlc_MediaPlayerEndReached, vlcEvents, this);
libvlc_event_attach(em, libvlc_MediaPlayerStopped, vlcEvents, this);
libvlc_event_attach(em, libvlc_MediaPlayerPlaying, vlcEvents, this);
libvlc_event_attach(em, libvlc_MediaPlayerPaused, vlcEvents, this);
libvlc_event_attach(em, libvlc_MediaPlayerPositionChanged, vlcEvents, this);
libvlc_event_attach(em, libvlc_MediaPlayerLengthChanged, vlcEvents, this);
libvlc_event_attach(em, libvlc_MediaPlayerTitleChanged, vlcEvents, this);
libvlc_event_attach(em, libvlc_MediaPlayerVout, vlcEvents, this);
//libvlc_media_player_set_hwnd(m_ptrMediaPlayer, (void *)ui.playWidget->winId());
return true;
void MyMediaplayer::initConnect()
connect(ui.openPushButton, &QPushButton::clicked, this, &MyMediaplayer::onOpenPushButtonClicked);
connect(ui.playPushButton, &QPushButton::clicked, this, &MyMediaplayer::onPlayPushButtonClicked);
connect(ui.pausePushButton, &QPushButton::clicked, this, &MyMediaplayer::onPausePushButtonClicked);
connect(ui.stopPushButton, &QPushButton::clicked, this, &MyMediaplayer::onStopPushButtonClicked);
connect(ui.horizontalSlider, &QSlider::sliderMoved, this, &MyMediaplayer::onSliderRelease);
connect(ui.pushButton, &QPushButton::clicked, this, &MyMediaplayer::onSingleButtonClicked);
connect(ui.VolumeSlider, &QSlider::valueChanged, this, [&](int val)
if (nullptr != m_ptrMediaPlayer)
qDebug() << "qqqq val:" << val;
libvlc_audio_set_volume(m_ptrMediaPlayer, val);
);
void MyMediaplayer::freeVlc()
if (m_ptrMedia)
libvlc_media_release(m_ptrMedia);
m_ptrMedia = nullptr;
if (m_ptrMediaPlayer)
libvlc_media_player_stop(m_ptrMediaPlayer);
libvlc_media_player_release(m_ptrMediaPlayer);
m_ptrMediaPlayer = nullptr;
if (m_ptrVlcInstance)
libvlc_release(m_ptrVlcInstance);
m_ptrVlcInstance = nullptr;
if (m_ptrListMediaPlayer)
libvlc_media_list_player_stop(m_ptrListMediaPlayer);
libvlc_media_list_player_release(m_ptrListMediaPlayer);
m_ptrListMediaPlayer = nullptr;
if (m_ptrMediaList)
libvlc_media_list_release(m_ptrMediaList);
m_ptrMediaList = nullptr;
void MyMediaplayer::transferTime(QString& time, int msTime)
msTime = msTime / 1000;
int hour = msTime / 3600;
msTime = msTime % 3600;
int minute = msTime / 60;
msTime = msTime % 60;
if (hour > 0)
time += QString::number(hour);
time += ":";
if (minute < 10)
time += "0";
time += QString::number(minute);
time += ":";
else
time += QString::number(minute);
time += ":";
if (msTime < 10)
time += "0";
time += QString::number(msTime);
else
time += QString::number(msTime);
void MyMediaplayer::setplayList()
if (m_ptrVlcInstance)
freeVlc();
if (!initVlcData())
return;
bool first = true;
for (auto& item : m_vecPlayList)
qDebug() << "qqqq path is" << item.toUtf8().data();
m_ptrMedia = libvlc_media_new_path(m_ptrVlcInstance, item.toUtf8().data());
if (m_ptrMedia)
qDebug() << "qqqq create media succ";
else
qDebug() << "qqqq create media failed";
libvlc_media_list_add_media(m_ptrMediaList, m_ptrMedia);
libvlc_media_parse(m_ptrMedia);
if (first)
first = false;
int time = libvlc_media_get_duration(m_ptrMedia);
QString strTime = "";
ui.horizontalSlider->setMaximum(time);
ui.horizontalSlider->setMinimum(0);
transferTime(strTime, time);
ui.totalTimelabel->setText(strTime);
libvlc_media_release(m_ptrMedia);
m_ptrMedia = nullptr;
libvlc_media_list_player_set_media_list(m_ptrListMediaPlayer, m_ptrMediaList);
libvlc_media_list_player_set_media_player(m_ptrListMediaPlayer, m_ptrMediaPlayer);
if (m_bIsSingleCycle)
libvlc_media_list_player_set_playback_mode(m_ptrListMediaPlayer, libvlc_playback_mode_repeat);
else
libvlc_media_list_player_set_playback_mode(m_ptrListMediaPlayer, libvlc_playback_mode_loop);
libvlc_media_player_set_hwnd(m_ptrMediaPlayer, (void*)ui.playWidget->winId());
void MyMediaplayer::vlcEvents(const libvlc_event_t* ev, void* param)
Q_UNUSED(param);
switch (ev->type)
case libvlc_MediaPlayerTimeChanged:
//curTime = ev->u.media_player_time_changed.new_time;
auto tmpTime = ev->u.media_player_time_changed.new_time;
QString time = "";
m_ptrThis->transferTime(time, tmpTime);
m_ptrThis->ui.horizontalSlider->setValue(tmpTime);
m_ptrThis->ui.curTimeLabel->setText(time);
break;
case libvlc_MediaPlayerEndReached:
break;
case libvlc_MediaPlayerStopped:
//m_ptrThis->onPlayFinish();
break;
case libvlc_MediaPlayerPlaying:
break;
case libvlc_MediaPlayerPaused:
break;
case libvlc_MediaPlayerPositionChanged:
break;
case libvlc_MediaPlayerVout:
qDebug() << ev->u.media_player_vout.new_count;
break;
case libvlc_MediaPlayerLengthChanged:
int time = (ev->u.media_player_length_changed.new_length);
qDebug() << "qqqq" << time;
QString strtime = "";
m_ptrThis->transferTime(strtime, time);
m_ptrThis->ui.horizontalSlider->setMaximum(time);
m_ptrThis->ui.totalTimelabel->setText(strtime);
break;
default:
break;
MyMediaplayer::~MyMediaplayer()
freeVlc();
delete m_ptrFileDialog;
m_ptrFileDialog = nullptr;
以上是关于JavaSwing_基于VLC media player内核制作一个简单的视频播放器的主要内容,如果未能解决你的问题,请参考以下文章