This project has moved and is read-only. For the latest updates, please go here.

NAudio 1.7-alpha MediaFoundationReader

May 30, 2013 at 4:01 PM
Hi, I have the following code to play aac format:
            var reader = new MediaFoundationReader("d:\\Projects\\Samples\\aac.aac");

            var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());
            var outDevice = CmbDevices.SelectedItem as OutDevice;
            if (outDevice != null)
                waveOut.DeviceNumber = outDevice.DeviceId;
            _waveOut = waveOut;
            _waveOut.Init(reader);
            _waveOut.PlaybackStopped += WaveOutOnPlaybackStopped;
            _waveOut.Play();
But song playing only 1 second and after this StopPlayBack event is raised with Excaption. There no such problem with mp3 and Mp3FileReader. Also with WaveCallbackInfo.NewWindows() also everything works fine.

The exception is
Exception = {"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...
I found this post in StackOverflow:
http://stackoverflow.com/questions/1233468/unable-to-cast-com-object-of-type-exception
but seems to me that it isn't the case because IMFSourceReader has ComImport with correct Guid.
Please help me to figure out with this issue.
Thanks, Roman
May 30, 2013 at 4:38 PM
I'd stay away from function callbacks if at all possible. Use WaveOutEvent if you want a background thread for audio playback.
May 31, 2013 at 8:21 AM
Edited May 31, 2013 at 8:22 AM
Thanks Mark,

So, if I understand you correct I need to do the following:

Use this:
var waveOut = new WaveOutEvent();
Instead of this:
var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());
for background thread audio playback, right?
Jun 3, 2013 at 4:05 PM
yes