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

Getting an ISampleProvider for an MP3 in WinRT

Feb 17, 2014 at 10:23 AM
I'm using NAudio 1.7 in a WinRT app. I have a WaveProvider for MP3 files working (using the MediaFoundationReaderRt class from the Win8 sample). I want to calculate the FFT for each sample.

Creating a SampleProvider goes fine:
var sp = wp.ToSampleProvider();
But sp.Read() throws the 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}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Feb 17, 2014 at 12:39 PM
According to
In Windows 8, this interface is extended with IMFSourceReaderEx.

So use IMFSourceReaderEx instead.
Feb 17, 2014 at 3:58 PM
I'm a little surprised you get this error. Is this regular Windows 8 or Windows 8 RT only device (e.g. Surface)
Feb 17, 2014 at 9:05 PM
@markheath Full Win8 PC, I haven't tested on my Surface yet. I'm intending for the app to work on both if at all possible though.

@K24A3 Where would I use IMFSourceReaderEx? I'm guessing this is the relevant code:
protected override IMFSourceReader CreateReader(MediaFoundationReaderSettings settings)
            var fileStream = ((MediaFoundationReaderRtSettings)settings).Stream;
            var byteStream = MediaFoundationApi.CreateByteStream(fileStream);
            var reader = MediaFoundationApi.CreateSourceReaderFromByteStream(byteStream);
            reader.SetStreamSelection(MediaFoundationInterop.MF_SOURCE_READER_ALL_STREAMS, false);
            reader.SetStreamSelection(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true);

            // Create a partial media type indicating that we want uncompressed PCM audio

            var partialMediaType = new MediaType();
            partialMediaType.MajorType = MediaTypes.MFMediaType_Audio;
            partialMediaType.SubType = settings.RequestFloatOutput ? AudioSubtypes.MFAudioFormat_Float : AudioSubtypes.MFAudioFormat_PCM;

            // set the media type
            // can return MF_E_INVALIDMEDIATYPE if not supported
            reader.SetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, IntPtr.Zero, partialMediaType.MediaFoundationObject);
            return reader;
Feb 18, 2014 at 12:22 AM
It appears that IMFSourceReaderEx has not been implemented.

Does the interface work in a Desktop app?