參考博主 1https://blog.csdn.net/liji_digital/article/details/83691035
參考博主2https://blog.csdn.net/qq_41071706/article/details/89855986
實(shí)現(xiàn)功能
- 通過選擇按鈕選擇本地視頻文件
- 點(diǎn)擊播放按鈕播放文本框中的文件(不支持其他類型文件的異常檢測(cè)) 支持mp4 avi等等 如果不支持見參考博主2的內(nèi)容
- 點(diǎn)擊暫停按鈕,暫停本視頻播放,再次點(diǎn)擊播放按鈕,繼續(xù)本視頻的播放
- 進(jìn)度條跟隨視頻進(jìn)度移動(dòng)
- 拖動(dòng)進(jìn)度條改變視頻播放位置
- 視頻必須播放之后才能拖動(dòng)進(jìn)度條改變播放進(jìn)度
- 不支持快進(jìn)慢放等操作
1.首先創(chuàng)建一個(gè)帶ui的project
步驟省略
2.在ui文件中繪制如下模塊
實(shí)現(xiàn)代碼如下
// 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,"警告","請(qǐng)選擇要播放的視頻文件!");
}
}
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);
}
實(shí)現(xiàn)效果文章來源:http://www.zghlxwxcb.cn/news/detail-421427.html
全部代碼見 https://download.csdn.net/download/hyperspace88466/72190342
僅供練習(xí),如有問題請(qǐng)留言~文章來源地址http://www.zghlxwxcb.cn/news/detail-421427.html
到了這里,關(guān)于QT5.9實(shí)現(xiàn)一個(gè)視頻播放器播放 暫停進(jìn)度條的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!