Grainy, distort audio playback with MemoryBlock

I tried to play an audio file with AudioFormatReader created from MemoryBlock but the output sound is pretty grainy and even stop after a few seconds sometimes:

void AudioEngine::play(const juce::File &audioToPlay) {
    MemoryBlock mb;
    audioToPlay.createInputStream()->readIntoMemoryBlock (mb);
    auto reader = rawToUniquePtr (formatManager.createReaderFor (std::make_unique<MemoryInputStream> (mb, false)));
    // set the source and play the audio
}

While create the reader directly with the file doesn’t have any of those glitch sounds. Have i done it wrong? Appreciate any help

P/s: I have reason to not create the reader directly with audioToPlay file, pretty much a platform specific error

Perhaps createReaderFor is returning a reader for the wrong file format. You could try creating an MP3AudioFormat instance and calling MP3AudioFormat::createReaderFor, instead of going through the audio format manager.

1 Like