In my C# program, I have a bunch of mp3 files I need to use, but they have different SampleRates, so I want to convert them to a common one. From this example, I created the following code. I want to use a MemoryStream instead of creating a physical file:
ISampleProvider sampleProvider = reader.ToSampleProvider();
var x = new SampleToWaveProvider(sampleProvider);
using (var resampled = new MediaFoundationResampler(x, targetWaveFormat))
{
Stream stream = new MemoryStream();
MediaFoundationEncoder.EncodeToMp3(resampled, stream, 0);
convertedMP3 = new Mp3FileReader(stream);
}
When I use a file instead of the stream it works, but with the stream, new Mp3FileReader(stream)
throws the exception "Invalid MP3 file - no MP3 Frames Detected". Both, EncodeToMp3 and Mp3FileReader support "Stream" as parameter.