gpt4 book ai didi

c# - 在 C# 中从麦克风和扬声器录制音频

转载 作者:太空宇宙 更新时间:2023-11-03 10:26:00 24 4
gpt4 key购买 nike

我正在构建一个应用程序,它将麦克风和扬声器的音频记录到一个文件 (.wav) 中。我希望我的应用程序将数据写入文件,从麦克风和扬声器写入同一个合并文件。我无法为两者保存合并文件。它只为麦克风保存。

这是我的代码:

using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test_Recorder
{
public partial class Form2 : Form
{
private WaveIn recorder;
private BufferedWaveProvider bufferedWaveProvider;
private SavingWaveProvider savingWaveProvider;
private WaveOut player;
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
StartRecording();
}

private void button2_Click(object sender, EventArgs e)
{
StopRecording();
}
public void StartRecording()
{
// set up the recorder
recorder = new WaveIn();
//recorder = new WasapiLoopbackCapture();
//recorder = new WaveIn();
recorder.DataAvailable += RecorderOnDataAvailable;

// set up our signal chain
bufferedWaveProvider = new BufferedWaveProvider(recorder.WaveFormat);
savingWaveProvider = new SavingWaveProvider(bufferedWaveProvider, Guid.NewGuid() + ".wav");

// set up playback
player = new WaveOut();
player.Init(savingWaveProvider);
player.Volume = 1;
// begin playback & record
player.Play();
recorder.StartRecording();
}
public void StopRecording()
{
// stop recording
recorder.StopRecording();
// stop playback
player.Stop();
// finalise the WAV file
savingWaveProvider.Dispose();
}
private void RecorderOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
{
bufferedWaveProvider.AddSamples(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);
}

private void button3_Click(object sender, EventArgs e)
{
//rec.BeginRecording();
Form3 frm = new Form3();
this.Hide();
frm.Show();
}
}


class SavingWaveProvider : IWaveProvider, IDisposable
{
private readonly IWaveProvider sourceWaveProvider;
private readonly WaveFileWriter writer;
private bool isWriterDisposed;

public SavingWaveProvider(IWaveProvider sourceWaveProvider, string wavFilePath)
{
this.sourceWaveProvider = sourceWaveProvider;
writer = new WaveFileWriter(wavFilePath, sourceWaveProvider.WaveFormat);
}

public int Read(byte[] buffer, int offset, int count)
{
var read = sourceWaveProvider.Read(buffer, offset, count);
if (count > 0 && !isWriterDisposed)
{
writer.Write(buffer, offset, read);
}
if (count == 0)
{
Dispose(); // auto-dispose in case users forget
}
return read;
}

public WaveFormat WaveFormat { get { return sourceWaveProvider.WaveFormat; } }

public void Dispose()
{
if (!isWriterDisposed)
{
isWriterDisposed = true;
writer.Dispose();
}
}
}
}

如何将扬声器和麦克风保存到同一个文件?

最佳答案

首先,您不能将 2 个数据流保存到一个已打开的文件中。您需要创建 2 个独立的内存流并将它们与一些奇特的魔法 dll 连接起来 :) 然后您可以使用 StreamWriter 或其他文件写入方法将这个内存流保存到文件中

为了节省您的谷歌搜索时间,我发现这段小代码可能会帮助您解决问题。 http://www.codeproject.com/Articles/31356/C-MP3-Sound-Capturing-Recording-Component ,连同这个 How to record audio from microphone (Line-In) and playback device (speakers Line-Out) to one MP3 file

还有一个提示。更好的做法是使用“using”子句然后调用 Dispose。

关于c# - 在 C# 中从麦克风和扬声器录制音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31588335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com