0

当我使用ResamplerDmoStream并选择除WaveOutput之外的任何内容(例如WASAPIDirectSound)时,我遇到以下异常:无法将 COM 对象NAudio.DMO.ResamplerMediaComObject转换为接口类型Naudio.Dmo.IMediaObject...

异常调用堆栈:

at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
at NAudio.Dmo.IMediaObject.GetInputStatus(Int32 inputStreamIndex, DmoInputStatusFlags& flags)
at NAudio.Dmo.MediaObject.IsAcceptingData(Int32 inputStreamIndex) in NAudio\Dmo\MediaObject.cs:line 468
at NAudio.Wave.ResamplerDmoStream.Read(Byte[] buffer, Int32 offset, Int32 count) in NAudio\Wave\WaveStreams\ResamplerDmoStream.cs:line 136
at NAudio.Wave.Wave16ToFloatProvider.Read(Byte[] destBuffer, Int32 offset, Int32 numBytes) in NAudio\Wave\WaveProviders\Wave16toFloatProvider.cs:line 47
at NAudio.Wave.MixingWaveProvider32.Read(Byte[] buffer, Int32 offset, Int32 count) in NAudio\Wave\WaveProviders\MixingWaveProvider32.cs:line 116
at NAudio.Wave.SampleProviders.WaveToSampleProvider.Read(Single[] buffer, Int32 offset, Int32 count) in NAudio\Wave\SampleProviders\WaveToSampleProvider.cs:line 33
at NAudio.Wave.SampleProviders.MeteringSampleProvider.Read(Single[] buffer, Int32 offset, Int32 count) in NAudio\Wave\SampleProviders\MeteringSampleProvider.cs:line 72
at NAudio.Wave.SampleProviders.VolumeSampleProvider.Read(Single[] buffer, Int32 offset, Int32 sampleCount) in NAudio\Wave\SampleProviders\VolumeSampleProvider.cs:line 42
at NAudio.Wave.SampleProviders.SampleChannel.Read(Single[] buffer, Int32 offset, Int32 sampleCount) in NAudio\Wave\SampleProviders\SampleChannel.cs:line 58
at NAudio.Wave.SampleProviders.MeteringSampleProvider.Read(Single[] buffer, Int32 offset, Int32 count) in NAudio\Wave\SampleProviders\MeteringSampleProvider.cs:line 72
at NAudio.Wave.SampleProviders.MultiplexingSampleProvider.Read(Single[] buffer, Int32 offset, Int32 count) in NAudio\Wave\SampleProviders\MultiplexingSampleProvider.cs:line 93
at NAudio.Wave.SampleProviders.SampleToWaveProvider.Read(Byte[] buffer, Int32 offset, Int32 count) in NAudio\Wave\SampleProviders\SampleToWaveProvider.cs:line 35
at NAudio.Wave.DirectSoundOut.Feed(Int32 bytesToCopy) in NAudio\Wave\WaveOutputs\DirectSoundOut.cs:line 516
at NAudio.Wave.DirectSoundOut.PlaybackThreadFunc() in NAudio\Wave\WaveOutputs\DirectSoundOut.cs:line 415
4

1 回答 1

2

问题很可能是您在 STAThread(如果您使用 WinForms 或 WPF)上创建 COM 对象(在本例中为 DMO 重采样器),然后将它们传递给将尝试访问的 NAudio 输出驱动程序模型他们在后台线程上,这是不允许的。

这是一个非常烦人的问题,因为如果您将 GUI 线程设置为 MTAThread,其他东西会中断(例如打开文件对话框)。您的主要选择如下:

  1. 坚持使用具有窗口回调的 WaveOut。您还可以使用WasapiOutGuiThread,这是我最近添加到演示项目中的一个实验类。这是迄今为止最简单的解决方案。您真的需要使用 DirectSound 吗?
  2. 音频管道中没有 ResamplerDmoStream。改用 WaveFormatConversionStream,或者简单地让 WASAPI 插入一个(它会自动执行)
  3. 创建一个用于音频播放的后台线程,并从您的 GUI 线程向它发送消息,告诉它您想要开始、停止、重新定位等。不幸的是,这是一个相当复杂的选项。
于 2012-11-27T09:35:06.383 回答