当前位置: 首页 > 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;…...

经典算法实现:二分查找、全排列与子集生成

在算法学习中&#xff0c;二分查找、全排列、子集生成是非常基础且重要的内容。本文将结合 C 代码&#xff0c;详细讲解这三种经典算法的实现思路与核心逻辑&#xff0c;帮助大家理解算法的底层原理和代码落地方式。一、二分查找&#xff08;Binary Search&#xff09;二分查找…...

OpenClaw节日营销助手:gemma-3-12b-it自动生成祝福语与发送邮件

OpenClaw节日营销助手&#xff1a;gemma-3-12b-it自动生成祝福语与发送邮件 1. 为什么需要节日营销自动化&#xff1f; 去年端午节前夜&#xff0c;我盯着电脑屏幕上的200多个客户邮箱地址发呆。每个客户都需要个性化的节日祝福&#xff0c;但手动编写和发送至少需要6小时。当…...

高效大麦抢票自动化工具实战指南:开源项目的专业配置教程

高效大麦抢票自动化工具实战指南&#xff1a;开源项目的专业配置教程 【免费下载链接】ticket-purchase 大麦自动抢票&#xff0c;支持人员、城市、日期场次、价格选择 项目地址: https://gitcode.com/GitHub_Trending/ti/ticket-purchase 大麦网作为国内领先的演出票务…...

雷小兔:让学术论文排版变得简单高效

产品概述 雷小兔是一款专门为学生和研究人员设计的学术论文辅助工具。无论你是在准备毕业论文、学位论文还是学术发表&#xff0c;雷小兔都能为你提供全面的支持和帮助。 论文排版方面的核心优势 1. 模板齐全&#xff0c;开箱即用 雷小兔内置了数十种符合国内外高校标准的论…...

三菱PLC与组态王四层电梯控制系统:详细图纸与IO分配解释

三菱PLC和组态王4层电梯四层电梯控制系统 我们主要的后发送的产品有&#xff0c;带解释的梯形图接线图原理图图纸&#xff0c;io分配&#xff0c;组态画面实验室四层电梯模型卡成狗的时候&#xff0c;真的恨自己当初梯形图只会写互锁单按钮那种幼儿园题。后来拆前辈的旧板子加…...

深入Fly-By拓扑:为什么你的LPDDR4必须做Write Leveling?一次讲清时钟与数据对齐的核心原理

深入Fly-By拓扑&#xff1a;为什么你的LPDDR4必须做Write Leveling&#xff1f;一次讲清时钟与数据对齐的核心原理 在4266 Mbps的高速数据传输场景下&#xff0c;LPDDR4内存子系统如同一条需要精确调谐的八车道高速公路。当信号传输速率突破4GT/s时&#xff0c;皮秒级的时序偏差…...

快速部署Python3.10环境:Miniconda镜像实战教学

快速部署Python3.10环境&#xff1a;Miniconda镜像实战教学 1. 为什么选择Miniconda搭建Python环境&#xff1f; 在Python开发中&#xff0c;最让人头疼的问题之一就是环境管理。不同项目可能需要不同版本的Python和依赖库&#xff0c;直接安装会导致版本冲突。Miniconda提供…...

如何3步掌握Home Assistant SSH Web终端:从零到精通的管理指南 ✨

如何3步掌握Home Assistant SSH Web终端&#xff1a;从零到精通的管理指南 ✨ 【免费下载链接】app-ssh Advanced SSH & Web Terminal - Home Assistant Community Apps 项目地址: https://gitcode.com/gh_mirrors/ad/app-ssh 在智能家居系统的日常维护中&#xff0…...

2025届必备的六大AI辅助写作神器解析与推荐

Ai论文网站排名&#xff08;开题报告、文献综述、降aigc率、降重综合对比&#xff09; TOP1. 千笔AI TOP2. aipasspaper TOP3. 清北论文 TOP4. 豆包 TOP5. kimi TOP6. deepseek 处于人工智能技术基础之上的智能辅助系统&#xff0c;是可给学术研究者送去高效、规范的开题…...

视频修复终极指南:如何用UNTRUNC拯救你的损坏视频文件

视频修复终极指南&#xff1a;如何用UNTRUNC拯救你的损坏视频文件 【免费下载链接】untrunc Restore a damaged (truncated) mp4, m4v, mov, 3gp video. Provided you have a similar not broken video. 项目地址: https://gitcode.com/gh_mirrors/unt/untrunc 还记得那…...