qt 简单视频播放器

发布时间:2024年01月05日
#include "videowidget.h"
#include <QGridLayout>
#include <QDebug>
#include "../TC_Common/global.h"

VideoWidget::VideoWidget(QWidget *parent) : QWidget(parent)
{
    setWindowFlag(Qt::FramelessWindowHint);

    setAutoFillBackground(true);
    QRadialGradient radial(HDMI_WIDTH/2,HDMI_HEIGHT/2,HDMI_WIDTH/2,HDMI_WIDTH/2,HDMI_HEIGHT/2);
    radial.setColorAt(0,Qt::cyan);
    radial.setColorAt(1,Qt::black);

    QPalette qplte;
    qplte.setBrush(QPalette::Window, QBrush(radial));
    setPalette(qplte);

     m_viewplayer = new QMediaPlayer;

     m_videoWidget = new QVideoWidget;

     /*网络读取*/
     m_viewplayer->setMuted(true);
     m_viewplayer->setVideoOutput(m_videoWidget);

     //必须要有布局,没有布局视频后台运行,但不显示画面
     QGridLayout*grid = new QGridLayout(this);
     grid->setSpacing(0);
     grid->setMargin(0);
     grid->addWidget(m_videoWidget);
     setLayout(grid);

     connect(m_viewplayer, QOverload<QMediaPlayer::State>::of(&QMediaPlayer::stateChanged),
             [=](QMediaPlayer::State newState){
         qDebug()<<DEBUGINFO<<newState;
     });

     connect(m_viewplayer, QOverload<QMediaPlayer::Error>::of(&QMediaPlayer::error),
             [=](QMediaPlayer::Error error){
         QString errorinfo;
         errorinfo.clear();
         switch (static_cast<int>(error))
         {
         case QMediaPlayer::Error::ResourceError:
             errorinfo = "媒体源错误,";
             break;

         case QMediaPlayer::Error::FormatError:
             errorinfo = "格式错误,播放可能会丢失音频或视频";
             break;

         case QMediaPlayer::Error::NetworkError:
             errorinfo = "网络错误";
             break;

         case QMediaPlayer::Error::AccessDeniedError:
             errorinfo = "访问权限错误";
             break;

         case QMediaPlayer::Error::ServiceMissingError:
             errorinfo = "服务丢失错误";
             break;

         default: break;
         }

         emit signal_videoError(QString("错误信息:"+QTime::currentTime().toString("hh:mm:ss")+": "+errorinfo));
     });

     connect(m_viewplayer, QOverload<QMediaPlayer::MediaStatus>::of(&QMediaPlayer::mediaStatusChanged),
             [=](QMediaPlayer::MediaStatus status){
         QString statusinfo;
         statusinfo.clear();
         switch(status)
         {
         case QMediaPlayer::UnknownMediaStatus:
             statusinfo = "媒体未知状况!";
             break;
         case QMediaPlayer::NoMedia:
             statusinfo = "没有媒体文件!";
             break;
         case QMediaPlayer::BufferingMedia:
             statusinfo = "正在缓冲媒体文件!";
             break;
         case QMediaPlayer::BufferedMedia:
             statusinfo = "媒体文件缓冲完成!";
             break;
         case QMediaPlayer::LoadingMedia:
             statusinfo = "正在加载媒体!";
             break;
         case QMediaPlayer::StalledMedia:
             statusinfo = "播放停滞!";
             break;
         case QMediaPlayer::EndOfMedia:
             statusinfo = "播放结束!";
             break;
         case QMediaPlayer::LoadedMedia:
             statusinfo = "媒体加载完成!";
             break;
         case QMediaPlayer::InvalidMedia:
             statusinfo = "不可用的媒体文件!";
             break;
         default: break;
         }

         emit signal_videoInfo(QString("播放状态:"+QTime::currentTime().toString("hh:mm:ss")+": "+statusinfo));
     });
}

VideoWidget::~VideoWidget()
{
    m_viewplayer->deleteLater();
    m_videoWidget->deleteLater();
}

void VideoWidget::play()
{
    m_viewplayer->setMedia(QMediaContent(QUrl(m_url)));
    m_viewplayer->play();
}

void VideoWidget::replay()
{
    m_viewplayer->stop();
    m_viewplayer->setMedia(QMediaContent(QUrl(m_url)));
    m_viewplayer->play();
}

void VideoWidget::stop()
{
    m_viewplayer->stop();
}

void VideoWidget::setUrl(QUrl url)
{
    m_url = url;
}
#ifndef VIDEOWIDGET_H
#define VIDEOWIDGET_H

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

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

    ~VideoWidget();

    void play();

    void replay();

    void stop();

    void setUrl(QUrl url);
signals:
    void signal_videoInfo(QString);

    void signal_videoError(QString);
public slots:

private:
    QMediaPlayer *m_viewplayer;

    QVideoWidget *m_videoWidget;

    QUrl m_url;
};

#endif // VIDEOWIDGET_H
文章来源:https://blog.csdn.net/weixin_41349971/article/details/135416011
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。