1. 简述
一个简单的视频播放器,包含暂停、停止、音量、播放时间、拖进等功能,界面未经过美化,纯功能例子
2.简单分析
2.1.视频播放器的两个主体
- 播放器
QVideoWidget
- 播放对象
QMediaPlayer
2.2.播放对象的属性
- 视频时长
duration()
//return qint64 毫秒值- 当前播放的时长
position()
//return qint64 毫秒值- 视频当前音量
volume()
//return qint64 毫秒值- 是否静音
isMuted()
return bool
对应的有
- 没有
setPosition(qint64)
//可以用来跳转到某个时间点setVolume()
//设置音量setMuted(bool)
//设置是否静音
2.3 播放对象的状态
play()
播放或者从当前位置继续播放pause()
暂停stop()
停止结束播放
对应状态
- QMediaPlayer::PlayingState
- QMediaPlayer::PausedState
- QMediaPlayer::StoppedState
3.代码
源码下载 https://download.csdn.net/download/qq_44084616/15744804
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtMultimedia>
#include <QVideoWidget>
#include <QFileDialog>
#include <QTimer>
#include <QTime>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_start_clicked();
void on_finish_clicked();
void on_url_clicked();
void on_stateChanged(QMediaPlayer::State);
void OnDurationChanged(qint64);
void on_timeout();
void on_TimeSlider_sliderPressed();
void on_TimeSlider_sliderMoved(int position);
void on_TimeSlider_sliderReleased();
void on_pushButton_clicked();
void on_horizontalSlider_sliderMoved(int position);
void on_horizontalSlider_sliderReleased();
private:
Ui::MainWindow *ui;
QVideoWidget* videoWidget;
QMediaPlayer* mediaPlayer;
QString sUrl;
QTimer timer;
bool isMute;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
mediaPlayer = new QMediaPlayer; //播放媒体源
videoWidget = new QVideoWidget; //用来显示媒体对象制作的视频 (播放器)
mediaPlayer->setVideoOutput(videoWidget); //视频输出到媒体播放器
ui->gridLayout_2->addWidget(videoWidget);
videoWidget->setAutoFillBackground(true); //允许背景自动填充
QPalette qplte;
qplte.setColor(QPalette::Window, QColor(0,0,0));
videoWidget->setPalette(qplte); //设置面板颜色
ui->start->setEnabled(false); //初始禁用开始、停止按钮
ui->finish->setEnabled(false);
ui->TimeSlider->setEnabled(false);
isMute = false; //扬声器未静音
connect(mediaPlayer,SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(on_stateChanged(QMediaPlayer::State))); //播放状态改变 开始/继续:play() 暂停:pause() 结束:stop()
connect(mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(OnDurationChanged(qint64))); //视频持续时间,毫秒值 mediaPlayer->duration()可以直接获取
connect(&timer,&QTimer::timeout,this,&MainWindow::on_timeout); //定时器,用于获取视频当前播放时间,毫秒值
}
MainWindow::~MainWindow()
{
delete ui;
}
/*
* 1. QT += core gui multimedia multimediawidgets
* 2.
*
*/
void MainWindow::on_url_clicked() //选择要播放视频
{
QString Url = QFileDialog::getOpenFileName(this,"打开视频","*.mp4");
if(!Url.isEmpty()){
sUrl = Url;
ui->start->setEnabled(true);
ui->TimeSlider->setEnabled(false);
ui->TimeSlider->setValue(0);
}
}
void MainWindow::on_start_clicked() //开始按钮点击后设置相应的播放状态和需要的文字
{
if(ui->start->text() == "开始"){
mediaPlayer->setMedia(QUrl::fromLocalFile(sUrl)); //设置当前媒体源。
mediaPlayer->play();
/*
* mediaPlayer->position() 当前播放时间,毫秒值
* mediaPlayer->duration() 视频总时长, 毫秒值
* mediaPlayer->volume() 视频的播放音量 默认值100
* QDateTime::fromMSecsSinceEpoch(mediaPlayer->position()).toString("hh:mm:ss") 将毫秒值按指定格式转换
*/
ui->showTime->setText(QDateTime::fromMSecsSinceEpoch(mediaPlayer->position()).toString("hh:mm:ss")+"/"+QDateTime::fromMSecsSinceEpoch(mediaPlayer->duration()).toString("hh:mm:ss"));
ui->TimeSlider->setEnabled(true);
ui->horizontalSlider->setValue(mediaPlayer->volume());
}else if(ui->start->text() == "暂停"){
mediaPlayer->pause();
}else if(ui->start->text() == "继续"){
mediaPlayer->play();
}
}
void MainWindow::on_finish_clicked() //停止播放
{
mediaPlayer->stop();
ui->TimeSlider->setValue(0);
ui->start->setText("开始");
ui->finish->setEnabled(false);
}
void MainWindow::on_stateChanged(QMediaPlayer::State state) //播放状态发生变化
{
switch (state) {
case QMediaPlayer::StoppedState:
ui->start->setText("开始");
ui->finish->setEnabled(false);
timer.stop();
break;
case QMediaPlayer::PausedState:
mediaPlayer->pause();
ui->start->setText("继续");
timer.stop();
break;
case QMediaPlayer::PlayingState:
ui->start->setText("暂停");
ui->finish->setEnabled(true);
timer.start(10);
break;
}
}
void MainWindow::OnDurationChanged(qint64 range)
{
if(range > 0){
ui->TimeSlider->setEnabled(true);
ui->TimeSlider->setRange(0,range);
}
}
void MainWindow::on_timeout() //更新播放时间轴和时间戳
{
ui->showTime->setText(QTime::fromMSecsSinceStartOfDay(mediaPlayer->position()).toString("hh:mm:ss")+"/"+QTime::fromMSecsSinceStartOfDay(mediaPlayer->duration()).toString("hh:mm:ss"));
ui->TimeSlider->setValue(mediaPlayer->position());
}
void MainWindow::on_TimeSlider_sliderPressed() //时间轴: 按下鼠标左键
{
timer.stop();
}
void MainWindow::on_TimeSlider_sliderMoved(int position) // 时间轴:鼠标移动
{
mediaPlayer->pause();
mediaPlayer->setPosition(position); //跳转到position 时间点位置
}
void MainWindow::on_TimeSlider_sliderReleased() //时间轴: 鼠标离开
{
mediaPlayer->play();
timer.start(10);
}
void MainWindow::on_pushButton_clicked() //扬声器按钮,切换静音和非静音状态
{
if(!isMute){
isMute = !isMute;
ui->pushButton->setIcon(QIcon(":/img/mute.png"));
mediaPlayer->setMuted(isMute);
}else{
isMute = !isMute;
ui->pushButton->setIcon(QIcon(":/img/loudSpeaker.png"));
mediaPlayer->setMuted(isMute);
mediaPlayer->setVolume(ui->horizontalSlider->value()); //设置音量
}
}
void MainWindow::on_horizontalSlider_sliderMoved(int position) //音量轴: 鼠标移动
{
mediaPlayer->setVolume(position);
if(position == 0){
isMute = !isMute;
mediaPlayer->setMuted(isMute);
ui->pushButton->setIcon(QIcon(":/img/mute.png"));
}else{
isMute = !isMute;
mediaPlayer->setMuted(isMute);
ui->pushButton->setIcon(QIcon(":/img/loudSpeaker.png"));
}
}
void MainWindow::on_horizontalSlider_sliderReleased() //音量轴: 鼠标离开
{
mediaPlayer->setVolume(ui->horizontalSlider->value());
}
4.截图
4.1初始界面
4.2 暂停
4.3 静音
4.4 拖进
不好截图,自己试试吧
5. 注意
如果加载视频报错,如:
DirectShowPlayerService::doRender: Unresolved error code 0x80040266 ()
出现这个报错的原因:
- 文件路径不对或者文件名是中文的
- 未安装LAVFilters解码器
下载链接:
https://github.com/Nevcairiel/LAVFilters/releases