星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZAudioRecordHelper.cs 6.5KB

пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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) => { mWriter.Write(a.Buffer, 0, a.BytesRecorded); };
  140. //结束录音
  141. mWaveIn.RecordingStopped += (s, a) =>
  142. {
  143. mWriter.Dispose();
  144. mWriter = null;
  145. mWaveIn.Dispose();
  146. if (File.Exists(mAudioFile))
  147. {
  148. File.Delete(mAudioFile);
  149. }
  150. };
  151. mWaveIn.StartRecording();
  152. ThreadPool.QueueUserWorkItem(o =>
  153. {
  154. Thread.Sleep(200);
  155. mWaveIn.StopRecording();
  156. });
  157. }
  158. else
  159. {
  160. string mAudioFile = Path.Combine(tempPath, "_loudspeaker.mp3");
  161. mCapture = new WasapiLoopbackCapture();
  162. mWriter = new WaveFileWriter(mAudioFile, mCapture.WaveFormat);
  163. mCapture.DataAvailable += (s, a) => { mWriter.Write(a.Buffer, 0, a.BytesRecorded); };
  164. //结束录音
  165. mCapture.RecordingStopped += (s, a) =>
  166. {
  167. mWriter.Dispose();
  168. mWriter = null;
  169. mCapture.Dispose();
  170. if (File.Exists(mAudioFile))
  171. {
  172. File.Delete(mAudioFile);
  173. }
  174. };
  175. mCapture.StartRecording();
  176. ThreadPool.QueueUserWorkItem(o =>
  177. {
  178. Thread.Sleep(200);
  179. mCapture.StopRecording();
  180. });
  181. }
  182. }
  183. catch (Exception)
  184. {
  185. if (mWriter != null)
  186. {
  187. mWriter.Dispose();
  188. mWriter = null;
  189. }
  190. return false;
  191. }
  192. return true;
  193. }
  194. }
  195. }