星火微课系统客户端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ZAudioRecordHelper.cs 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using NAudio.Wave;
  5. namespace XHWK.WKTool.Utils
  6. {
  7. public class ZAudioRecordHelper
  8. {
  9. public enum RecordType
  10. {
  11. Loudspeaker = 0, // 扬声器
  12. Microphone = 1 //麦克风
  13. }
  14. public enum RecordState
  15. {
  16. Stop = 0,
  17. Start = 1,
  18. Pause = 2
  19. }
  20. //录制的类型
  21. private RecordType _t;
  22. private RecordState _state;
  23. //录制麦克风的声音
  24. private WaveInEvent _waveIn; //new WaveInEvent();
  25. //录制扬声器的声音
  26. private WasapiLoopbackCapture _capture; //new WasapiLoopbackCapture();
  27. //生成音频文件的对象
  28. private WaveFileWriter _writer;
  29. private string _audioFile;
  30. public ZAudioRecordHelper(string filePath, RecordType type)
  31. {
  32. _t = type;
  33. _audioFile = filePath;
  34. _state = RecordState.Pause;
  35. try
  36. {
  37. if (_t == RecordType.Microphone)
  38. {
  39. _waveIn = new WaveInEvent();
  40. _writer = new WaveFileWriter(_audioFile, _waveIn.WaveFormat);
  41. //开始录音,写数据
  42. _waveIn.DataAvailable += (s, a) =>
  43. {
  44. if (_state == RecordState.Start)
  45. {
  46. _writer.Write(a.Buffer, 0, a.BytesRecorded);
  47. }
  48. };
  49. //结束录音
  50. _waveIn.RecordingStopped += (s, a) =>
  51. {
  52. _writer.Dispose();
  53. _writer = null;
  54. _waveIn.Dispose();
  55. };
  56. _waveIn.StartRecording();
  57. }
  58. else
  59. {
  60. _capture = new WasapiLoopbackCapture();
  61. _writer = new WaveFileWriter(_audioFile, _capture.WaveFormat);
  62. _capture.DataAvailable += (s, a) =>
  63. {
  64. if (_state == RecordState.Start)
  65. {
  66. _writer.Write(a.Buffer, 0, a.BytesRecorded);
  67. }
  68. };
  69. //结束录音
  70. _capture.RecordingStopped += (s, a) =>
  71. {
  72. _writer.Dispose();
  73. _writer = null;
  74. _capture.Dispose();
  75. };
  76. _capture.StartRecording();
  77. }
  78. }
  79. catch (Exception)
  80. {
  81. // ignored
  82. }
  83. }
  84. /// <summary>
  85. /// 开始录制
  86. /// </summary>
  87. public void StartRecordAudio()
  88. {
  89. _state = RecordState.Start;
  90. }
  91. /// <summary>
  92. /// 结束录制
  93. /// </summary>
  94. public void StopRecordAudio()
  95. {
  96. _state = RecordState.Stop;
  97. if (_t == RecordType.Microphone)
  98. {
  99. _waveIn.StopRecording();
  100. }
  101. else
  102. {
  103. _capture.StopRecording();
  104. }
  105. }
  106. /// <summary>
  107. /// 暂停录制
  108. /// </summary>
  109. public void PauseRecordAudio()
  110. {
  111. _state = RecordState.Pause;
  112. }
  113. /// <summary>
  114. /// 恢复录制
  115. /// </summary>
  116. public void ResumeRecordAudio()
  117. {
  118. _state = RecordState.Start;
  119. }
  120. /// <summary>
  121. /// 设备是否可用
  122. /// </summary>
  123. /// <param name="type"></param>
  124. /// <returns></returns>
  125. public static bool IsDeviceGood(RecordType type)
  126. {
  127. string tempPath = Path.GetTempPath();
  128. // Console.WriteLine("临时路径:" + tempPath);
  129. WaveInEvent mWaveIn;
  130. WaveFileWriter mWriter = null;
  131. WasapiLoopbackCapture mCapture;
  132. try
  133. {
  134. if (type == RecordType.Microphone)
  135. {
  136. string mAudioFile = Path.Combine(tempPath, "_microphone.mp3");
  137. mWaveIn = new WaveInEvent();
  138. mWriter = new WaveFileWriter(mAudioFile, mWaveIn.WaveFormat);
  139. //开始录音,写数据
  140. mWaveIn.DataAvailable += (s, a) => { mWriter.Write(a.Buffer, 0, a.BytesRecorded); };
  141. //结束录音
  142. mWaveIn.RecordingStopped += (s, a) =>
  143. {
  144. if (mWriter != null)
  145. {
  146. mWriter.Dispose();
  147. }
  148. mWriter = null;
  149. mWaveIn.Dispose();
  150. if (File.Exists(mAudioFile))
  151. {
  152. File.Delete(mAudioFile);
  153. }
  154. };
  155. mWaveIn.StartRecording();
  156. ThreadPool.QueueUserWorkItem(
  157. o =>
  158. {
  159. Thread.Sleep(200);
  160. mWaveIn.StopRecording();
  161. }
  162. );
  163. }
  164. else
  165. {
  166. string mAudioFile = Path.Combine(tempPath, "_loudspeaker.mp3");
  167. mCapture = new WasapiLoopbackCapture();
  168. mWriter = new WaveFileWriter(mAudioFile, mCapture.WaveFormat);
  169. mCapture.DataAvailable += (s, a) =>
  170. {
  171. mWriter?.Write(a.Buffer, 0, a.BytesRecorded);
  172. };
  173. //结束录音
  174. mCapture.RecordingStopped += (s, a) =>
  175. {
  176. mWriter?.Dispose();
  177. mWriter = null;
  178. mCapture.Dispose();
  179. if (File.Exists(mAudioFile))
  180. {
  181. File.Delete(mAudioFile);
  182. }
  183. };
  184. mCapture.StartRecording();
  185. ThreadPool.QueueUserWorkItem(
  186. o =>
  187. {
  188. Thread.Sleep(200);
  189. mCapture.StopRecording();
  190. }
  191. );
  192. }
  193. }
  194. catch (Exception)
  195. {
  196. if (mWriter != null)
  197. {
  198. mWriter.Dispose();
  199. mWriter = null;
  200. }
  201. return false;
  202. }
  203. return true;
  204. }
  205. }
  206. }