百度能找到很多的案例,把录制音频出来,但是会出现几个问题
(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_OBJECT
public:
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