this is a example.
Only Procedure modified
privatestring _file = "";private IMFSourceReader pReaderTh;privatebool MFStart = false; // Change pReader = Init() public MediaFoundationReader(string file) { _file = file; pReader = Init(); length = GetLength(); }// Init MediaFoundation private IMFSourceReader Init() { IMFSourceReader retReader;//if (!initialized)//{// once only per app - TODO, maybe move this elsewhere MediaFoundationInterop.MFStartup(MediaFoundationInterop.MF_VERSION);// initialized = true;//}var uri = new Uri(_file); MediaFoundationInterop.MFCreateSourceReaderFromURL(uri.AbsoluteUri, IntPtr.Zero, out retReader); retReader.SetStreamSelection(MediaFoundationInterop.MF_SOURCE_READER_ALL_STREAMS, false); retReader.SetStreamSelection(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true);/*IMFMediaType currentMediaType; pReader.GetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, out currentMediaType); Guid currentMajorType; currentMediaType.GetMajorType(out currentMajorType); IMFMediaType nativeMediaType; pReader.GetNativeMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, out nativeMediaType);*/// Create a partial media type indicating that we want uncompressed PCM audio IMFMediaType partialMediaType = null; MediaFoundationInterop.MFCreateMediaType(ref partialMediaType); partialMediaType.SetGUID(MediaFoundationInterop.MF_MT_MAJOR_TYPE, MediaFoundationInterop.MFMediaType_Audio); partialMediaType.SetGUID(MediaFoundationInterop.MF_MT_SUBTYPE, MediaFoundationInterop.MFAudioFormat_PCM);// set the media type retReader.SetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, IntPtr.Zero, partialMediaType); Marshal.ReleaseComObject(partialMediaType);// now let's find out what we actually got IMFMediaType uncompressedMediaType; retReader.GetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, out uncompressedMediaType);// Two ways to query it, first is to ask for properties (section is to convet into WaveFormatEx using MFCreateWaveFormatExFromMFMediaType) Guid actualMajorType; uncompressedMediaType.GetGUID(MediaFoundationInterop.MF_MT_MAJOR_TYPE, out actualMajorType); Debug.Assert(actualMajorType == MediaFoundationInterop.MFMediaType_Audio); Guid audioSubType; uncompressedMediaType.GetGUID(MediaFoundationInterop.MF_MT_SUBTYPE, out audioSubType); Debug.Assert(audioSubType == MediaFoundationInterop.MFAudioFormat_PCM);int channels; uncompressedMediaType.GetUINT32(MediaFoundationInterop.MF_MT_AUDIO_NUM_CHANNELS, out channels);int bits; uncompressedMediaType.GetUINT32(MediaFoundationInterop.MF_MT_AUDIO_BITS_PER_SAMPLE, out bits);int sampleRate; uncompressedMediaType.GetUINT32(MediaFoundationInterop.MF_MT_AUDIO_SAMPLES_PER_SECOND, out sampleRate); waveFormat = new WaveFormat(sampleRate, bits, channels); retReader.SetStreamSelection(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true);return retReader; }///<summary>/// Reads from this wave stream///</summary>///<param name="buffer">Buffer to read into</param>///<param name="offset">Offset in buffer</param>///<param name="count">Bytes required</param>///<returns>Number of bytes read; 0 indicates end of stream</returns>publicoverrideint Read(byte[] buffer, int offset, int count) { // Here Init MF if (MFStart == false) { pReaderTh = Init();var pv = PropVariant.FromLong(position); pReaderTh.SetCurrentPosition(Guid.Empty, ref pv); MFStart = true; }int bytesWritten = 0;// read in any leftovers from last timeif (decoderOutputCount > 0) { bytesWritten += ReadFromDecoderBuffer(buffer, offset, count - bytesWritten); }while (bytesWritten < count) { IMFSample pSample;int dwFlags;ulong timestamp;int actualStreamIndex; pReaderTh.ReadSample(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, out actualStreamIndex, out dwFlags, out timestamp, out pSample);if (dwFlags != 0) {// reached the end of the stream or media type changedbreak; }/* if (dwFlags & MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED) { printf("Type change - not supported by WAVE file format.\n"); break; } if (dwFlags & MF_SOURCE_READERF_ENDOFSTREAM) { printf("End of input file.\n"); break; }*/ IMFMediaBuffer pBuffer; pSample.ConvertToContiguousBuffer(out pBuffer); IntPtr pAudioData = IntPtr.Zero;int cbBuffer;int pcbMaxLength; pBuffer.Lock(out pAudioData, out pcbMaxLength, out cbBuffer); EnsureBuffer(cbBuffer); Marshal.Copy(pAudioData, decoderOutputBuffer, 0, cbBuffer); decoderOutputOffset = 0; decoderOutputCount = cbBuffer; bytesWritten += ReadFromDecoderBuffer(buffer, offset + bytesWritten, count - bytesWritten); pBuffer.Unlock(); Marshal.ReleaseComObject(pBuffer); Marshal.ReleaseComObject(pSample); } position += bytesWritten; return bytesWritten; }







