diff --git a/NAudio/Wave/WaveStreams/MediaFoundationReader.cs b/NAudio/Wave/WaveStreams/MediaFoundationReader.cs index 75cddcb5..8836698e 100644 --- a/NAudio/Wave/WaveStreams/MediaFoundationReader.cs +++ b/NAudio/Wave/WaveStreams/MediaFoundationReader.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; @@ -150,19 +150,27 @@ protected virtual IMFSourceReader CreateReader(MediaFoundationReaderSettings set var currentMediaType = GetCurrentMediaType(reader); - // HE-AAC (and v2) seems to halve the samplerate - if (currentMediaType.SubType == AudioSubtypes.MFAudioFormat_AAC && currentMediaType.ChannelCount == 1) - { - currentMediaType.SampleRate *= 2; - currentMediaType.ChannelCount *= 2; - } // mono, low sample rate files can go wrong on Windows 10 unless we specify here partialMediaType.ChannelCount = currentMediaType.ChannelCount; partialMediaType.SampleRate = currentMediaType.SampleRate; - // 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); + try + { + // 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); + } + catch (COMException ex) when (ex.GetHResult() == MediaFoundationErrors.MF_E_INVALIDMEDIATYPE) + { + // HE-AAC (and v2) seems to halve the samplerate + if (currentMediaType.SubType == AudioSubtypes.MFAudioFormat_AAC && currentMediaType.ChannelCount == 1) + { + partialMediaType.SampleRate = currentMediaType.SampleRate *= 2; + partialMediaType.ChannelCount = currentMediaType.ChannelCount *= 2; + reader.SetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, IntPtr.Zero, partialMediaType.MediaFoundationObject); + } + else { throw; } + } Marshal.ReleaseComObject(currentMediaType.MediaFoundationObject); return reader;