1. 程式人生 > >qt實現視訊播放器

qt實現視訊播放器

本篇部落格介紹如何利用qMediaPlayer和qvideowidget實現視訊檔案(avi,mp4....)的播放,並且提供進度顯示,還可以通過拖動進度條來變換播放位置。相關程式碼可以在我的資源裡下載"基於qt的視訊播放器"

pro檔案:

#-------------------------------------------------
#
# Project created by QtCreator 2018-11-02T20:03:58
#
#-------------------------------------------------

QT       += core gui multimedia multimediawidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = VideoPlayer
TEMPLATE = app


SOURCES += main.cpp \
    videoplayer.cpp \
    playerslider.cpp

HEADERS  += \
    videoplayer.h \
    playerslider.h

FORMS    += \
    videoplayer.ui

main.cpp

#include "videoplayer.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    VideoPlayer w;
    w.show();

    return a.exec();
}

VideoPlayer是我定義的播放器窗體類。它包括一個QMediaplayer,一個QVideoWidget,一個播放按鈕,一個停止按鈕,一個載入按鈕,和進度條PlayerSlider。VideoPlayer的ui檔案外觀如圖:

這裡面的PlayerSlider繼承自QSlider,行為定義如下:

#ifndef PLAYERSLIDER_H
#define PLAYERSLIDER_H

#include <QSlider>
#include <QMouseEvent>

class PlayerSlider : public QSlider
{
    Q_OBJECT
public:
    PlayerSlider(QWidget * parent = 0);
    void        setProgress(qint64);
signals:
    void        sigProgress(qint64);
private:
    bool        m_bPressed;
protected:
    void        mousePressEvent(QMouseEvent *);
    void        mouseMoveEvent(QMouseEvent *);
    void        mouseReleaseEvent(QMouseEvent *);
};

#endif // PLAYERSLIDER_H
#include "playerslider.h"

PlayerSlider::PlayerSlider(QWidget * parent) : QSlider(parent)
{
    m_bPressed = false;
}

void PlayerSlider::mousePressEvent(QMouseEvent *e)
{
    m_bPressed = true;
    QSlider::mousePressEvent(e);//必須有這句,否則手動不能移動滑塊
}

void PlayerSlider::mouseMoveEvent(QMouseEvent *e)
{
    QSlider::mouseMoveEvent(e);//必須有這句,否則手動不能移動滑塊
}

void PlayerSlider::mouseReleaseEvent(QMouseEvent *e)
{
    m_bPressed = false;
    qint64 i64Pos = value();
    emit sigProgress(i64Pos);

    QSlider::mouseReleaseEvent(e);//必須有這句,否則手動不能移動滑塊
}

void PlayerSlider::setProgress(qint64 i64Progress)
{
    if(!m_bPressed)
        setValue(i64Progress);
}

這個類的作用有3個:1)接收QMediaPlaer發來的進度資訊,更新進度條;2)當用戶操作進度條時,不再讓進度條響應QMediaPlaer發來的進度資訊;3)當用戶完成對進度條的拖動後,向QMediaPlaer傳送播放位置更新資訊。

另一個類是VideoPlayer,行為定義如下:

#ifndef VIDEOPLAYER_H
#define VIDEOPLAYER_H

#include <QWidget>
#include <QtMultimedia>
#include <QVideoWidget>

namespace Ui {
class VideoPlayer;
}

class VideoPlayer : public QWidget
{
    Q_OBJECT

public:
    explicit VideoPlayer(QWidget *parent = 0);
    ~VideoPlayer();

    bool            m_bReLoad;
public slots:
    void            OnSetMediaFile(void);
    void            OnSlider(qint64);
    void            OnDurationChanged(qint64);
    void            OnStateChanged(QMediaPlayer::State);
private:
    QVideoWidget    *       m_pPlayerWidget;
    QMediaPlayer    *       m_pPlayer;
    Ui::VideoPlayer *ui;
};

#endif // VIDEOPLAYER_H
#include "videoplayer.h"
#include "ui_videoplayer.h"
#include <QFileDialog>

VideoPlayer::VideoPlayer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VideoPlayer)
{
    ui->setupUi(this);

    m_pPlayer = new QMediaPlayer;
    m_pPlayerWidget = new QVideoWidget;
    m_pPlayer->setVideoOutput(m_pPlayerWidget);
    ui->verticalLayout->addWidget(m_pPlayerWidget);

    m_pPlayerWidget->setAutoFillBackground(true);
    QPalette qplte;
    qplte.setColor(QPalette::Window, QColor(0,0,0));
    m_pPlayerWidget->setPalette(qplte);
    //載入
    connect(ui->BtnLoad, SIGNAL(clicked()), this, SLOT(OnSetMediaFile()));
    //播放
    connect(ui->BtnPlay, SIGNAL(clicked()), m_pPlayer, SLOT(play()));
    //停止
    connect(ui->BtnStop, SIGNAL(clicked()), m_pPlayer, SLOT(stop()));

    connect(m_pPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(OnStateChanged(QMediaPlayer::State)));

    ui->BtnStop->setEnabled(false);
    //設定滑塊行為
    m_bReLoad = true;
    ui->slider->setEnabled(false);
    connect(m_pPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(OnSlider(qint64)));
    connect(m_pPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(OnDurationChanged(qint64)));
    connect(ui->slider, SIGNAL(sigProgress(qint64)), m_pPlayer, SLOT(setPosition(qint64)));
}

VideoPlayer::~VideoPlayer()
{
    delete m_pPlayer;
    delete m_pPlayerWidget;
    delete ui;
}

void VideoPlayer::OnSetMediaFile(void)
{
    QFileDialog dialog(this);
    dialog.setFileMode(QFileDialog::AnyFile);
    QStringList fileNames;
    if (dialog.exec())
        fileNames = dialog.selectedFiles();

    if(!fileNames.empty())
    {
        m_pPlayer->setMedia(QUrl::fromLocalFile(fileNames[0]));
        m_bReLoad = true;
        ui->slider->setValue(0);
    }
}

void VideoPlayer::OnSlider(qint64 i64Pos)
{
    ui->slider->setProgress(i64Pos);
}

void VideoPlayer::OnDurationChanged(qint64 i64Duration)
{
    if(i64Duration > 0 && m_bReLoad)
    {
        ui->slider->setRange(0, i64Duration);
        m_bReLoad = false;
    }
}


void VideoPlayer::OnStateChanged(QMediaPlayer::State enumState)
{
    if(QMediaPlayer::StoppedState == enumState)
    {
        ui->BtnPlay->setEnabled(true);
        ui->BtnStop->setEnabled(false);
        ui->slider->setEnabled(false);
    }
    else if(QMediaPlayer::PlayingState == enumState)
    {
        ui->BtnPlay->setEnabled(false);
        ui->BtnStop->setEnabled(true);
        ui->slider->setEnabled(true);
    }
}

之所以在他的建構函式裡設定QPalette,是因為假如不這樣,在程式啟動時,m_pPlayer不是黑色,而是與背景同樣的顏色,顯得不好看。

顯示效果: