Qt项目实战:基于QMediaPlayer开发视频播放器
创作时间:
作者:
@小白创作中心
Qt项目实战:基于QMediaPlayer开发视频播放器
引用
1
来源
1.
https://www.cnblogs.com/ybqjymy/p/18020952
本文基于Qt 5.12版本,介绍了如何使用QMediaPlayer开发一个简单的视频播放器。文章详细讲解了从项目配置到代码实现的全过程,并展示了如何在界面上添加QVideoWidget控件以及如何创建右键菜单。需要注意的是,Qt的最新版本已经更新到Qt 6.x,因此在实际开发中可能需要根据具体版本对代码进行适当调整。
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: 测试代码
#include "widget.h"
#include <QApplication>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 创建一个MediaPlayer
QMediaPlayer *player = new QMediaPlayer;
// 创建一个播放器窗口(Widget),用于显示MediaPlayer
QVideoWidget *vw = new QVideoWidget;
// 将player绑定到显示的窗口上
player->setVideoOutput(vw);
// 打开播放器的位置
player->setSource(QUrl::fromLocalFile("G:/video_test/video-h265.mkv"));
vw->setGeometry(100,100,640,480);
vw->show();
// Widget w;
// w.show();
return a.exec();
}
如何添加不是“非基本图形控件”的派生类
需求:需要在界面上直接拖一个 QVideoWidget,由于基本控件中没有QVideoWidget,所以需要想想办法
新建提升的类
因为QVideoWidget本身就继承于QWidget,所以这里选择QWidget作为基类
控件进行提升
成功播放视频
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QMenu>
#include <QFileDialog>
#include <QFile>
#include <QDir>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected:
virtual void contextMenuEvent(QContextMenuEvent *event);
private:
void createRightPopActions();
private slots:
void openLocalVideoSlot();
void openUrlVideoSlot();
private:
Ui::Widget *ui;
QMenu *popMenu; //右键弹出式菜单
QAction *openLocalAction; //打开本地文件
QAction *openUrlAction; //打开网络文件
QAction *fullScreenAction; //全屏显示
QAction *normalScreenAction; //普通显示
QAction *quitAction; //退出
QMediaPlayer *player;
//QVideoWidget* vw; //不需要了,界面上已经拖了一个QVideoWidget进去了
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget),
player(new QMediaPlayer) //创建一个MediaPlayer
{
ui->setupUi(this);
createRightPopActions();
player->setVideoOutput(ui->av_widget); //把player绑定到显示窗口上
}
/* 右键弹出 Menu & Action */
void Widget::createRightPopActions()
{
popMenu = new QMenu(this);
popMenu->setStyleSheet("background-color: rgb(100, 100, 100);");
openLocalAction = new QAction(this);
openLocalAction->setText(QString("打开本地文件"));
openUrlAction = new QAction(this);
openUrlAction->setText(QString("打开本地文件"));
fullScreenAction = new QAction(this);
fullScreenAction->setText(QString("全屏"));
normalScreenAction = new QAction(this);
normalScreenAction->setText(QString("普通"));
quitAction = new QAction(this);
quitAction->setText(QString("退出"));
connect(openLocalAction, SIGNAL(triggered(bool)), this, SLOT(openLocalVideoSlot()));
connect(openUrlAction, SIGNAL(triggered(bool)), this, SLOT(openUrlVideoSlot()));
connect(fullScreenAction, &QAction::triggered, this, &Widget::showFullScreen);
connect(normalScreenAction, &QAction::triggered, this, &Widget::showNormal);
connect(quitAction, &QAction::triggered, this, &Widget::close);
}
void Widget::openLocalVideoSlot()
{
QString file = QFileDialog::getOpenFileName(this, tr("Open file"),
QDir::homePath(),
tr("Multimedia files(*)"));
if (file.isEmpty())
return;
player->setSource(QUrl::fromLocalFile(file)); //设置需要打开的媒体文件
player->play();
}
void Widget::openUrlVideoSlot()
{
}
/*右键菜单接口*/
void Widget::contextMenuEvent(QContextMenuEvent *event)
{
Q_UNUSED(event); //未使用不警告
popMenu->clear();
popMenu->addAction(openLocalAction);
popMenu->addAction(openUrlAction);
popMenu->addAction(fullScreenAction);
popMenu->addAction(normalScreenAction);
popMenu->addAction(quitAction);
popMenu->exec(QCursor::pos());//QAction *exec(const QPoint &pos, QAction *at = Q_NULLPTR);
}
Widget::~Widget()
{
delete ui;
}
无边框的视屏
布局器边框都设置为zero,设置widget的背景颜色为666
热门推荐
录的视频怎么消除杂音?从录制到后期的杂音消除攻略
脚脖子肿了怎么消肿
异地婚姻如何经营?这些建议帮你维系幸福关系
如何根据考评意见进行员工反馈?
历时300多年,被称为“羊吃人”的圈地运动,推动了英国工业革命
辣椒种植条件的环境与气候要求(了解辣椒种植需要的生长条件)
广元的13种特色美食,你吃过几种,女皇故里的味蕾盛筵
故事从三个平遥人开始——山西大学引进人才记事
职业规划和需求怎么写好
牛磺酸功能分析
不看不知道!夏天来东山岛玩,原来还有这么多特色玩法......
双枪充电+智能升压,比亚迪破解超充桩功率桎梏难题
喀什自由行终极攻略|古城漫步+帕米尔秘境+美食地图
NGINX获取客户端真实IP地址的配置指南
心态决定命运:心态好,则一切顺遂;心态差,则烦恼不断
山西十大面食,带你品味地方美食之旅!
“拔萝卜”用上机器人 采收运输全过程自动化
拉普拉斯变换在信号处理中的应用:深度解析与课后答案(专家视角)
购房前,如何评估房屋周边配套成熟度?
Win11插耳机没声音怎么办?耳机插孔故障排查全攻略
本命年的传统信仰与心理调适:迎接挑战与成长的机会
ComfyUI 节点操作相关说明
彩椒和青椒的营养价值一样吗
人工智能的可解释性:从黑箱到透明
白石山景区旅游攻略
流化床的用途是什么?提高工业流程的效率
科学有效的屋内小蚂蚁治理方案(打造无小蚂蚁的舒适家园)
Uniapp 生命周期钩子destroyed 详解
如何和团队说再见的话
如何找回丢失的微信聊天记录:多种方法详解与备份建议