我能夠在WasapiLoopbackCapture(naudio)的幫助下捕獲揚聲器生成的系統音頻。但問題是它捕獲的wav文件和wav文件的大小非常大(幾乎10到15 MB /分鐘)。我必須捕捉2-3小時的音頻,這將會太高。 我正在尋找一種解決方案,將WAV流轉換爲MP3,然後將其保存到磁盤上,由WasapiLoopbackCapture捕獲。我嘗試了LAME.exe或其他解決方案,但沒有成功。任何工作代碼。將WasapiLoopbackCapture wav音頻流轉換爲MP3文件
這裏是我的代碼:
private void button1_Click(object sender, EventArgs e){
LoopbackRecorder obj = new LoopbackRecorder();
string a = textBox1.Text;
obj.StartRecording(@"e:\aman.mp3");
}
public class LoopbackRecorder
{
private IWaveIn _waveIn;
private Mp3WaveFormat _mp3format;
private WaveFormat _wavFormat;
private WaveFileWriter _writer;
private bool _isRecording = false;
/// <summary>
/// Constructor
/// </summary>
public LoopbackRecorder()
{
}
/// <summary>
/// Starts the recording.
/// </summary>
/// <param name="fileName"></param>
public void StartRecording(string fileName)
{
// If we are currently record then go ahead and exit out.
if (_isRecording == true)
{
return;
}
_fileName = fileName;
_waveIn = new WasapiLoopbackCapture();
// _waveIn.WaveFormat = new WaveFormat(16000, 16 , 2);
_writer = new WaveFileWriter(fileName, _waveIn.WaveFormat);
_waveIn.DataAvailable += OnDataAvailable;
// _waveIn.RecordingStopped += OnRecordingStopped;
_waveIn.StartRecording();
_isRecording = true;
}
private void OnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
{
if (_writer == null)
{
_writer = new WaveFileWriter(@"e:\aman.mp3", _waveIn.WaveFormat);
}
_writer.Write(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);
byte[] by= Float32toInt16(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);
}
private string _fileName = "";
/// <summary>
/// The name of the file that was set when StartRecording was called. E.g. the current file being written to.
/// </summary>
public string FileName
{
get
{
return _fileName;
}
}
}
嗨,我無法添加使用NAudio.Lame; 在我的項目中我嘗試添加lame_enc.dll它也沒有添加到我的項目中,你可以告訴我如何在我的項目中添加這個名稱空間。 –
嗨,不,我可以將naudio.lame的ref添加到我的項目中,現在顯示錯誤「在聲明之前不能使用局部變量'waveIn'」。我嘗試像這樣//從迴環開始記錄 IWaveIn waveIn = new WasapiLoopbackCapture(); wri = new LameMP3FileWriter(@「C:\ temp \ test_output.mp3」,waveIn.WaveFormat,128); waveIn.DataAvailable + = waveIn_DataAvailable; waveIn.RecordingStopped + = waveIn_RecordingStopped; waveIn.StartRecording(); stopped = false; 但它仍然顯示錯誤: - 無支持的編碼格式可擴展 –
嗨科裏感謝它現在爲我工作。是的,它在基於64位的機器上顯示錯誤。解決此問題的任何解決方法。 –