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

linux之kylin系统nginx的安装

一、nginx的作用 1.可做高性能的web服务器 直接处理静态资源&#xff08;HTML/CSS/图片等&#xff09;&#xff0c;响应速度远超传统服务器类似apache支持高并发连接 2.反向代理服务器 隐藏后端服务器IP地址&#xff0c;提高安全性 3.负载均衡服务器 支持多种策略分发流量…...

docker详细操作--未完待续

docker介绍 docker官网: Docker&#xff1a;加速容器应用程序开发 harbor官网&#xff1a;Harbor - Harbor 中文 使用docker加速器: Docker镜像极速下载服务 - 毫秒镜像 是什么 Docker 是一种开源的容器化平台&#xff0c;用于将应用程序及其依赖项&#xff08;如库、运行时环…...

DeepSeek 赋能智慧能源:微电网优化调度的智能革新路径

目录 一、智慧能源微电网优化调度概述1.1 智慧能源微电网概念1.2 优化调度的重要性1.3 目前面临的挑战 二、DeepSeek 技术探秘2.1 DeepSeek 技术原理2.2 DeepSeek 独特优势2.3 DeepSeek 在 AI 领域地位 三、DeepSeek 在微电网优化调度中的应用剖析3.1 数据处理与分析3.2 预测与…...

跨链模式:多链互操作架构与性能扩展方案

跨链模式&#xff1a;多链互操作架构与性能扩展方案 ——构建下一代区块链互联网的技术基石 一、跨链架构的核心范式演进 1. 分层协议栈&#xff1a;模块化解耦设计 现代跨链系统采用分层协议栈实现灵活扩展&#xff08;H2Cross架构&#xff09;&#xff1a; 适配层&#xf…...

全志A40i android7.1 调试信息打印串口由uart0改为uart3

一&#xff0c;概述 1. 目的 将调试信息打印串口由uart0改为uart3。 2. 版本信息 Uboot版本&#xff1a;2014.07&#xff1b; Kernel版本&#xff1a;Linux-3.10&#xff1b; 二&#xff0c;Uboot 1. sys_config.fex改动 使能uart3(TX:PH00 RX:PH01)&#xff0c;并让boo…...

【笔记】WSL 中 Rust 安装与测试完整记录

#工作记录 WSL 中 Rust 安装与测试完整记录 1. 运行环境 系统&#xff1a;Ubuntu 24.04 LTS (WSL2)架构&#xff1a;x86_64 (GNU/Linux)Rust 版本&#xff1a;rustc 1.87.0 (2025-05-09)Cargo 版本&#xff1a;cargo 1.87.0 (2025-05-06) 2. 安装 Rust 2.1 使用 Rust 官方安…...

【Linux手册】探秘系统世界:从用户交互到硬件底层的全链路工作之旅

目录 前言 操作系统与驱动程序 是什么&#xff0c;为什么 怎么做 system call 用户操作接口 总结 前言 日常生活中&#xff0c;我们在使用电子设备时&#xff0c;我们所输入执行的每一条指令最终大多都会作用到硬件上&#xff0c;比如下载一款软件最终会下载到硬盘上&am…...

【深度学习新浪潮】什么是credit assignment problem?

Credit Assignment Problem(信用分配问题) 是机器学习,尤其是强化学习(RL)中的核心挑战之一,指的是如何将最终的奖励或惩罚准确地分配给导致该结果的各个中间动作或决策。在序列决策任务中,智能体执行一系列动作后获得一个最终奖励,但每个动作对最终结果的贡献程度往往…...

PostgreSQL 对 IPv6 的支持情况

PostgreSQL 对 IPv6 的支持情况 PostgreSQL 全面支持 IPv6 网络协议&#xff0c;包括连接、存储和操作 IPv6 地址。以下是详细说明&#xff1a; 一、网络连接支持 1. 监听 IPv6 连接 在 postgresql.conf 中配置&#xff1a; listen_addresses 0.0.0.0,:: # 监听所有IPv4…...

Heygem50系显卡合成的视频声音杂音模糊解决方案

如果你在使用50系显卡有杂音的情况&#xff0c;可能还是官方适配问题&#xff0c;可以使用以下方案进行解决&#xff1a; 方案一&#xff1a;剪映替换音色&#xff08;简单适合普通玩家&#xff09; 使用剪映换音色即可&#xff0c;口型还是对上的&#xff0c;没有剪映vip的&…...