QT 实现录音功能(注意事项)
百度能找到很多的案例,把录制音频出来,但是会出现几个问题
(1)、如何读取
(2)、录制的声音不对
(3)、录制的声音有很大的杂音
现在我们来解决这个问题:
1、如何读取,大家不用写代码那么麻烦给大家分享一个工具cooleditprolvse
https://pan.baidu.com/s/1uDABRO9lVIka8pHWMt8p4w
2、如何录制电脑系统的声音,注意不是麦克风
3、录制的声音有很大的杂音
代码中的设置需要匹配当前 录音设备的配置(开发中替换成自动获取)
format.setSampleRate(48000);format.setChannelCount(2);format.setSampleSize(16);//这个需要完全匹配 ,不然读取不出来,或者杂音很大
附加源代码一份:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);init();//read();
}void MainWindow::init()
{outputFile.setFileName("test.raw");outputFile.open( QIODevice::WriteOnly | QIODevice::Truncate );QAudioFormat format;format.setSampleRate(48000);format.setChannelCount(2);format.setSampleSize(16);format.setCodec("audio/pcm");format.setByteOrder(QAudioFormat::LittleEndian);format.setSampleType(QAudioFormat::UnSignedInt);QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();if (!info.isFormatSupported(format)){qWarning()<<"default format not supported try to use nearest";format = info.nearestFormat(format);}audio = new QAudioInput(info, format, this);QTimer::singleShot(10000, this, SLOT(stopRecording()));audio->start(&outputFile);qDebug() <<"record begin!";
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::read()
{outputFile.setFileName("test.raw");outputFile.open(QIODevice::ReadOnly);QAudioFormat format;format.setSampleRate(48000);format.setChannelCount(2);format.setSampleSize(16);format.setCodec("audio/pcm");format.setByteOrder(QAudioFormat::LittleEndian);format.setSampleType(QAudioFormat::UnSignedInt);QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());if (!info.isFormatSupported(format)){qWarning()<<"raw audio format not supported by backend, cannot play audio.";return;}outAudio = new QAudioOutput(format, this);connect(outAudio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));outAudio->start(&outputFile);qDebug() <<"play begin!";
}void MainWindow::stopRecording()
{audio->stop();outputFile.close();delete audio;qDebug() <<"record end!";
}void MainWindow::finishedPlaying(QAudio::State state)
{if(state == QAudio::IdleState){outAudio->stop();outputFile.close();delete outAudio;}qDebug() << "play end!";
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QWidget>
#include <QtGui>
#include <QAudioInput>
#include <QAudioOutput>
#include <iostream>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);void init();~MainWindow();void read();private slots:void stopRecording();void finishedPlaying(QAudio::State state);private:Ui::MainWindow *ui;QFile outputFile; // class member.QAudioInput* audio; // class member.QAudioOutput *outAudio;
};#endif // MAINWINDOW_H
QT 实现录音功能(注意事项)相关推荐
- CMake结合Visual Studio中开发Qt应用程序注意事项
Qt工程管理 个人比较偏爱于使用CMake来管理C++工程,因为只要编写一个CMakeLists.txt文件,就可以在Windows和Mac上生成各自的IDE工程.在Windows上, CMake自然 ...
- QT Q_OBJECT使用注意事项
使用qobject_cast时遇到的问题 void Optimask::currentCellTreeChanged(int index) { CCellTreeWidget *celltree; c ...
- Qt编写自定义控件插件路过的坑及注意事项
在一日一控件的口号下,终于写好了五十几个自定义控件,包括各种仪表盘,各种温度计,各种进度条,各种按钮等,具体可参见(http://www.cnblogs.com/feiyangqingyun/p/61 ...
- Qt编写自定义控件及插件的使用
在使用Qt Designer设计窗体界面时,我们可以使用Widget Box里的窗体控件非常方便的绘制界面,比如拖进去一个按钮,一个文本编辑器等.虽然Qt Designer里的控件可以满足我们大部分的 ...
- Qt之可重入与线程安全
本篇文章中,术语"可重入性"和"线程安全"被用来标记类与函数,以表明它们如何被应用在多线程应用程序中. 一个线程安全的函数可以同时被多个线程调用,甚至调用者会使 ...
- Jenkins系列:5、wsl下的Jenkins编译Windows下的Qt程序并自动打包成exe
Jenkins系列:5.wsl下的Jenkins编译Windows下的Qt程序并自动打包成exe 文章目录 Jenkins系列:5.wsl下的Jenkins编译Windows下的Qt程序并自动打包成e ...
- Qt+MySQL编程
项目需要开发一个数据库软件,并且整个软件都是使用Qt搭建的,数据库选用的是MySQL,因此需要使用Qt调用MySQL,在此记录一下相关方法与问题,以方便自己与他人. 本项目使用的是QtCreator( ...
- 基于海思H3520DV400和QT5.9设计的车载终端DVR控制平台
前言: 随着4G/5G的发展,无线带宽不断的扩大,数据流量费用不断的降低,使得现在的实时网络视频和视频监控逐渐的普及.传统的安防项目和车载监控系统都离不开音视频的录制,保存,回放,再加上现在的远程实时 ...
- Qt 小技巧之“To-Do 事项”
Qt Creator 2.5 版本的时候增加了这个插件 一直没怎么用到 现在想起来 google了一下 做个总结吧 先晒图 就是这样啦 用法也很简单 在项目的随便一个位置 写一个注释 只要以上面5个关 ...
最新文章
- Scrum Master是什么?Scrum Master的职责是什么?和PM又有哪些区别?
- POJ 2653 线段交
- RegularExpressionValidator 控件用法
- python创建txt文件
- 概率统计笔记: 协方差与相关系数
- 精进:如何成为一个很厉害的人---书摘(转)
- Jmeter初学(一)
- Expression Blend 定义RadioButton行为动画
- OkHttp3 HTTP请求执行流程分析
- 《java设计模式》之责任链模式
- ie浏览器在线使用_微软加速反IE战略,超过1000个网站将拒绝渲染
- 传感器和单片机主板之间远距离通讯应该有哪些保障处理措施
- VMware vSAN 的内部版本号和版本 (2150753)--2020-10-27 更新
- 第一章 硬件介绍和环境配置
- 详解今日头条技术架构分析
- cmd 复制文件命令copy 复制目录树命令xcopy
- 绘图用计算机软件的基本种类有,主编教您电脑绘图软件有哪些
- qq团购群如何引流?群主如何建群拉新以及营销
- 韩剧Search[1992-2005]
- 汉诺塔游戏玩法介绍(攻略和编程实现)