星火微课系统客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ZAudioRecordHelper.cs 6.6KB

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