当前位置: 首页 > news >正文

QT----------多媒体

实现思路

  1. 多媒体模块功能概述
    • QT 的多媒体模块提供了丰富的功能,包括音频播放、录制、视频播放和摄像头操作等。
  2. 播放音频
    • 使用 QMediaPlayer 播放完整的音频文件。
    • 使用 QSoundEffect 播放简短的音效文件。
  3. 录制音频
    • 使用 QMediaRecorder 类进行音频录制。
    • 使用 QAudioSourceQAudioSink 类采集和播放原始音频数据。
  4. 播放视频文件
    • QVideoWidgetQGraphicsVideoItem 上播放视频。
  5. 摄像头的使用
    • 实现摄像头的控制,包括拍照和录像功能。

代码示例

1. 基于 QMediaPlayer 的音乐播放器
#include <QtWidgets/QApplication>
#include <QtMultimedia/QMediaPlayer>
#include <QtMultimediaWidgets/QVideoWidget>
#include <QtMultimedia/QMediaPlaylist>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QtCore/QUrl>
#include <QtCore/QDebug>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget *mainWidget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(mainWidget);QMediaPlayer *player = new QMediaPlayer();QMediaPlaylist *playlist = new QMediaPlaylist();playlist->addMedia(QUrl::fromLocalFile("path/to/your/audio.mp3"));playlist->setPlaybackMode(QMediaPlaylist::Loop);player->setPlaylist(playlist);QPushButton *playButton = new QPushButton("Play");connect(playButton, &QPushButton::clicked, player, &QMediaPlayer::play);layout->addWidget(playButton);mainWidget->show();return app.exec();
}
2. 使用 QSoundEffect 播放音效文件
#include <QtWidgets/QApplication>
#include <QtMultimedia/QSoundEffect>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget *mainWidget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(mainWidget);QSoundEffect *effect = new QSoundEffect();effect->setSource(QUrl::fromLocalFile("path/to/your/sound.wav"));QPushButton *playButton = new QPushButton("Play Sound");connect(playButton, &QPushButton::clicked, effect, &QSoundEffect::play);layout->addWidget(playButton);mainWidget->show();return app.exec();
}
3. 录制音频
#include <QtWidgets/QApplication>
#include <QtMultimedia/QMediaRecorder>
#include <QtMultimedia/QAudioRecorder>
#include <QtMultimedia/QAudioEncoderSettings>
#include <QtMultimedia/QVideoEncoderSettings>
#include <QtMultimedia/QCamera>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QtCore/QUrl>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget *mainWidget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(mainWidget);QMediaRecorder *recorder = new QMediaRecorder();QAudioEncoderSettings audioSettings;audioSettings.setCodec("audio/mp3");audioSettings.setQuality(QMultimedia::HighQuality);recorder->setAudioSettings(audioSettings);QPushButton *recordButton = new QPushButton("Record");connect(recordButton, &QPushButton::clicked, [recorder]() {if (recorder->state() == QMediaRecorder::StoppedState) {recorder->record();} else {recorder->stop();}});layout->addWidget(recordButton);mainWidget->show();return app.exec();
}
4. 采集和播放原始音频数据
#include <QtWidgets/QApplication>
#include <QtMultimedia/QAudioSource>
#include <QtMultimedia/QAudioSink>
#include <QtMultimedia/QAudioFormat>
#include <QtMultimedia/QAudioDeviceInfo>
#include <QtCore/QIODevice>
#include <QtCore/QDebug>class AudioProcessor : public QIODevice {Q_OBJECT
public:AudioProcessor(QObject *parent = nullptr) : QIODevice(parent) {}qint64 readData(char *data, qint64 maxlen) override {return 0;}qint64 writeData(const char *data, qint64 len) override {qDebug() << "Received audio data of length:" << len;return len;}
};int main(int argc, char *argv[]) {QApplication app(argc, argv);QAudioFormat format;format.setSampleRate(44100);format.setChannelCount(2);format.setSampleSize(16);format.setCodec("audio/pcm");format.setByteOrder(QAudioFormat::LittleEndian);format.setSampleType(QAudioFormat::UnSignedInt);QAudioDeviceInfo inputDevice = QAudioDeviceInfo::defaultInputDevice();if (!inputDevice.isFormatSupported(format)) {qWarning() << "Default input device does not support the format";format = inputDevice.nearestFormat(format);}QAudioSource *audioSource = new QAudioSource(format);AudioProcessor *processor = new AudioProcessor();audioSource->start(processor);QAudioDeviceInfo outputDevice = QAudioDeviceInfo::defaultOutputDevice();QAudioSink *audioSink = new QAudioSink(outputDevice, format);audioSink->start(processor);return app.exec();
}#include "main.moc"

在这里插入图片描述

5. 在 QVideoWidget 上播放视频文件
#include <QtWidgets/QApplication>
#include <QtMultimedia/QMediaPlayer>
#include <QtMultimediaWidgets/QVideoWidget>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QtCore/QUrl>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget *mainWidget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(mainWidget);QMediaPlayer *player = new QMediaPlayer();QVideoWidget *videoWidget = new QVideoWidget();player->setVideoOutput(videoWidget);player->setMedia(QUrl::fromLocalFile("path/to/your/video.mp4"));QPushButton *playButton = new QPushButton("Play Video");connect(playButton, &QPushButton::clicked, player, &QMediaPlayer::play);layout->addWidget(playButton);layout->addWidget(videoWidget);mainWidget->show();return app.exec();
}
6. 在 QGraphicsVideoItem 上播放视频文件
#include <QtWidgets/QApplication>
#include <QtMultimedia/QMediaPlayer>
#include <QtMultimediaWidgets/QGraphicsVideoItem>
#include <QtWidgets/QGraphicsView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QtCore/QUrl>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget *mainWidget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(mainWidget);QMediaPlayer *player = new QMediaPlayer();QGraphicsVideoItem *videoItem = new QGraphicsVideoItem();player->setVideoOutput(videoItem);player->setMedia(QUrl::fromLocalFile("path/to/your/video.mp4"));QGraphicsScene *scene = new QGraphicsScene();scene->addItem(videoItem);QGraphicsView *view = new QGraphicsView(scene);QPushButton *playButton = new QPushButton("Play Video");connect(playButton, &QPushButton::clicked, player, &QMediaPlayer::play);layout->addWidget(playButton);layout->addWidget(view);mainWidget->show();return app.exec();
}
7. 摄像头的使用(拍照和录像)
#include <QtWidgets/QApplication>
#include <QtMultimedia/QCamera>
#include <QtMultimedia/QCameraViewfinder>
#include <QtMultimedia/QCameraImageCapture>
#include <QtMultimedia/QMediaRecorder>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget *mainWidget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(mainWidget);QCamera *camera = new QCamera(QCamera::defaultCamera());QCameraViewfinder *viewfinder = new QCameraViewfinder();camera->setViewfinder(viewfinder);QCameraImageCapture *imageCapture = new QCameraImageCapture(camera);QMediaRecorder *mediaRecorder = new QMediaRecorder(camera);QPushButton *captureButton = new QPushButton("Take Photo");connect(captureButton, &QPushButton::clicked, [imageCapture]() {imageCapture->capture();});QPushButton *recordButton = new QPushButton("Start/Stop Recording");connect(recordButton, &QPushButton::clicked, [mediaRecorder]() {if (mediaRecorder->state() == QMediaRecorder::StoppedState) {mediaRecorder->record();} else {mediaRecorder->stop();}});layout->addWidget(viewfinder);layout->addWidget(captureButton);layout->addWidget(recordButton);camera->start();mainWidget->show();return app.exec();
}

代码解释

1. 基于 QMediaPlayer 的音乐播放器
  • QMediaPlayer
    • QMediaPlayer 用于播放音频和视频文件。
    • QMediaPlaylist 用于管理播放列表,可设置播放模式,如循环、顺序、随机等。
2. 使用 QSoundEffect 播放音效文件
  • QSoundEffect
    • 适合播放短音效,使用 setSource 加载音效文件,点击按钮调用 play 播放。
3. 录制音频
  • QMediaRecorder
    • QMediaRecorder 可以录制音频和视频。
    • QAudioEncoderSettings 可设置音频编码和质量。
4. 采集和播放原始音频数据
  • QAudioSource
    • 从输入设备采集音频,使用 start 开始采集。
  • QAudioSink
    • 向输出设备播放音频,使用 start 开始播放。
  • AudioProcessor
    • 自定义 QIODevice 子类,重写 writeData 处理接收到的音频数据。
5. 在 QVideoWidget 上播放视频文件
  • QMediaPlayer
    • 用于播放视频,通过 setVideoOutput 关联到 QVideoWidget 进行播放。
6. 在 QGraphicsVideoItem 上播放视频文件
  • QMediaPlayer
    • 关联到 QGraphicsVideoItem,添加到 QGraphicsScene 并通过 QGraphicsView 显示。
7. 摄像头的使用(拍照和录像)
  • QCamera
    • 表示摄像头设备,使用 setViewfinder 关联视图。
  • QCameraImageCapture
    • 用于拍照,调用 capture 进行拍照操作。
  • QMediaRecorder
    • 用于录像,调用 recordstop 控制录制过程。

使用说明

  • 对于每个示例,将代码保存为 main.cpp 文件。
  • 确保 .pro 文件包含 QT += multimedia multimediawidgets widgets 以及 CONFIG += c++11
  • 编译并运行程序,观察多媒体功能的效果。

在这里插入图片描述

相关文章:

QT----------多媒体

实现思路 多媒体模块功能概述&#xff1a; QT 的多媒体模块提供了丰富的功能&#xff0c;包括音频播放、录制、视频播放和摄像头操作等。 播放音频&#xff1a; 使用 QMediaPlayer 播放完整的音频文件。使用 QSoundEffect 播放简短的音效文件。 录制音频&#xff1a; 使用 QMe…...

选择器(结构伪类选择器,伪元素选择器),PxCook软件,盒子模型

结构为类选择器 伪元素选择器 PxCook 盒子模型 (内外边距&#xff0c;边框&#xff09; 内外边距合并&#xff0c;塌陷问题 元素溢出 圆角 阴影: 模糊半径&#xff1a;越大越模糊&#xff0c;也就是越柔和 案例一&#xff1a;产品卡片 <!DOCTYPE html> <html lang&q…...

Vue2/Vue3 响应式原理对比指南

Vue2/Vue3 响应式原理对比指南 1. 基本实现原理 1.1 Vue2 响应式实现 (Object.defineProperty) // Vue2 响应式核心实现 function defineReactive(obj, key, val) {// 递归处理嵌套对象observe(val);const dep new Dep();Object.defineProperty(obj, key, {get() {// 依赖收…...

FastExcel:超越EasyExcel的新一代Excel处理工具

简介 FastExcel是由原EasyExcel作者在阿里巴巴宣布停止维护EasyExcel之后推出的升级版框架。它继承了EasyExcel的所有优点&#xff0c;并且在性能和功能上进行了显著的提升和创新。 FastExcel的特点 高性能读写&#xff1a;FastExcel专注于性能优化&#xff0c;能够高效处理…...

大模型系列17-RAGFlow搭建本地知识库

大模型系列17-RAGFlow搭建本地知识库 安装ollama安装open-wehui安装并运行ragflowRAG&#xff08;检索、增强、生成&#xff09;RAG是什么RAG三过程RAG问答系统构建步骤向量库构建检索模块生成模块 RAG解决LLM的痛点 使用ragflow访问ragflow配置ollama模型添加Embedding模型添加…...

常用的mac软件下载地址

目录 iRightMouse Pro&#xff08;超级右键&#xff09; xmind&#xff08;思维导图&#xff09; Parallels Desktop&#xff08;虚拟机工具&#xff09; Paste&#xff08;跨平台复制粘贴&#xff09; AutoSwitchInput Pro&#xff08;自动切换输入法&#xff09; Snipa…...

基于51单片机和16X16LED点阵屏(74HC138和74HC595驱动)的小游戏《贪吃蛇》

目录 系列文章目录前言一、效果展示二、原理分析三、各模块代码1、定时器02、自制八位独立按键3、点阵屏模块 四、主函数总结 系列文章目录 前言 《贪吃蛇》&#xff0c;一款经典的、怀旧的小游戏&#xff0c;单片机入门必写程序。 以《贪吃蛇》为载体&#xff0c;熟悉各种屏…...

python中常用的内置函数介绍

python中常用的内置函数介绍 1. print()2. len()3. type()4. str(), int(), float()5. list(), tuple(), set(), dict()6. range()7. sum()8. max(), min()9. sorted()10. zip()11. enumerate()12. map()13. filter()14. any(), all()15. abs()16. pow()17. round()18. ord(), …...

【微服务】Spring Cloud Config解决的问题和案例

文章目录 强烈推荐引言解决问题1. 配置管理的集中化2. 配置的版本控制3. 环境特定配置4. 配置的动态刷新5. 安全管理敏感数据6. 配置的一致性 组件1. **配置服务器&#xff08;Config Server&#xff09;**2. **配置客户端&#xff08;Config Client&#xff09;** 配置示例配置…...

华为OD机试E卷 --最小的调整次数--24年OD统一考试(Java JS Python C C++)

文章目录 题目描述输入描述输出描述用例题目解析JS算法源码Java算法源码python算法源码c算法源码c++算法源码题目描述 有一个特异性的双端队列一,该队列可以从头部或尾部添加数据,但是只能从头部移出数据。 小A依次执行2n个指令往队列中添加数据和移出数据。其中n个指令是添…...

Oracle Dataguard(主库为 Oracle 11g 单节点)配置详解(2):配置主数据库

Oracle Dataguard&#xff08;主库为 Oracle 11g 单节点&#xff09;配置详解&#xff08;2&#xff09;&#xff1a;配置主数据库 目录 Oracle Dataguard&#xff08;主库为 Oracle 11g 单节点&#xff09;配置详解&#xff08;2&#xff09;&#xff1a;配置主数据库一、配置…...

慧集通iPaaS集成平台低代码训练-实践篇

练习使用帐号信息&#xff1a; 1.致远A8平台&#xff08;请自行准备测试环境&#xff09; 慧集通连接器配置相关信息 访问地址&#xff1a; rest账号&#xff1a;rest rest密码&#xff1a; OA账号&#xff1a; 2.云星空&#xff08;请自行准备测试环境&#xff09; 连接…...

TDengine 如何进行高效数据建模

1.背景 数据建模对于数据库建立后整体高效运行非常关键&#xff0c;不同建模方式&#xff0c;可能会产生相差几倍的性能差别 2. 建库 建模在建库阶段应考虑几下几点&#xff1a; 建多少库 根据业务情况确定建库个数&#xff0c;TDengine 不支持跨库查询&#xff0c;如果业…...

HarmonyOS NEXT应用开发实战:一分钟写一个网络接口,JsonFormat插件推荐

在开发鸿蒙操作系统应用时&#xff0c;网络接口的实现往往是一个繁琐且重复的过程。为了提高开发效率&#xff0c;坚果派(nutpi.net)特别推出了一个非常实用的插件——JsonFormat。这款插件的主要功能是将JSON格式的数据直接转换为arkts的结构定义&#xff0c;让我们在编写接口…...

基于动力学的MPC控制器设计盲点解析

文章目录 Apollo MPC控制器的设计架构误差模型和离散化预测模型推导目标函数和约束设计优化求解优化OSQP求解器参考文献 Apollo MPC控制器的设计架构 误差模型和离散化 状态变量和控制变量 1、Apollo MPC控制器中状态变量主要有如下6个 matrix_state_ Matrix::Zero(basic_stat…...

Java重要面试名词整理(十六):SpringBoot

由于SpringBoot和Spring、SpringMVC重合度较高&#xff0c;更多详细内容请参考https://blog.csdn.net/weixin_73195042/article/details/144632385 本文着重于SpringBoot的启动流程 文章目录 概念启动流程底层分析构造SpringApplication对象run(String... args)方法SpringBoo…...

在K8S中,如何部署kubesphere?

在Kubernetes集群中&#xff0c;对于一些基础能力较弱的群体来说K8S控制面板操作存在一定的难度&#xff0c;此时kubesphere可以有效的解决这类难题。以下是部署kubesphere的操作步骤&#xff1a; 操作部署&#xff1a; 1. 部署nfs共享存储目录 yum -y install nfs-server e…...

算法-查找缺失的数字

给定一个包含 [0, n] 中 n 个数的数组 nums &#xff0c;找出 [0, n] 这个范围内没有出现在数组中的那个数。 示例 1&#xff1a; 输入&#xff1a;nums [3,0,1] 输出&#xff1a;2 解释&#xff1a;n 3&#xff0c;因为有 3 个数字&#xff0c;所以所有的数字都在范围 [0,3…...

antd-vue - - - - - a-date-picker限制选择范围

antd-vue - - - - - a-date-picker限制选择范围 1. 效果展示2. 代码展示 1. 效果展示 如图&#xff1a;限制选择范围为 今年 & 去年 的 月份. 2. 代码展示 <template><a-date-picker:disabledDate"disabledDate"picker"month"/> &l…...

计算机网络练习题

学习这么多啦&#xff0c;那就简单写几个选择题巩固一下吧&#xff01; 1. 在IPv4分组各字段中&#xff0c;以下最适合携带隐藏信息的是&#xff08;D&#xff09; A、源IP地址 B、版本 C、TTL D、标识 2. OSI 参考模型中&#xff0c;数据链路层的主要功能是&#xff08;…...

第19节 Node.js Express 框架

Express 是一个为Node.js设计的web开发框架&#xff0c;它基于nodejs平台。 Express 简介 Express是一个简洁而灵活的node.js Web应用框架, 提供了一系列强大特性帮助你创建各种Web应用&#xff0c;和丰富的HTTP工具。 使用Express可以快速地搭建一个完整功能的网站。 Expre…...

使用VSCode开发Django指南

使用VSCode开发Django指南 一、概述 Django 是一个高级 Python 框架&#xff0c;专为快速、安全和可扩展的 Web 开发而设计。Django 包含对 URL 路由、页面模板和数据处理的丰富支持。 本文将创建一个简单的 Django 应用&#xff0c;其中包含三个使用通用基本模板的页面。在此…...

PPT|230页| 制造集团企业供应链端到端的数字化解决方案:从需求到结算的全链路业务闭环构建

制造业采购供应链管理是企业运营的核心环节&#xff0c;供应链协同管理在供应链上下游企业之间建立紧密的合作关系&#xff0c;通过信息共享、资源整合、业务协同等方式&#xff0c;实现供应链的全面管理和优化&#xff0c;提高供应链的效率和透明度&#xff0c;降低供应链的成…...

Swift 协议扩展精进之路:解决 CoreData 托管实体子类的类型不匹配问题(下)

概述 在 Swift 开发语言中&#xff0c;各位秃头小码农们可以充分利用语法本身所带来的便利去劈荆斩棘。我们还可以恣意利用泛型、协议关联类型和协议扩展来进一步简化和优化我们复杂的代码需求。 不过&#xff0c;在涉及到多个子类派生于基类进行多态模拟的场景下&#xff0c;…...

在四层代理中还原真实客户端ngx_stream_realip_module

一、模块原理与价值 PROXY Protocol 回溯 第三方负载均衡&#xff08;如 HAProxy、AWS NLB、阿里 SLB&#xff09;发起上游连接时&#xff0c;将真实客户端 IP/Port 写入 PROXY Protocol v1/v2 头。Stream 层接收到头部后&#xff0c;ngx_stream_realip_module 从中提取原始信息…...

C++中string流知识详解和示例

一、概览与类体系 C 提供三种基于内存字符串的流&#xff0c;定义在 <sstream> 中&#xff1a; std::istringstream&#xff1a;输入流&#xff0c;从已有字符串中读取并解析。std::ostringstream&#xff1a;输出流&#xff0c;向内部缓冲区写入内容&#xff0c;最终取…...

全面解析各类VPN技术:GRE、IPsec、L2TP、SSL与MPLS VPN对比

目录 引言 VPN技术概述 GRE VPN 3.1 GRE封装结构 3.2 GRE的应用场景 GRE over IPsec 4.1 GRE over IPsec封装结构 4.2 为什么使用GRE over IPsec&#xff1f; IPsec VPN 5.1 IPsec传输模式&#xff08;Transport Mode&#xff09; 5.2 IPsec隧道模式&#xff08;Tunne…...

Aspose.PDF 限制绕过方案:Java 字节码技术实战分享(仅供学习)

Aspose.PDF 限制绕过方案&#xff1a;Java 字节码技术实战分享&#xff08;仅供学习&#xff09; 一、Aspose.PDF 简介二、说明&#xff08;⚠️仅供学习与研究使用&#xff09;三、技术流程总览四、准备工作1. 下载 Jar 包2. Maven 项目依赖配置 五、字节码修改实现代码&#…...

【VLNs篇】07:NavRL—在动态环境中学习安全飞行

项目内容论文标题NavRL: 在动态环境中学习安全飞行 (NavRL: Learning Safe Flight in Dynamic Environments)核心问题解决无人机在包含静态和动态障碍物的复杂环境中进行安全、高效自主导航的挑战&#xff0c;克服传统方法和现有强化学习方法的局限性。核心算法基于近端策略优化…...

Kafka入门-生产者

生产者 生产者发送流程&#xff1a; 延迟时间为0ms时&#xff0c;也就意味着每当有数据就会直接发送 异步发送API 异步发送和同步发送的不同在于&#xff1a;异步发送不需要等待结果&#xff0c;同步发送必须等待结果才能进行下一步发送。 普通异步发送 首先导入所需的k…...