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

ZAudioRecordHelper.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 = RecordType.microphone;
  22. private RecordState _state = RecordState.stop;
  23. //录制麦克风的声音
  24. private WaveInEvent waveIn = null; //new WaveInEvent();
  25. //录制扬声器的声音
  26. private WasapiLoopbackCapture capture = null; //new WasapiLoopbackCapture();
  27. //生成音频文件的对象
  28. private WaveFileWriter writer = null;
  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. }
  82. }
  83. /// <summary>
  84. /// 开始录制
  85. /// </summary>
  86. public void StartRecordAudio()
  87. {
  88. _state = RecordState.start;
  89. }
  90. /// <summary>
  91. /// 结束录制
  92. /// </summary>
  93. public void StopRecordAudio()
  94. {
  95. _state = RecordState.stop;
  96. if (_t == RecordType.microphone)
  97. {
  98. waveIn.StopRecording();
  99. }
  100. else
  101. {
  102. capture.StopRecording();
  103. }
  104. }
  105. /// <summary>
  106. /// 暂停录制
  107. /// </summary>
  108. public void PauseRecordAudio()
  109. {
  110. _state = RecordState.pause;
  111. }
  112. /// <summary>
  113. /// 恢复录制
  114. /// </summary>
  115. public void ResumeRecordAudio()
  116. {
  117. _state = RecordState.start;
  118. }
  119. /// <summary>
  120. /// 设备是否可用
  121. /// </summary>
  122. /// <param name="type"></param>
  123. /// <returns></returns>
  124. public static bool IsDeviceGood(RecordType type)
  125. {
  126. string tempPath = Path.GetTempPath();
  127. Console.WriteLine("临时路径:" + tempPath);
  128. WaveInEvent mWaveIn = null;
  129. WaveFileWriter mWriter = null;
  130. WasapiLoopbackCapture mCapture = null;
  131. try
  132. {
  133. if (type == RecordType.microphone)
  134. {
  135. string mAudioFile = Path.Combine(tempPath, "_microphone.mp3");
  136. mWaveIn = new WaveInEvent();
  137. mWriter = new WaveFileWriter(mAudioFile, mWaveIn.WaveFormat);
  138. //开始录音,写数据
  139. mWaveIn.DataAvailable += (s, a) =>
  140. {
  141. mWriter.Write(a.Buffer, 0, a.BytesRecorded);
  142. };
  143. //结束录音
  144. mWaveIn.RecordingStopped += (s, a) =>
  145. {
  146. mWriter.Dispose();
  147. mWriter = null;
  148. mWaveIn.Dispose();
  149. if (File.Exists(mAudioFile))
  150. {
  151. File.Delete(mAudioFile);
  152. }
  153. };
  154. mWaveIn.StartRecording();
  155. ThreadPool.QueueUserWorkItem(o =>
  156. {
  157. Thread.Sleep(200);
  158. mWaveIn.StopRecording();
  159. });
  160. }
  161. else
  162. {
  163. string mAudioFile = Path.Combine(tempPath, "_loudspeaker.mp3");
  164. mCapture = new WasapiLoopbackCapture();
  165. mWriter = new WaveFileWriter(mAudioFile, mCapture.WaveFormat);
  166. mCapture.DataAvailable += (s, a) =>
  167. {
  168. mWriter.Write(a.Buffer, 0, a.BytesRecorded);
  169. };
  170. //结束录音
  171. mCapture.RecordingStopped += (s, a) =>
  172. {
  173. mWriter.Dispose();
  174. mWriter = null;
  175. mCapture.Dispose();
  176. if (File.Exists(mAudioFile))
  177. {
  178. File.Delete(mAudioFile);
  179. }
  180. };
  181. mCapture.StartRecording();
  182. ThreadPool.QueueUserWorkItem(o =>
  183. {
  184. Thread.Sleep(200);
  185. mCapture.StopRecording();
  186. });
  187. }
  188. }
  189. catch (Exception ex)
  190. {
  191. if (mWriter != null)
  192. {
  193. mWriter.Dispose();
  194. mWriter = null;
  195. }
  196. return false;
  197. }
  198. return true;
  199. }
  200. }
  201. }