星火微课系统客户端
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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using NAudio.Wave;
  3. namespace XHWK.WKTool.Utils
  4. {
  5. public class ZAudioRecordHelper
  6. {
  7. public enum RecordType
  8. {
  9. loudspeaker = 0, // 扬声器
  10. microphone = 1 //麦克风
  11. }
  12. public enum RecordState
  13. {
  14. stop = 0,
  15. start = 1,
  16. pause = 2
  17. }
  18. //录制的类型
  19. RecordType _t = RecordType.microphone;
  20. RecordState _state = RecordState.stop;
  21. //录制麦克风的声音
  22. WaveInEvent waveIn = null; //new WaveInEvent();
  23. //录制扬声器的声音
  24. WasapiLoopbackCapture capture = null; //new WasapiLoopbackCapture();
  25. //生成音频文件的对象
  26. WaveFileWriter writer = null;
  27. string audioFile = "";
  28. public ZAudioRecordHelper(string filePath,RecordType type)
  29. {
  30. _t = type;
  31. audioFile = filePath;
  32. }
  33. /// <summary>
  34. /// 开始录制
  35. /// </summary>
  36. public void StartRecordAudio()
  37. {
  38. _state = RecordState.start;
  39. try
  40. {
  41. if (_t == RecordType.microphone)
  42. {
  43. waveIn = new WaveInEvent();
  44. writer = new WaveFileWriter(audioFile, waveIn.WaveFormat);
  45. //开始录音,写数据
  46. waveIn.DataAvailable += (s, a) =>
  47. {
  48. if (_state == RecordState.start) {
  49. writer.Write(a.Buffer, 0, a.BytesRecorded);
  50. }
  51. };
  52. //结束录音
  53. waveIn.RecordingStopped += (s, a) =>
  54. {
  55. writer.Dispose();
  56. writer = null;
  57. waveIn.Dispose();
  58. };
  59. waveIn.StartRecording();
  60. }
  61. else
  62. {
  63. capture = new WasapiLoopbackCapture();
  64. writer = new WaveFileWriter(audioFile, capture.WaveFormat);
  65. capture.DataAvailable += (s, a) =>
  66. {
  67. if (_state == RecordState.start)
  68. {
  69. writer.Write(a.Buffer, 0, a.BytesRecorded);
  70. }
  71. };
  72. //结束录音
  73. capture.RecordingStopped += (s, a) =>
  74. {
  75. writer.Dispose();
  76. writer = null;
  77. capture.Dispose();
  78. };
  79. capture.StartRecording();
  80. }
  81. }
  82. catch (Exception ex)
  83. {
  84. }
  85. }
  86. /// <summary>
  87. /// 结束录制
  88. /// </summary>
  89. public void StopRecordAudio()
  90. {
  91. _state = RecordState.stop;
  92. if (_t == RecordType.microphone)
  93. {
  94. waveIn.StopRecording();
  95. }
  96. else
  97. {
  98. capture.StopRecording();
  99. }
  100. }
  101. /// <summary>
  102. /// 暂停录制
  103. /// </summary>
  104. public void PauseRecordAudio()
  105. {
  106. _state = RecordState.pause;
  107. }
  108. /// <summary>
  109. /// 恢复录制
  110. /// </summary>
  111. public void ResumeRecordAudio() {
  112. _state = RecordState.start;
  113. }
  114. }
  115. }