参考博主 1https://blog.csdn.net/liji_digital/article/details/83691035
参考博主2https://blog.csdn.net/qq_41071706/article/details/89855986
实现功能
- 通过选择按钮选择本地视频文件
- 点击播放按钮播放文本框中的文件(不支持其他类型文件的异常检测) 支持mp4 avi等等 如果不支持见参考博主2的内容
- 点击暂停按钮,暂停本视频播放,再次点击播放按钮,继续本视频的播放
- 进度条跟随视频进度移动
- 拖动进度条改变视频播放位置
- 视频必须播放之后才能拖动进度条改变播放进度
- 不支持快进慢放等操作
1.首先创建一个带ui的project
步骤省略
2.在ui文件中绘制如下模块
实现代码如下
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWidget>
#include <QtMultimediaWidgets/QVideoWidget>
#include <QtMultimedia/QtMultimedia>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QVideoWidget * m_pPlayerWidget;
QMediaPlayer * m_pPlayer;
public slots:
void onSetMediaFile();
void onStartPlayMedia();
void onChangedPlayerPosition(qint64 pos);
};
#endif // MAINWINDOW_H
// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtMultimediaWidgets/QVideoWidget>
#include <QtMultimedia/QtMultimedia>
#include <QFileDialog>
#include <QMessageBox>
#include <QException>
#include <QDebug>
#include "player-slider.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_pPlayer = new QMediaPlayer;
m_pPlayerWidget = new QVideoWidget;
QPalette qplte;
qplte.setColor(QPalette::Window, QColor(0,0,0));
m_pPlayerWidget->setPalette(qplte);
m_pPlayer->setVideoOutput(m_pPlayerWidget);
ui->verticalLayout->addWidget(m_pPlayerWidget);
m_pPlayerWidget->setAutoFillBackground(true);
QObject::connect(ui->pushButton_3,&QPushButton::clicked,this,&MainWindow::onSetMediaFile);
QObject::connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::onStartPlayMedia);
QObject::connect(ui->pushButton_2,&QPushButton::clicked,this->m_pPlayer,&QMediaPlayer::pause);
QObject::connect(m_pPlayer, &QMediaPlayer::positionChanged, ui->horizontalSlider,&QPlayerSlider::setValue);
QObject::connect(m_pPlayer, &QMediaPlayer::durationChanged, ui->horizontalSlider,&QPlayerSlider::setDurationPosition);
QObject::connect(ui->horizontalSlider,&QPlayerSlider::sigProgress,this,&MainWindow::onChangedPlayerPosition);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onSetMediaFile()
{
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
QStringList fileNames;
if(dialog.exec())
{
fileNames = dialog.selectedFiles();
}
if(!fileNames.empty())
{
ui->lineEdit->setText(fileNames[0]);
}
}
void MainWindow::onStartPlayMedia()
{
qDebug()<<ui->lineEdit->text()<<"\n"<<m_pPlayer->currentMedia().canonicalUrl().path();
QString filePath = ui->lineEdit->text();
if(filePath != nullptr && filePath != ""){
if(m_pPlayer->state() == QMediaPlayer::PausedState &&
ui->lineEdit->text() == m_pPlayer->currentMedia().canonicalUrl().path().right(m_pPlayer->currentMedia().canonicalUrl().path().length()-1))
{
m_pPlayer->play();
}
else {
m_pPlayer->setMedia(QUrl::fromLocalFile(filePath));
m_pPlayer->play();
}
}
else{
QMessageBox::critical(this,"警告","请选择要播放的视频文件!");
}
}
void MainWindow::onChangedPlayerPosition(qint64 pos)
{
QObject::disconnect(m_pPlayer, &QMediaPlayer::positionChanged, ui->horizontalSlider,&QPlayerSlider::setValue);
m_pPlayer->setPosition(pos);
QObject::connect(m_pPlayer, &QMediaPlayer::positionChanged, ui->horizontalSlider,&QPlayerSlider::setValue);
}
// playerslider.h 需要将ui的slider提升为QPlayerSlider类
#ifndef PLAYERSLIDER_H
#define PLAYERSLIDER_H
#include <QSlider>
class QPlayerSlider :public QSlider{
Q_OBJECT
public:
QPlayerSlider(QWidget* parent = 0);
public slots:
void setDurationPosition(qint64 max_len);
signals:
void sigProgress(qint64 pos);
protected:
void mouseReleaseEvent(QMouseEvent *e);
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
};
#endif // PLAYERSLIDER_H
// playerslider.cpp
#include "player-slider.h"
#include <QDebug>
QPlayerSlider::QPlayerSlider(QWidget * parent) : QSlider(parent)
{
}
void QPlayerSlider::setDurationPosition(qint64 max_len)
{
setRange(0,max_len);
}
void QPlayerSlider::mouseReleaseEvent(QMouseEvent *e)
{
qint64 pos = this->value();
emit sigProgress(pos);
QSlider::mouseReleaseEvent(e);
}
void QPlayerSlider::mousePressEvent(QMouseEvent *e)
{
QSlider::mousePressEvent(e);
}
void QPlayerSlider::mouseMoveEvent(QMouseEvent *e)
{
QSlider::mouseMoveEvent(e);
}
实现效果
文章来源:https://www.toymoban.com/news/detail-421427.html
全部代码见 https://download.csdn.net/download/hyperspace88466/72190342
仅供练习,如有问题请留言~文章来源地址https://www.toymoban.com/news/detail-421427.html
到了这里,关于QT5.9实现一个视频播放器播放 暂停进度条的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!