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
热门推荐
在家种植食用菌其实有风险 孢子可能会引发过敏性鼻炎等
鼓和打击乐器物理特性和声波传播过程和话筒的特性FUNK&动力声学
莫桑钻和培育钻石的4个区别 一分钟搞清楚
托管工作如何管理团队
预算分配和财务规划
如何准确计算价格升跌幅?这种计算对市场趋势有何指示作用?
都喊超了GPT-4?杨立昆团队新上测试集,让大模型评分再也做不了弊
广电营销策划方案
2024运维学习路线图
减肥期间能吃腰果和开心果吗?医生的专业解答来了
证件过期不能用银行卡?不少上海人收到短信!和这件事有关→
CAE工程师必知:泊松比的前世今生
逾期申请减免的方法有哪些
古诗词里的气象密码 | 春风
五款经典麻酱蘸料配方,总有一款适合你
体重减轻幅度与血清尿酸变化关系研究:真实世界数据揭示关键阈值
苗刀:“百兵之帅”刚正不阿
健康减重之旅:BMI自测与28天定制化调整策略
“黄金晚自习”该做些什么
揭秘宵宫:五星火元素弓箭手的华丽烟花技艺与命座提升的惊人效果
ArduPilot无人船(车)常用参数调试
胃癌晚期手术成功率知多少?专业解答来了!
DDR4 vs. DDR5内存:全面对比与选购指南
民族乐器的节奏感(欢快节奏感强的民族乐器)
一个工资支付周期是1天还是3天?夜场压工资如何处理?
六礼:古代人结婚的六个步骤,你都了解吗?
墨西哥内战:卖国者圣安纳
被韩国无限推崇的“世宗大王”,是个什么人物?三分钟了解一下
成长中承担责任:青少年责任意识的培养与自我提升
周末涮火锅吧!火锅底料这样选→