-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.cpp
51 lines (47 loc) · 1.25 KB
/
audio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "audio.h"
Audio::Audio(QObject *parent) : QObject(parent)
{
}
void Audio::startAudio(QString fileName)
{
QAudioDeviceInfo device = QAudioDeviceInfo::defaultInputDevice();
if(device.isNull())//录音设备不存在
{
QMessageBox::warning(NULL,"QAudioDeviceInfo","录音设备不存在");
}
else
{
//音频编码要求
QAudioFormat m_format;
//设置采样频率
m_format.setSampleRate(16000);//采样率为16k
//设置通道
m_format.setChannelCount(1);
//设置位深
m_format.setSampleSize(16);
//设置编码格式
m_format.setCodec("aduio/pcm");
//判断设备是否支持该格式
if(!device.isFormatSupported(m_format))
{
m_format = device.nearestFormat(m_format);
}
//打开文件
m_file = new QFile;
m_file->setFileName(fileName);
m_file->open(QIODevice::WriteOnly);//向里面写数据
//创建录音对象
m_audio = new QAudioInput(m_format,this);
m_audio->start(m_file);
}
}
void Audio::stopAudio()
{
//停止录音
m_audio->stop();
//关闭文件
m_file->close();
//删除文件对象
delete m_file;
m_file = NULL;
}