naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.38k stars 1.09k forks source link

'Unable to cast COM object of type 'System.__ComObject' to interface type 'NAudio.MediaFoundation.IMFSourceReader'. #1076

Open MichaelSvejcar opened 9 months ago

MichaelSvejcar commented 9 months ago

Hello, in my GUI app I am implementing audio player using NAudio. The way it is working now is that I am creating AudioFileReader, which I then pass into VolumeSampleProvider constructor as a source ISampleProvider. Then I initialize WaveOut, using this VolumeSampleProvider as an argument to the WaveOut constructor. The reason I do this is that I need the functionality to change volume during the playback, sometimes to values larger than 1 (if, for example, the maximum peak of the input audio file is not normalized to the max value, so I basically normalize the samples in real time..) In my app, I have a simple progressbar, and when user clicks or drags it to some position during the playback, it first calls WaveOut.Stop, then change AudioFileReader.Position accordingly and then call WaveOut.Play() again. However, while during this, I keep getting this error - sometimes less, sometimes more often. What is also odd is that this error never occurs while dragging the progressbar while playback is paused or stopped (for which I am using the same WaveOut.Pause() and WaveOut.Stop() methods, similar as in the Position changing method..) What can be causing this issue?

System.InvalidCastException
  HResult=0x80004002
  Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'NAudio.MediaFoundation.IMFSourceReader'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{70AE66F2-C809-4E4F-8915-BDCB406B7993}' failed due to the following error: Neznámé rozhraní (0x80004002 (E_NOINTERFACE)).
  Source=System.Private.CoreLib
  StackTrace:
   at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
   at NAudio.MediaFoundation.IMFSourceReader.SetCurrentPosition(Guid guidTimeFormat, IntPtr varPosition)
   at NAudio.Wave.MediaFoundationReader.Reposition(Int64 desiredPosition) in NAudio.Wave\MediaFoundationReader.cs:line 264
   at NAudio.Wave.MediaFoundationReader.Read(Byte[] buffer, Int32 offset, Int32 count) in NAudio.Wave\MediaFoundationReader.cs:line 208
   at NAudio.Wave.SampleProviders.Pcm16BitToSampleProvider.Read(Single[] buffer, Int32 offset, Int32 count) in NAudio.Wave.SampleProviders\Pcm16BitToSampleProvider.cs:line 16
   at NAudio.Wave.SampleProviders.MeteringSampleProvider.Read(Single[] buffer, Int32 offset, Int32 count) in NAudio.Wave.SampleProviders\MeteringSampleProvider.cs:line 42
   at NAudio.Wave.SampleProviders.VolumeSampleProvider.Read(Single[] buffer, Int32 offset, Int32 sampleCount) in NAudio.Wave.SampleProviders\VolumeSampleProvider.cs:line 19
   at NAudio.Wave.AudioFileReader.Read(Single[] buffer, Int32 offset, Int32 count) in NAudio.Wave\AudioFileReader.cs:line 107
   at NAudio.Wave.AudioFileReader.Read(Byte[] buffer, Int32 offset, Int32 count) in NAudio.Wave\AudioFileReader.cs:line 99
   at NAudio.Wave.SampleProviders.WaveToSampleProvider.Read(Single[] buffer, Int32 offset, Int32 count) in NAudio.Wave.SampleProviders\WaveToSampleProvider.cs:line 21
   at NAudio.Wave.SampleProviders.VolumeSampleProvider.Read(Single[] buffer, Int32 offset, Int32 sampleCount) in NAudio.Wave.SampleProviders\VolumeSampleProvider.cs:line 19
   at NAudio.Wave.SampleProviders.SampleToWaveProvider.Read(Byte[] buffer, Int32 offset, Int32 count) in NAudio.Wave.SampleProviders\SampleToWaveProvider.cs:line 24
   at NAudio.Wave.WaveOutBuffer.OnDone() in NAudio.Wave\WaveOutBuffer.cs:line 91
   at NAudio.Wave.WaveOut.EnqueueBuffers() in NAudio.Wave\WaveOut.cs:line 122
   at NAudio.Wave.WaveOut.Play() in NAudio.Wave\WaveOut.cs:line 111
   at DropNormalizer.AudioPlayer.set_PositionInPercentage(Single value) in C:\CSharp\DropNormalizer\DropNormalizer\AudioPlayer\AudioPlayer.cs:line 184
   at DropNormalizerGUI_Avalonia.Views.MainWindow.PlayerProgressBar_PointerPressed(Object sender, PointerPressedEventArgs e) in C:\CSharp\DropNormalizer\DropNormalizerGUI_Avalonia\Views\MainWindow.axaml.cs:line 253
   at Avalonia.Interactivity.EventRoute.RaiseEventImpl(RoutedEventArgs e) in /_/src/Avalonia.Interactivity/EventRoute.cs:line 152
   at Avalonia.Interactivity.EventRoute.RaiseEvent(IInteractive source, RoutedEventArgs e) in /_/src/Avalonia.Interactivity/EventRoute.cs:line 102
   at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e) in /_/src/Avalonia.Interactivity/Interactive.cs:line 122
   at Avalonia.Input.MouseDevice.MouseDown(IMouseDevice device, UInt64 timestamp, IInputElement root, Point p, PointerPointProperties properties, KeyModifiers inputModifiers) in /_/src/Avalonia.Input/MouseDevice.cs:line 263
   at Avalonia.Input.MouseDevice.ProcessRawEvent(RawPointerEventArgs e) in /_/src/Avalonia.Input/MouseDevice.cs:line 157
   at Avalonia.Input.InputManager.ProcessInput(RawInputEventArgs e) in /_/src/Avalonia.Input/InputManager.cs:line 36
   at Avalonia.Win32.WindowImpl.AppWndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) in /_/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs:line 526
   at Avalonia.Win32.WindowImpl.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) in /_/src/Windows/Avalonia.Win32/WindowImpl.WndProc.cs:line 33
   at Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg)
   at Avalonia.Win32.Win32Platform.RunLoop(CancellationToken cancellationToken) in /_/src/Windows/Avalonia.Win32/Win32Platform.cs:line 206
   at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken) in /_/src/Avalonia.Base/Threading/Dispatcher.cs:line 66
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 121
   at DropNormalizerGUI_Avalonia.Program.Main(String[] args) in C:\CSharp\DropNormalizer\DropNormalizerGUI_Avalonia\Program.cs:line 15
markheath commented 8 months ago

Often casting issues are to do with creating a COM object on one thread, and then trying to access it from a different thread. Try to keep access to a single thread if possible and see if that helps.