using NAudio.Wave; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace ListenerMic { public partial class Form1 : Form { public Form1() { InitializeComponent(); } WasapiLoopbackCapture capture; private void Form1_Load(object sender, EventArgs e) { AudioMixer.AudioMixing("recorded.wav", "test.wav", "mixaudio.wav"); } public void StartListener() { var outputFilePath = "recorded.wav"; capture = new WasapiLoopbackCapture(); var writer = new WaveFileWriter(outputFilePath, capture.WaveFormat); capture.DataAvailable += (s, a) => { writer.Write(a.Buffer, 0, a.BytesRecorded); }; capture.RecordingStopped += (s, a) => { writer.Dispose(); writer = null; capture.Dispose(); }; capture.StartRecording(); } private IWaveIn waveIn; private WaveFileWriter writer; private WaveFileWriter sysWriter; /// <summary> /// 开始录音 /// </summary> private void StartRecording() { if (waveIn != null) return; waveIn = new WaveIn { WaveFormat = new WaveFormat(48000, 1) };//设置码率 writer = new WaveFileWriter("test.wav", waveIn.WaveFormat); waveIn.DataAvailable += waveIn_DataAvailable; waveIn.RecordingStopped += OnRecordingStopped; waveIn.StartRecording(); } /// <summary> /// 停止录音 /// </summary> private void StopRecording() { waveIn.StopRecording(); waveIn.Dispose(); } /// <summary> /// 录音中 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void waveIn_DataAvailable(object sender, WaveInEventArgs e) { if (this.InvokeRequired) { this.BeginInvoke(new EventHandler<WaveInEventArgs>(waveIn_DataAvailable), sender, e); } else { writer.Write(e.Buffer, 0, e.BytesRecorded); int secondsRecorded = (int)(writer.Length / writer.WaveFormat.AverageBytesPerSecond);//录音时间获取 //if (secondsRecorded >= 30) //{ // StopRecording(); //} } } /// <summary> /// 停止录音 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnRecordingStopped(object sender, StoppedEventArgs e) { if (this.InvokeRequired) { this.BeginInvoke(new EventHandler<StoppedEventArgs>(OnRecordingStopped), sender, e); } else { if (waveIn != null) // 关闭录音对象 { waveIn.Dispose(); waveIn = null; } if (writer != null)//关闭文件流 { writer.Close(); writer = null; } if (e.Exception != null) { MessageBox.Show(String.Format("出现问题 {0}", e.Exception.Message)); } } } private void button1_Click(object sender, EventArgs e) { StartRecording(); StartListener(); } private void button2_Click(object sender, EventArgs e) { capture.StopRecording(); StopRecording(); } } }
using NAudio.Wave; using NAudio.Wave.SampleProviders; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ListenerMic { public class AudioMixer { /// <summary> /// 音频混合(混音) /// </summary> /// <param name="filePath1">音频文件路径</param> /// <param name="filePath2">音频文件路径</param> /// <param name="mixedPath">混合音频文件路径</param> public static void AudioMixing(string filePath1, string filePath2, string mixedPath) { using (var reader1 = new AudioFileReader(filePath1)) using (var reader2 = new AudioFileReader(filePath2)) { var mixer = new MixingSampleProvider(new[] { reader1, reader2 }); WaveFileWriter.CreateWaveFile16(mixedPath, mixer); } } } }
文章评论