Skip to content

Instantly share code, notes, and snippets.

@BrainCrumbz
Last active August 29, 2015 14:16
Show Gist options
  • Save BrainCrumbz/091de69fbbec6ad5fcd6 to your computer and use it in GitHub Desktop.
Save BrainCrumbz/091de69fbbec6ad5fcd6 to your computer and use it in GitHub Desktop.
Join WAV files into a WMA file with NAudio - With COMException (see fork for fixes)
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in WavToWma.Presentation.exe
Additional information: Eccezione da HRESULT: 0xC00D36B0
in NAudio.MediaFoundation.MediaFoundationInterop.MFCreateSinkWriterFromURL(String pwszOutputURL, IMFByteStream pByteStream, IMFAttributes pAttributes, IMFSinkWriter& ppSinkWriter)
in NAudio.Wave.MediaFoundationEncoder.CreateSinkWriter(String outputFile)
in NAudio.Wave.MediaFoundationEncoder.Encode(String outputFile, IWaveProvider inputProvider)
in NAudio.Wave.MediaFoundationEncoder.EncodeToWma(IWaveProvider inputProvider, String outputFile, Int32 desiredBitRate)
... <the line invoking MediaFoundationEncoder.EncodeToWma> ...
class JoiningWaveProvider : IWaveProvider
{
public JoiningWaveProvider(IEnumerable<WaveFileReader> inputFileReaders)
{
if (! inputFileReaders.Any())
{
throw new ArgumentException("At least one input file currentReader is needed");
}
WaveFormat firstFormat = inputFileReaders.First().WaveFormat;
IEnumerable<WaveFileReader> nonMatchingReaders =
inputFileReaders.Where(reader => !reader.WaveFormat.Equals(firstFormat));
if (nonMatchingReaders.Any())
{
throw new ArgumentException("All readers must have the same WAV format");
}
WaveFormat = firstFormat;
_inputFileReaders = inputFileReaders.ToList();
_readIndex = 0;
}
public WaveFormat WaveFormat { get; private set; }
public int Read(byte[] buffer, int offset, int count)
{
int currentOffset = offset;
int totalRead = 0;
int remaining = count;
for (int i = _readIndex; i < _inputFileReaders.Count && remaining > 0; i++, _readIndex++)
{
WaveFileReader currentReader = _inputFileReaders[i];
int readNow = currentReader.Read(buffer, currentOffset, remaining);
currentOffset += readNow;
totalRead += readNow;
remaining -= readNow;
}
return totalRead;
}
private int _readIndex;
private readonly List<WaveFileReader> _inputFileReaders;
}
// ...
List<WaveFileReader> waveFileReaders = BuildTheReaderListFromPathNames();
IWaveProvider joinedWaveProvider = buildProvider(waveFileReaders);
// This one causes Exception
MediaFoundationEncoder.EncodeToWma(joinedWaveProvider, _joinedOutputFile, 48000);
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment