C# WPF window 调整大小阻塞 NAudio WaveIn / WaveOut 流

C# WPF window resize is blocking NAudio WaveIn / WaveOut stream

我正在使用 NAudio 和 WPF 在 C# 中开发混音器软件。

但我遇到了一个非通信行为:输出正常播放,但当我调整 MainWindow 大小时,输出音频开始分裂。如果保持 window 调整大小,缓冲区已满并抛出异常。看起来调整大小事件正在阻塞主线程。

有人有什么建议吗?

_wavein = new WaveIn();
_wavein.DeviceNumber = 0;
_wavein.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(0).Channels);
    
waveInProvider = new WaveInProvider(_wavein);
    
volumeSampleProvider = new VolumeSampleProvider(waveInProvider.ToSampleProvider());
volumeSampleProvider.Volume = (float)slider.Value;
    
MeteringSampleProvider meteringSampleProvider = new MeteringSampleProvider(volumeSampleProvider, 1000);
meteringSampleProvider.StreamVolume += meteringSampleProviderStreamVolume;
    
_waveOut = new WaveOut();
_waveOut.DesiredLatency = 50;
_waveOut.Init(meteringSampleProvider.ToWaveProvider());

_wavein.StartRecording();
_waveOut.Play();

如果您使用 WaveOutEventWaveOutEvent,音频处理将在后台线程上进行,这应该有很大帮助