I have COM object casting problem while using naudio's wavefilewriter class. It seems to be working at first, but after playing the audio file using Outputdevice.Play(), it leads to empty wav file with System.InvalidCastException.
In short, WavefileWriter works before playing audio, but it doesn't after playing audio.
The code below is from a custom class I made to set the drivers in one hand. It consists of WaveOut, WASAPIOut, and AsioOut.
```c#
// This part is about Play, Stop, Pause, and Dispose
public void Play(int drivernum)
{
if (drivernum == 0)
{
waveOut.Play();
}
else if (drivernum == 1)
{
wasapiOut.Play();
}
else if (drivernum == 2)
{
asioOut.Play();
}
else
{
//some error message
}
}
public void Stop(int drivernum)
{
if (drivernum == 0)
{
waveOut.Stop();
AudioPlayer.MainMixer.Position = 0;
}
else if (drivernum == 1)
{
wasapiOut.Stop();
AudioPlayer.MainMixer.Position = 0;
}
else if (drivernum == 2)
{
asioOut.Stop();
AudioPlayer.MainMixer.Position = 0;
}
else
{
//some error message
}
}
public void Pause(int drivernum)
{
if (drivernum == 0)
{
waveOut.Pause();
}
else if (drivernum == 1)
{
wasapiOut.Pause();
}
else if (drivernum == 2)
{
asioOut.Pause();
}
else
{
//some error message
}
}
public void Dispose(int drivernum)
{
if (drivernum == 0)
{
if (waveOut != null)
{
waveOut.Dispose();
waveOut = null;
}
}
else if (drivernum == 1)
{
if (wasapiOut != null)
{
wasapiOut.Dispose();
wasapiOut = null;
}
}
else if (drivernum == 2)
{
if (asioOut != null)
{
asioOut.Dispose();
asioOut = null;
}
}
else
{
//some error message
}
}
// This part is about writing wav files.
public void Write(int drivernum, string OutFile, float startpoint, float endpoint)
{
//byte[] buffer = new byte[8820];
//WaveFileWriter waveFileWriter = null;
if (PlaybackState(drivernum) == NAudio.Wave.PlaybackState.Playing)
{
// Show "Audio Will Stop, Proceed?" Message box, and if okay, then go to next line
Stop(drivernum);
}
try
{
AudioPlayer.MainMixer.Position = 0;
Dispose(drivernum);
// error only occurs when the files have been played no matter what device it is
WaveFileWriter.CreateWaveFile(OutFile, AudioPlayer.MainMixer.Take(TimeSpan.FromMilliseconds(endpoint - startpoint)).ToWaveProvider());
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
```
After trying to write wav file after playing from the source, error message like below showed up.
System.InvalidCastException: 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: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
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)
at NAudio.Wave.MediaFoundationReader.Read(Byte[] buffer, Int32 offset, Int32 count)
at NAudio.Wave.SampleProviders.Mono16SampleChunkConverter.LoadNextChunk(IWaveProvider source, Int32 samplePairsRequired)
at NAudio.Wave.WaveChannel32.Read(Byte[] destBuffer, Int32 offset, Int32 numBytes)
User contributions licensed under CC BY-SA 3.0