一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

QMediaPlayer开发视频播放器

Q:我们为何不使用QMediaPlayer?

A:QMediaPlayer支持的编解码库太少;QMediaPlayer在windows中解码调用的是
DirectShow,在Linux中调用的是GStreamer;相对Windows而言GStreamer扩展编解
码库比较方便,但是windows中的DirectShow太老了,Demuxer Decoder都比较麻烦
较麻烦;
QtMultimediaDemo 这个例子为老师编写的基于QMediaPlayer的播放器,它可以播放
MPEG­4编码方式的视频

使用QMediaPlayer搭建最简单的播放器
step1:pro文件添加内容

QT += core gui multimedia multimediawidgets

step2:框架

step3:测试代码

 1 #include "widget.h"
 2 
 3 #include <QApplication>
 4 #include <QMediaPlayer>
 5 #include <QVideoWidget>
 6 #include <QDebug>
 7 
 8 int main(int argc, char *argv[])
 9 {
10     QApplication a(argc, argv);
11 
12     // 创建一个MediaPlayer
13     QMediaPlayer *player = new QMediaPlayer;
14 
15     // 创建一个播放器窗口(Widget),用于显示MediaPlayer
16     QVideoWidget *vw = new QVideoWidget;
17 
18     // 将player绑定到显示的窗口上
19     player->setVideoOutput(vw);
20 
21     // 打开播放器的位置
22     player->setSource(QUrl::fromLocalFile("G:/video_test/video-h265.mkv"));
23     vw->setGeometry(100,100,640,480);
24     vw->show();
25     //    Widget w;
26     //    w.show();
27     return a.exec();
28 }

不支持格式

如何添加不是“非基本图形控件”的派生类

需求: 需要在界面上直接拖一个 QVideoWidget,由于基本控件中没有QVideoWidget,所以需要想想办法

新建提升的类

 因为QVideoWidget本身就继承于QWidget,所以这里选择QWidget作为基类

控件进行提升

成功播放视频

widget.h

 1 #ifndef WIDGET_H
 2 #define WIDGET_H
 3 
 4 #include <QWidget>
 5 #include <QMediaPlayer>
 6 #include <QVideoWidget>
 7 #include <QMenu>
 8 #include <QFileDialog>
 9 #include <QFile>
10 #include <QDir>
11 
12 QT_BEGIN_NAMESPACE
13 namespace Ui { class Widget; }
14 QT_END_NAMESPACE
15 
16 class Widget : public QWidget
17 {
18     Q_OBJECT
19 
20 public:
21     Widget(QWidget *parent = nullptr);
22     ~Widget();
23 
24 protected:
25     virtual void contextMenuEvent(QContextMenuEvent *event);
26 
27 private:
28     void createRightPopActions();
29 
30 private slots:
31     void openLocalVideoSlot();
32     void openUrlVideoSlot();
33 
34 private:
35     Ui::Widget *ui;
36     QMenu *popMenu;               //右键弹出式菜单
37     QAction *openLocalAction;     //打开本地文件
38     QAction *openUrlAction;       //打开网络文件
39     QAction *fullScreenAction;    //全屏显示
40     QAction *normalScreenAction;  //普通显示
41     QAction *quitAction;          //退出
42 
43     QMediaPlayer *player;
44 
45 
46     //QVideoWidget* vw;    //不需要了,界面上已经拖了一个QVideoWidget进去了
47 };
48 #endif // WIDGET_H

widget.cpp

 1 #include "widget.h"
 2 #include "ui_widget.h"
 3 
 4 Widget::Widget(QWidget *parent) :
 5     QWidget(parent),
 6     ui(new Ui::Widget),
 7     player(new QMediaPlayer) //创建一个MediaPlayer
 8 {
 9     ui->setupUi(this);
10 
11     createRightPopActions();
12 
13     player->setVideoOutput(ui->av_widget); //把player绑定到显示窗口上
14 }
15 
16 /* 右键弹出 Menu & Action */
17 void Widget::createRightPopActions()
18 {
19     popMenu = new QMenu(this);
20     popMenu->setStyleSheet("background-color: rgb(100, 100, 100);");
21 
22     openLocalAction = new QAction(this);
23     openLocalAction->setText(QString("打开本地文件"));
24 
25     openUrlAction = new QAction(this);
26     openUrlAction->setText(QString("打开本地文件"));
27 
28     fullScreenAction = new QAction(this);
29     fullScreenAction->setText(QString("全屏"));
30 
31     normalScreenAction = new QAction(this);
32     normalScreenAction->setText(QString("普通"));
33 
34 
35     quitAction = new QAction(this);
36     quitAction->setText(QString("退出"));
37 
38     connect(openLocalAction, SIGNAL(triggered(bool)), this, SLOT(openLocalVideoSlot()));
39     connect(openUrlAction, SIGNAL(triggered(bool)), this, SLOT(openUrlVideoSlot()));
40     connect(fullScreenAction, &QAction::triggered, this, &Widget::showFullScreen);
41     connect(normalScreenAction, &QAction::triggered, this, &Widget::showNormal);
42     connect(quitAction, &QAction::triggered, this, &Widget::close);
43 }
44 
45 void Widget::openLocalVideoSlot()
46 {
47     QString file = QFileDialog::getOpenFileName(this, tr("Open file"),
48                                                 QDir::homePath(),
49                                                 tr("Multimedia files(*)"));
50 
51     if (file.isEmpty())
52         return;
53 
54     player->setSource(QUrl::fromLocalFile(file)); //设置需要打开的媒体文件
55 
56     player->play();
57 }
58 
59 void Widget::openUrlVideoSlot()
60 {
61 
62 }
63 
64 
65 
66 /*右键菜单接口*/
67 void Widget::contextMenuEvent(QContextMenuEvent *event)
68 {
69     Q_UNUSED(event);         //未使用不警告
70     popMenu->clear();
71 
72 
73     popMenu->addAction(openLocalAction);
74     popMenu->addAction(openUrlAction);
75     popMenu->addAction(fullScreenAction);
76     popMenu->addAction(normalScreenAction);
77     popMenu->addAction(quitAction);
78 
79     popMenu->exec(QCursor::pos());//QAction *exec(const QPoint &pos, QAction *at = Q_NULLPTR);
80 }
81 
82 
83 
84 Widget::~Widget()
85 {
86     delete ui;
87 }

无边框的视屏

 布局器边框都设置为zero,设置widget的背景颜色为666

posted on 2024-02-19 14:02  一杯清酒邀明月  阅读(815)  评论(0编辑  收藏  举报