Qt媒体播放器实现

本文介绍Qt媒体播放器实现。

Qt应用程序有时会涉及到视频文件的播放,Qt提供了QVideoWidget类实现视频和音频的播放,本文基于QVideoWidget类实现一个简单的媒体播放器。

1.自定义类(基于QVideoWidget类)

由于Qt并未提供专门的控件实现视频的显示,这里需要基于QVideoWidget类自定义一个类,再将拖放的QWidget类提升为自定义的类。这里将其命名为“VideoWidget”类。在使用QVideoWidget类时需在“.pro”文件中添加“multimedia”,“multimediawidgets”,参考代码如下:

QT       += core gui \
            multimedia \
            multimediawidgets

1)头文件

参考头文件如下:

#ifndef VIDEOWIDGET_H
#define VIDEOWIDGET_H

#include <QWidget>
#include <QVideoWidget>
#include <QMediaPlayer>


class VideoWidget : public QVideoWidget
{
    Q_OBJECT
public:
    explicit VideoWidget(QWidget *parent = nullptr);

    ~VideoWidget() override;

public:
    void setMediaPlayer(QMediaPlayer *player);

protected:
    void mousePressEvent(QMouseEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;

signals:

private:
    QMediaPlayer *mediaPlayer;

public slots:
};

#endif // VIDEOWIDGET_H

2)源文件

参考源文件如下:

#include "videowidget.h"
#include <QEvent>
#include <QKeyEvent>
#include <QMouseEvent>


VideoWidget::VideoWidget(QWidget *parent) : QVideoWidget(parent)
{

}

VideoWidget::~VideoWidget()
{

}

void VideoWidget::setMediaPlayer(QMediaPlayer *player)
{
    mediaPlayer = player;
}

void VideoWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        if (mediaPlayer->state() == QMediaPlayer::PlayingState) {
            mediaPlayer->pause();
        } else {
            mediaPlayer->play();
        }
    }

    QVideoWidget::mousePressEvent(event);
}

void VideoWidget::keyPressEvent(QKeyEvent *event)
{
    if ((event->key() == Qt::Key_Escape) && isFullScreen()) {
        setFullScreen(false);
        event->accept();
    }

    QVideoWidget::keyPressEvent(event);
}

2.界面设计

界面设计如下图:

其中,

视频显示控件“videoWidget”是拖放了一个QWidget控件,然后将此控件提升为前面自定义的类“VideoWidget”。

3.功能实现

1)头文件

参考头文件如下:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMediaPlayer>
#include <QTimer>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget() override;

public slots:
    void play(void);
    void stop(void);

private:
    Ui::Widget *ui;

private:
     QMediaPlayer *mediaPlayer;
     int m_duration;

private slots:
    void mediaStateChanged(QMediaPlayer::State state);
    void positionChanged(qint64 position);
    void durationChanged(qint64 duration);
    void setPosition(int position);
    void setFullScreen(void);
    void setVolume(int volume);
};
#endif // WIDGET_H

2)源文件

参考源文件如下:

#include "widget.h"
#include "ui_widget.h"
#include <QStyle>
#include <QMouseEvent>
#include <QTime>

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

    m_duration = 0;

    mediaPlayer = new QMediaPlayer(this, QMediaPlayer::VideoSurface);

    ui->btnPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));

    connect(ui->btnPlay, &QAbstractButton::clicked, this, &Widget::play);
    connect(ui->btnFullScreen, &QAbstractButton::clicked, this, &Widget::setFullScreen);
    connect(ui->sldPosition, &QAbstractSlider::sliderMoved, this, &Widget::setPosition);
    connect(ui->sldVolume, &QAbstractSlider::valueChanged, this, &Widget::setVolume);

    mediaPlayer->setVideoOutput(ui->videoWidget);
    ui->videoWidget->setMediaPlayer(mediaPlayer);
    connect(mediaPlayer, &QMediaPlayer::stateChanged, this, &Widget::mediaStateChanged);
    connect(mediaPlayer, &QMediaPlayer::positionChanged, this, &Widget::positionChanged);
    connect(mediaPlayer, &QMediaPlayer::durationChanged, this, &Widget::durationChanged);

    QString curPath = QCoreApplication::applicationDirPath();
    mediaPlayer->setMedia(QUrl::fromLocalFile(curPath + "/" + "test.mp4"));

    ui->sldVolume->setValue(50);

    mediaPlayer->setPlaybackRate(1.0);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::play()
{
    switch (mediaPlayer->state()) {
        case (QMediaPlayer::PlayingState): {
            mediaPlayer->pause();
            break;
        }
        default: {
            mediaPlayer->play();
            break;
        }
    }
}

void Widget::stop()
{
    mediaPlayer->stop();
}

void Widget::mediaStateChanged(QMediaPlayer::State state)
{
    switch(state) {
        case (QMediaPlayer::PlayingState): {
            ui->btnPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
            break;
        }
        case (QMediaPlayer::StoppedState): {
            ui->sldPosition->setValue(0);
            ui->btnPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
            break;
        }
        default: {
            ui->btnPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
            break;
        }
    }
}

void Widget::positionChanged(qint64 position)
{
    int temp = 0;

    temp = static_cast<int>(position);
    ui->sldPosition->setValue(temp);

    QString strTime;
    QTime currentTime((temp / 1000 / 3600) % 60, (temp / 1000 / 60) % 60,
                       temp / 1000 % 60, temp % 1000);
    QTime totalTime((m_duration / 1000 / 3600) % 60, (m_duration / 1000 / 60) % 60,
                     m_duration / 1000 % 60, m_duration % 1000);
    QString format = "mm:ss";
    if (m_duration / 1000 > 3600) {
        format = "hh:mm:ss";
    }
    strTime = currentTime.toString(format) + " / " + totalTime.toString(format);
    ui->lbTime->setText(strTime);
}

void Widget::durationChanged(qint64 duration)
{
    m_duration = static_cast<int>(duration);
    ui->sldPosition->setRange(0, m_duration);
}

void Widget::setPosition(int position)
{
    mediaPlayer->setPosition(position);
}

void Widget::setFullScreen()
{
    ui->videoWidget->setFullScreen(true);
}

void Widget::setVolume(int volume)
{
    mediaPlayer->setVolume(volume);
}

其中,

构造函数中媒体播放器绑定了输出控件,输出控件指定了媒体播放器,其他的功能块比较简单,这里不再赘述。

4.效果

运行效果如下:

总结,本文介绍了Qt媒体播放器实现。

上一篇:【前端】Bootstrap:响应式布局与工具类


下一篇:xss-labs靶场第七关测试报告