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

国防科技大学计算机基础课程笔记02信息编码

1.机内码和国标码 国标码就是我们非常熟悉的这个GB2312,但是因为都是16进制&#xff0c;因此这个了16进制的数据既可以翻译成为这个机器码&#xff0c;也可以翻译成为这个国标码&#xff0c;所以这个时候很容易会出现这个歧义的情况&#xff1b; 因此&#xff0c;我们的这个国…...

Xshell远程连接Kali(默认 | 私钥)Note版

前言:xshell远程连接&#xff0c;私钥连接和常规默认连接 任务一 开启ssh服务 service ssh status //查看ssh服务状态 service ssh start //开启ssh服务 update-rc.d ssh enable //开启自启动ssh服务 任务二 修改配置文件 vi /etc/ssh/ssh_config //第一…...

AI Agent与Agentic AI:原理、应用、挑战与未来展望

文章目录 一、引言二、AI Agent与Agentic AI的兴起2.1 技术契机与生态成熟2.2 Agent的定义与特征2.3 Agent的发展历程 三、AI Agent的核心技术栈解密3.1 感知模块代码示例&#xff1a;使用Python和OpenCV进行图像识别 3.2 认知与决策模块代码示例&#xff1a;使用OpenAI GPT-3进…...

mongodb源码分析session执行handleRequest命令find过程

mongo/transport/service_state_machine.cpp已经分析startSession创建ASIOSession过程&#xff0c;并且验证connection是否超过限制ASIOSession和connection是循环接受客户端命令&#xff0c;把数据流转换成Message&#xff0c;状态转变流程是&#xff1a;State::Created 》 St…...

2024年赣州旅游投资集团社会招聘笔试真

2024年赣州旅游投资集团社会招聘笔试真 题 ( 满 分 1 0 0 分 时 间 1 2 0 分 钟 ) 一、单选题(每题只有一个正确答案,答错、不答或多答均不得分) 1.纪要的特点不包括()。 A.概括重点 B.指导传达 C. 客观纪实 D.有言必录 【答案】: D 2.1864年,()预言了电磁波的存在,并指出…...

JVM暂停(Stop-The-World,STW)的原因分类及对应排查方案

JVM暂停(Stop-The-World,STW)的完整原因分类及对应排查方案,结合JVM运行机制和常见故障场景整理而成: 一、GC相关暂停​​ 1. ​​安全点(Safepoint)阻塞​​ ​​现象​​:JVM暂停但无GC日志,日志显示No GCs detected。​​原因​​:JVM等待所有线程进入安全点(如…...

Map相关知识

数据结构 二叉树 二叉树&#xff0c;顾名思义&#xff0c;每个节点最多有两个“叉”&#xff0c;也就是两个子节点&#xff0c;分别是左子 节点和右子节点。不过&#xff0c;二叉树并不要求每个节点都有两个子节点&#xff0c;有的节点只 有左子节点&#xff0c;有的节点只有…...

Mobile ALOHA全身模仿学习

一、题目 Mobile ALOHA&#xff1a;通过低成本全身远程操作学习双手移动操作 传统模仿学习&#xff08;Imitation Learning&#xff09;缺点&#xff1a;聚焦与桌面操作&#xff0c;缺乏通用任务所需的移动性和灵活性 本论文优点&#xff1a;&#xff08;1&#xff09;在ALOHA…...

10-Oracle 23 ai Vector Search 概述和参数

一、Oracle AI Vector Search 概述 企业和个人都在尝试各种AI&#xff0c;使用客户端或是内部自己搭建集成大模型的终端&#xff0c;加速与大型语言模型&#xff08;LLM&#xff09;的结合&#xff0c;同时使用检索增强生成&#xff08;Retrieval Augmented Generation &#…...

Python ROS2【机器人中间件框架】 简介

销量过万TEEIS德国护膝夏天用薄款 优惠券冠生园 百花蜂蜜428g 挤压瓶纯蜂蜜巨奇严选 鞋子除臭剂360ml 多芬身体磨砂膏280g健70%-75%酒精消毒棉片湿巾1418cm 80片/袋3袋大包清洁食品用消毒 优惠券AIMORNY52朵红玫瑰永生香皂花同城配送非鲜花七夕情人节生日礼物送女友 热卖妙洁棉…...