源地址:http://www.oschina.net/code/snippet_1243295_48623
[代码] [C/C++]代码
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui( new Ui::MainWindow) { ui->setupUi( this ); } MainWindow::~MainWindow() { delete ui; } //开始录音 void MainWindow::on_start_clicked() { file = new QFile(); file->setFileName( "E:\nihao" ); //设置其实设置音频文件的存放路径(输入音频名及存放路径) bool is_open =file->open(QIODevice::WriteOnly | QIODevice::Truncate); if (!is_open) { qDebug()<< "打开失败失败" <<endl; exit (1); } QAudioFormat format; format.setSampleRate(8000); format.setChannelCount(1); format.setSampleSize(8); format.setCodec( "audio/wav" ); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::UnSignedInt); QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice(); QString str=info.deviceName(); qDebug()<< "使用的录音设备是:" <<str<<endl; if (!info.isFormatSupported(format)) { format = info.nearestFormat(format); } input = new QAudioInput(format, this ); ui->label->setText( "录音开始......" ); input->start(file); } //停止录音 void MainWindow::on_finish_clicked() { input->stop(); file->close(); ui->label->setText( "录音结束......" ); } //播放录音 void MainWindow::on_play_clicked() { file->setFileName( "E:\test.wav" ); file->open(QIODevice::ReadOnly); QAudioFormat format; format.setSampleRate(8000); format.setChannelCount(1); format.setSampleSize(8); format.setCodec( "audio/wav" ); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::UnSignedInt); output = new QAudioOutput(format); output->start(file); } mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMultimedia> #include <QFile> #include <QDebug> #include <QAudioFormat> #include <QAudioDeviceInfo> #include <QAudioInput> #include <QTimer> #include <QAudioOutput> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public : explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_start_clicked(); void on_finish_clicked(); void on_play_clicked(); private : Ui::MainWindow *ui; QFile *file; QAudioInput * input; QAudioOutput* output; }; #endif // MAINWINDOW_H |