QT-Qt中MediaPlay的使用

相关资料:

https://blog.csdn.net/itas109/article/details/83829396

https://blog.csdn.net/birenxiaofeigg/article/details/104129207

https://www.cnblogs.com/zhangyi-studio/p/8596837.html

PS:

1.工程文件.pro需要引入 multimedia multimediawidgets 二个库。

2.引入#include <QMediaPlayer>   #include <QVideoWidget>   #include <QMediaPlaylist>三个文件。

3.播放无图像,并报错 “DirectShowPlayerService::doRender: Unresolved error code 80040266”,原因:Qt 中的多媒体播放,底层是使用DirectShowPlayerService,所以安装一个DirectShow解码器,例如LAV Filters,就可以解决运行出错问题。

4.安装LAV Filters后,出现花屏,卡屏问题,卸载之前安装的”LAV Filters“,安装”K-Lite Codec Pack“,本博主推荐使用”K-Lite Codec Pack“。

LAV Filters下载:https://github.com/Nevcairiel/LAVFilters/releases

K-Lite Codec Pack下载:https://pc.qq.com/detail/8/detail_528.html

.pro

 1 QT       += core gui multimedia multimediawidgets
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 #CONFIG += console
 7 
 8 # The following define makes your compiler emit warnings if you use
 9 # any Qt feature that has been marked deprecated (the exact warnings
10 # depend on your compiler). Please consult the documentation of the
11 # deprecated API in order to know how to port your code away from it.
12 DEFINES += QT_DEPRECATED_WARNINGS
13 
14 # You can also make your code fail to compile if it uses deprecated APIs.
15 # In order to do so, uncomment the following line.
16 # You can also select to disable deprecated APIs only up to a certain version of Qt.
17 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
18 
19 SOURCES += 
20     main.cpp 
21     mainwindow.cpp 
22     showMediaPlay.cpp
23 
24 HEADERS += 
25     mainwindow.h 
26     showMediaPlay.h
27 
28 FORMS += 
29     mainwindow.ui
30 
31 # Default rules for deployment.
32 qnx: target.path = /tmp/$${TARGET}/bin
33 else: unix:!android: target.path = /opt/$${TARGET}/bin
34 !isEmpty(target.path): INSTALLS += target
35 
36 RESOURCES += 
37     rec.qrc
View Code

main.cpp

 1 #include <QApplication>
 2 
 3 #include "showMediaPlay.h"
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     showMediaPlay *oShowMediaPlay = new showMediaPlay;
 9     oShowMediaPlay->exec();
10     return a.exec();
11 }
View Code

showMediaPlay.h

 1 #ifndef SHOWMEDIAPLAY_H
 2 #define SHOWMEDIAPLAY_H
 3 
 4 #include <QDialog>
 5 #include <QMediaPlayer>
 6 #include <QVideoWidget>
 7 #include <QMediaPlaylist>
 8 
 9 #include<QDebug>
10 #include<QFileDialog>
11 #include<QVBoxLayout>
12 #include<QFile>
13 #include<QMessageBox>
14 #include<QPushButton>
15 
16 class showMediaPlay : public QDialog
17 {
18     Q_OBJECT
19 public:
20     showMediaPlay(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
21     ~showMediaPlay();
22 private:
23     QVBoxLayout *layout = new QVBoxLayout;
24     QPushButton *play = new QPushButton;
25     QPushButton *stop = new QPushButton;
26     QPushButton *paused = new QPushButton;
27     QPushButton *duration = new QPushButton;
28 
29     QMediaPlayer* player = new QMediaPlayer;
30     QVideoWidget* videoWidget = new QVideoWidget;
31     QMediaPlaylist* playlist=new QMediaPlaylist;
32 private:
33     int m_nCount;
34 private slots:
35     void playVideo();
36     void stopVideo();
37     void pausedVideo();
38     void showduration();
39     void getDuration(qint64 playtime);
40 };
41 
42 #endif // SHOWMEDIAPLAY_H
View Code

showMediaPlay.cpp

 1 #include "showMediaPlay.h"
 2 
 3 showMediaPlay::showMediaPlay(QWidget *parent, Qt::WindowFlags f)
 4     :QDialog(parent, f)
 5 {
 6     this->resize(400, 300);
 7     m_nCount = 0;
 8     layout = new QVBoxLayout;
 9     play = new QPushButton;
10     play->setText(QStringLiteral("播放"));
11     stop = new QPushButton;
12     stop->setText(QStringLiteral("停止"));
13     paused = new QPushButton;
14     paused->setText(QStringLiteral("暂停"));
15     duration = new QPushButton;
16     duration->setText(QStringLiteral("持续时间"));
17     player = new QMediaPlayer;
18     videoWidget = new QVideoWidget(this);
19     videoWidget->setAspectRatioMode(Qt::IgnoreAspectRatio);
20     playlist = new QMediaPlaylist;
21     layout->addWidget(play);
22     layout->addWidget(stop);
23     layout->addWidget(paused);
24     layout->addWidget(duration);
25     layout->addWidget(videoWidget);
26     layout->setMargin(0);
27     this->setLayout(layout);
28     playlist->clear();
29     playlist->addMedia(QUrl::fromLocalFile("E:\AA.mp4"));
30     player->setPlaylist(playlist);
31     QFile file("E:\AA.mp4");
32     if(!file.open(QIODevice::ReadOnly))
33     qDebug() << "Could not open file";
34     player->setVideoOutput(videoWidget);
35     player->setPlaylist(playlist);
36     player->play();
37     connect(play, &QPushButton::clicked, this, &showMediaPlay::playVideo);
38     connect(stop, &QPushButton::clicked, this, &showMediaPlay::stopVideo);
39     connect(paused, &QPushButton::clicked, this, &showMediaPlay::pausedVideo);
40     connect(duration, &QPushButton::clicked, this, &showMediaPlay::showduration);
41     connect(player, &QMediaPlayer::durationChanged, this, &showMediaPlay::getDuration);
42 }
43 
44 showMediaPlay::~showMediaPlay()
45 {
46 
47 }
48 
49 void showMediaPlay::playVideo()
50 {
51     player->play();
52 }
53 
54 void showMediaPlay::stopVideo()
55 {
56     player->stop();
57 }
58 
59 void showMediaPlay::pausedVideo()
60 {
61     player->pause();
62 }
63 
64 void showMediaPlay::showduration()
65 {
66     setWindowTitle(QString("duration:%1  position:%2 ").arg(player->duration()).arg(player->position()));
67 }
68 
69 void showMediaPlay::getDuration(qint64 playtime)
70 {
71     m_nCount = m_nCount + 1;
72     setWindowTitle(QString("int:%1  duration:%2  position:%3 ").arg(m_nCount).arg(playtime).arg(player->position()));
73     if (0 == playtime)
74     {
75         accept();
76     }
77 }
View Code

  

原文地址:https://www.cnblogs.com/FKdelphi/p/13404404.html