步骤:
1、QT编写音频播放器可以使用QMediaPlayer的类,画面可以使用QVideoWidget的类。这两个类在新建的工程中没有,需要手动添加。这两个类属于multimedia和multimediawidgets,所以需要在.pro文件中添加。如图:
2、ui设计
其中进度条属于QSlider类,QSlider的父类是QAbstractSlider类
QAbstractSlider的信号与槽可以达到我们使用需求
3、头文件定义
头文件可以一边写一边添加函数,缺啥补啥
4、cpp文件编程
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//创建播放对象
player = new QMediaPlayer(this);
videowidget = new QVideoWidget(this);
//视频显示组件
ui->verticalLayout->addWidget(videowidget);
player->setVideoOutput(videowidget);
videowidget->show();
//初始化
systemInit();
//视频连接进度条
connect(player, &QMediaPlayer::positionChanged, this, &MainWindow::positionchanged);
//进度条调节视频
connect(ui->horizontalSlider_process, &QAbstractSlider::valueChanged, this, &MainWindow::on_horizontalSlider_player_moved);
//总时长变化
connect(player, &QMediaPlayer::durationChanged, this, &MainWindow::updataDurationLable);
//音量实时显示
connect(player, &QMediaPlayer::volumeChanged, ui->horizontalSlider_voice, [=](){
ui->horizontalSlider_voice->setValue(player->volume());
ui->label_voice->setNum(ui->horizontalSlider_voice->value());
});
//音量调整
connect(ui->horizontalSlider_voice, &QAbstractSlider::valueChanged, this, [=](){
player->setVolume(ui->horizontalSlider_voice->value());
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_choosefile_clicked()
{
QString currentpath = QDir::homePath();//获取系统当前目录
QString openfiletitle = "请选择文件";//title
QString filefilter = "all(*.*);;MP4(*.mp4);;image(*.jpg);;file(*.txt)";//选择文件
QString filepath = QFileDialog::getOpenFileName(this, openfiletitle, currentpath, filefilter);
if(filepath.isEmpty())
{
QMessageBox::warning(this, "打开文件", "文件不能为空!!!!!");
return;
}
QFileInfo info(filepath);//获取文件信息
ui->lineEdit_filename->setText(info.fileName());
//设置视频播放文件
player->setMedia(QUrl::fromLocalFile(filepath));
player->play();//播放
}
void MainWindow::on_pushButton_restart_clicked()
{
player->stop();
player->play();
}
void MainWindow::on_pushButton_stop_clicked()
{
player->pause();
}
void MainWindow::on_pushButton_start_clicked()
{
player->play();
}
QString MainWindow::formatTime(qint64 time)
{
int seconds = (time / 1000) % 60;
int minutes = (time / 60000) % 60;
int hours = (time / 3600000) % 24;
return QString("%1:%2:%3")
.arg(hours, 2, 10, QLatin1Char('0'))
.arg(minutes, 2, 10, QLatin1Char('0'))
.arg(seconds, 2, 10, QLatin1Char('0'));
}
void MainWindow::systemInit()
{
//音量初始化
ui->horizontalSlider_voice->setRange(0, 100);
ui->horizontalSlider_voice->setValue(100);
ui->label_voice->setNum(100);
ui->horizontalSlider_process->setRange(0, static_cast<int>(player->duration()));
}
void MainWindow::positionchanged(qint64 position)
{
if(player->duration() > 0)
{
int progress = static_cast<int>(position);
ui->horizontalSlider_process->setValue(progress);
ui->label_totaltime->setText(formatTime(position));
//重播
if(position == player->duration())
{
player->stop();
player->play();
}
}
}
void MainWindow::on_horizontalSlider_player_moved(qint64 pos)
{
if(player->duration() > 0)
{
player->setPosition(pos);
}
}
void MainWindow::updataDurationLable()
{
//显示视频总时长
qint64 totaltime = player->duration();
ui->label_totaltime->setText(formatTime(totaltime));
//视频进度条初始化,duration为视频总长,单位毫秒
ui->horizontalSlider_process->setRange(0, totaltime);
}
5、总结
最好自己编写下,使用的方法和类熟悉下,不知道再查找资料,CV不利于培养开发思维,后面的方法可以全部用lamda表达式代替。还有很多可以增加的功能,自己摸索自己加,如播放列表,按键换成资源文件的图标等
6、补充
需要下载k_lite视频编解码工具,不然放不出来,已经放在压缩包中
7、成果展示
MainWindow 2024-01-11 17-19-06