使用 NAudio 将 MemoryStream 直接编码为 MP3

Encode MemoryStream directly to MP3 using NAudio

我正在尝试使用以下 NAudio nuget 包的方法之一:

MediaFoundationEncoder.EncodeToMp3(new RawSourceWaveStream(_audioStream, _audioWriter.WaveFormat), "Test.mp3", 320000);

using (var reader = new WaveFileReader(_audioStream))
{
    MediaFoundationEncoder.EncodeToMp3(reader, "Test.mp3", 320000);
}

这应该将 wav 流直接编码为 mp3。确实如此,但结果只包含噪音。

当我用

保存同一个 MemoryStream(WaveFileWriter)
File.WriteAllBytes("Test.wav", _audioStream.ToArray());
MediaFoundationEncoder.EncodeToMp3(new WaveFileReader("Test.wav"), "Test.mp3", 320000);

创建的wav文件很好,我可以用这个wav文件将它编码成mp3。但是我想避免先保存wav文件。

有没有一种方法可以将流直接编码为 mp3 而不会产生噪音?我不知道为什么一种方法有效而另一种方法无效。 Google 也没有帮助我。所以也许你们有什么想法。

感谢您的努力。

更多信息:
.NET 核心 3.0 Windows 应用程序
NAudio Nuget 包(版本:1.9.0)

完成class:

using NAudio.MediaFoundation;
using NAudio.Wave;
using System;
using System.IO;

namespace Recorder.NAudio
{
    public class Recorder
    {
        private WasapiLoopbackCapture _audioCapture;
        private WaveFileWriter _audioWriter;
        private MemoryStream _audioStream;

        public bool IsRecording { get; private set; }

        public Recorder()
        {
            MediaFoundationApi.Startup();
        }

        public void Init()
        {
            _audioCapture = new WasapiLoopbackCapture();

            _audioStream = new MemoryStream(1024);
            _audioWriter = new WaveFileWriter(_audioStream, new WaveFormat(44100, 24, 2));

            _audioCapture.DataAvailable += DataReceived;
            _audioCapture.RecordingStopped += RecordingStopped;
        }

        private void RecordingStopped(object sender, StoppedEventArgs e)
        {
            _audioCapture.DataAvailable -= DataReceived;
            _audioCapture.RecordingStopped -= RecordingStopped;

            _audioCapture.Dispose();
            _audioCapture = null;
            _audioWriter.Flush();
            _audioStream.Flush();

            _audioStream.Position = 0;

            //This works the mp3 file is fine, but I want to avoid the workaround of first saving a wav file to my hard drive
            File.WriteAllBytes("Test.wav", _audioStream.ToArray());
            MediaFoundationEncoder.EncodeToMp3(new WaveFileReader("Test.wav"), "Test.mp3", 320000);

            //Try 1: This doesn't work the mp3 file consists only of noise
            MediaFoundationEncoder.EncodeToMp3(new RawSourceWaveStream(_audioStream, _audioWriter.WaveFormat), "Test.mp3", 320000);

            //Try 2: This doesn't work the mp3 file consists only of noise
            using (var reader = new WaveFileReader(_audioStream))
            {
                MediaFoundationEncoder.EncodeToMp3(reader, "Test.mp3", 320000);
            }

            _audioWriter.Close();
            _audioWriter.Dispose();
            _audioWriter = null;
            _audioStream.Close();
            _audioStream.Dispose();
            _audioStream = null;

            GC.Collect();
        }

        private void DataReceived(object sender, WaveInEventArgs e)
        {
            _audioWriter.Write(e.Buffer, 0, e.BytesRecorded);
        }

        public void Start()
        {
            Init();

            _audioCapture.StartRecording();
            IsRecording = true;
        }

        public void Stop()
        {
            _audioCapture.StopRecording();
            IsRecording = false;
        }
    }
}

我找到了解决办法。当我将 WaveFileWriter 的构造函数更改为:

_audioWriter = new WaveFileWriter(_audioStream, _audioCapture.WaveFormat);

然后在 windows 声音设置对话框中将我设备的音频设置更改为:
2 通道,24 位,44100Hz,而不是 2 通道,24 位,96000Hz 它出于某种原因有效...