Overview
NAudio has long provided a the .Net community with a robust library to handle audio processing. One of the features that I used recently was the ability to convert a WAVE file into an MP3 without any external libraries provided outside of the NAdudio distribution (like lame). On my first pass I encountered the following error:
System.Runtime.InteropServices.COMException: 'The request is invalid because Shutdown() has been called. (Exception from HRESULT: 0xC00D3E85)'
In order to fix this error I updated my code to call both MediaFoundationApi.Startup()
and MediaFoundationApi.Shutdown()
. Provided below is a snippet of code to accomplish writing a file out. Make an assumption that the "ms" variable is a MemoryStream that has been populated with the bytes of a WAVE file you've read in.
C#
using (var wfr = new WaveFileReader(ms))
{
MediaFoundationApi.Startup();
MediaFoundationEncoder.EncodeToMp3(wfr, @"C:\Temp\Test.mp3", 160000);
MediaFoundationApi.Shutdown();
}