环境介绍
Qt版本:5.15.2
VLC版本:3.0.20
编译器:MinGW 64
系统:Win11
效果
可以自己去增加功能和美化如:进度条,视频速度,显示视频时长等
下载VLC的SDK文件
VLC库官网下载地址:https://www.videolan.org/
所有VLC的SDK下载地址:https://ftp.heanet.ie/pub/videolan/vlc/
3.0.20的SDK下载地址:https://ftp.heanet.ie/pub/videolan/vlc/last/win64/
下载完成后解压
需要的文件
进入文件夹 ,需要的一个是SDK文件夹和运行所需要的.dll和plugins
SDK文件下是我们需要的头文件和lib库
创建工程
将sdk文件夹放到工程目录下
pro文件
新建一个工程,在xxx.pro加入路径,这里的路径代表当前工程目录下的路径
INCLUDEPATH += $$PWD\sdk\include
LIBS += $$PWD\sdk\lib\libvlc.lib \
$$PWD\sdk\lib\libvlccore.lib
将上面提到的文件和文件夹放入可执行文件.exe目录的下面
ui文件设置
mainwindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <vlc/vlc.h>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void OpenVideoFile();
public:
void MediaShowWindow(libvlc_media_player_t*player);
void MediaPlayerPlay();
private:
Ui::MainWindow *ui;
libvlc_instance_t*m_instence;
libvlc_media_player_t*m_vlcplayer = nullptr;
bool is_playing = false;
int m_currentVolume;
};
#endif // MAINWINDOW_H
mainwindow.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//初始化
ui->btn_play->setText("暂停");
ui->Slider_volume->setMaximum(100);
ui->Slider_volume->setValue(100);
m_instence = libvlc_new(0,nullptr);
if(m_instence == nullptr){
qDebug()<<"instence null";
}else{
qDebug()<<"ok...";
}
//选择文件
connect(ui->btn_open,&QPushButton::clicked,this,&MainWindow::OpenVideoFile);
//播放暂停
connect(ui->btn_play,&QPushButton::clicked,this,[=](){
if(m_vlcplayer == nullptr){
QMessageBox::warning(this,"无视频","请选择一个视频");
return;
}
if(is_playing){
libvlc_media_player_pause(m_vlcplayer);
ui->btn_play->setText("播放");
is_playing = false;
}else{
libvlc_media_player_play(m_vlcplayer);
ui->btn_play->setText("暂停");
is_playing = true;
}
});
//音量调节
connect(ui->Slider_volume,&QSlider::valueChanged,this,[=](int value){
if(m_vlcplayer != nullptr){
int volume = static_cast<int>(static_cast<float>(value) / ui->Slider_volume->maximum() * 100);
libvlc_audio_set_volume(m_vlcplayer, volume);
m_currentVolume = volume;
ui->statusbar->showMessage("音量为: " + QString::number(volume));
}
});
}
MainWindow::~MainWindow()
{
if(m_instence != nullptr){
libvlc_release(m_instence);
m_instence = nullptr;
}
delete ui;
}
void MainWindow::OpenVideoFile()
{
if(m_vlcplayer != nullptr){
libvlc_media_player_stop(m_vlcplayer);
libvlc_media_player_release(m_vlcplayer);
m_vlcplayer = nullptr;
}
//选择文件
QString filepath = QFileDialog::getOpenFileName(this,tr("open video"),tr("E:\\pic"),tr("Video Files(*.avi *.mp4 *.mkv *.mp3 *.wma)"));
if(filepath.isEmpty()){
qDebug()<< "path is null";
return;
}
filepath = QDir::toNativeSeparators(filepath);
libvlc_media_t*media = libvlc_media_new_path(m_instence,filepath.toUtf8().data());
libvlc_media_parse(media);
m_vlcplayer = libvlc_media_player_new_from_media(media);
libvlc_media_release(media);
//设置窗口句柄
MediaShowWindow(m_vlcplayer);
//播放
MediaPlayerPlay();
}
void MainWindow::MediaShowWindow(libvlc_media_player_t *player)
{
libvlc_media_player_set_hwnd(player,(void*)ui->widget_player->winId());
}
void MainWindow::MediaPlayerPlay()
{
if(m_vlcplayer == nullptr){
qDebug()<<"m_vlcplayer is null";
return;
}
libvlc_media_player_play(m_vlcplayer);
is_playing = true;
}