|
@@ -1,4 +1,6 @@
|
1
|
1
|
using System;
|
|
2
|
+using System.IO;
|
|
3
|
+using System.Threading;
|
2
|
4
|
|
3
|
5
|
using NAudio.Wave;
|
4
|
6
|
|
|
@@ -33,7 +35,7 @@ namespace XHWK.WKTool.Utils
|
33
|
35
|
|
34
|
36
|
string audioFile = "";
|
35
|
37
|
|
36
|
|
- public ZAudioRecordHelper(string filePath,RecordType type)
|
|
38
|
+ public ZAudioRecordHelper(string filePath, RecordType type)
|
37
|
39
|
{
|
38
|
40
|
_t = type;
|
39
|
41
|
audioFile = filePath;
|
|
@@ -54,7 +56,8 @@ namespace XHWK.WKTool.Utils
|
54
|
56
|
//开始录音,写数据
|
55
|
57
|
waveIn.DataAvailable += (s, a) =>
|
56
|
58
|
{
|
57
|
|
- if (_state == RecordState.start) {
|
|
59
|
+ if (_state == RecordState.start)
|
|
60
|
+ {
|
58
|
61
|
writer.Write(a.Buffer, 0, a.BytesRecorded);
|
59
|
62
|
}
|
60
|
63
|
};
|
|
@@ -89,7 +92,7 @@ namespace XHWK.WKTool.Utils
|
89
|
92
|
capture.Dispose();
|
90
|
93
|
};
|
91
|
94
|
capture.StartRecording();
|
92
|
|
- }
|
|
95
|
+ }
|
93
|
96
|
}
|
94
|
97
|
catch (Exception ex)
|
95
|
98
|
{
|
|
@@ -123,8 +126,98 @@ namespace XHWK.WKTool.Utils
|
123
|
126
|
/// <summary>
|
124
|
127
|
/// 恢复录制
|
125
|
128
|
/// </summary>
|
126
|
|
- public void ResumeRecordAudio() {
|
|
129
|
+ public void ResumeRecordAudio()
|
|
130
|
+ {
|
127
|
131
|
_state = RecordState.start;
|
128
|
132
|
}
|
|
133
|
+
|
|
134
|
+ /// <summary>
|
|
135
|
+ /// 设备是否可用
|
|
136
|
+ /// </summary>
|
|
137
|
+ /// <param name="type"></param>
|
|
138
|
+ /// <returns></returns>
|
|
139
|
+ public static bool IsDeviceGood(RecordType type)
|
|
140
|
+ {
|
|
141
|
+ string tempPath = Path.GetTempPath();
|
|
142
|
+ Console.WriteLine("临时路径:" + tempPath);
|
|
143
|
+ WaveInEvent mWaveIn = null;
|
|
144
|
+ WaveFileWriter mWriter = null;
|
|
145
|
+ WasapiLoopbackCapture mCapture = null;
|
|
146
|
+ try
|
|
147
|
+ {
|
|
148
|
+ if (type == RecordType.microphone)
|
|
149
|
+ {
|
|
150
|
+ string mAudioFile = Path.Combine(tempPath, "_microphone.mp3");
|
|
151
|
+
|
|
152
|
+ mWaveIn = new WaveInEvent();
|
|
153
|
+ mWriter = new WaveFileWriter(mAudioFile, mWaveIn.WaveFormat);
|
|
154
|
+ //开始录音,写数据
|
|
155
|
+ mWaveIn.DataAvailable += (s, a) =>
|
|
156
|
+ {
|
|
157
|
+ mWriter.Write(a.Buffer, 0, a.BytesRecorded);
|
|
158
|
+ };
|
|
159
|
+
|
|
160
|
+ //结束录音
|
|
161
|
+ mWaveIn.RecordingStopped += (s, a) =>
|
|
162
|
+ {
|
|
163
|
+ mWriter.Dispose();
|
|
164
|
+ mWriter = null;
|
|
165
|
+ mWaveIn.Dispose();
|
|
166
|
+
|
|
167
|
+ if (File.Exists(mAudioFile))
|
|
168
|
+ {
|
|
169
|
+ File.Delete(mAudioFile);
|
|
170
|
+ }
|
|
171
|
+ };
|
|
172
|
+
|
|
173
|
+ mWaveIn.StartRecording();
|
|
174
|
+
|
|
175
|
+ ThreadPool.QueueUserWorkItem(o =>
|
|
176
|
+ {
|
|
177
|
+ Thread.Sleep(200);
|
|
178
|
+ mWaveIn.StopRecording();
|
|
179
|
+ });
|
|
180
|
+ }
|
|
181
|
+ else
|
|
182
|
+ {
|
|
183
|
+ string mAudioFile = Path.Combine(tempPath, "_loudspeaker.mp3");
|
|
184
|
+ mCapture = new WasapiLoopbackCapture();
|
|
185
|
+ mWriter = new WaveFileWriter(mAudioFile, mCapture.WaveFormat);
|
|
186
|
+
|
|
187
|
+ mCapture.DataAvailable += (s, a) =>
|
|
188
|
+ {
|
|
189
|
+ mWriter.Write(a.Buffer, 0, a.BytesRecorded);
|
|
190
|
+ };
|
|
191
|
+ //结束录音
|
|
192
|
+ mCapture.RecordingStopped += (s, a) =>
|
|
193
|
+ {
|
|
194
|
+ mWriter.Dispose();
|
|
195
|
+ mWriter = null;
|
|
196
|
+ mCapture.Dispose();
|
|
197
|
+
|
|
198
|
+ if (File.Exists(mAudioFile))
|
|
199
|
+ {
|
|
200
|
+ File.Delete(mAudioFile);
|
|
201
|
+ }
|
|
202
|
+ };
|
|
203
|
+ mCapture.StartRecording();
|
|
204
|
+ ThreadPool.QueueUserWorkItem(o =>
|
|
205
|
+ {
|
|
206
|
+ Thread.Sleep(200);
|
|
207
|
+ mCapture.StopRecording();
|
|
208
|
+ });
|
|
209
|
+ }
|
|
210
|
+ }
|
|
211
|
+ catch (Exception ex)
|
|
212
|
+ {
|
|
213
|
+ if (mWriter != null) {
|
|
214
|
+ mWriter.Dispose();
|
|
215
|
+ mWriter = null;
|
|
216
|
+ }
|
|
217
|
+ return false;
|
|
218
|
+ }
|
|
219
|
+
|
|
220
|
+ return true;
|
|
221
|
+ }
|
129
|
222
|
}
|
130
|
223
|
}
|