Problem reading mp3 on windows - AudioFormatManager and AudioFormatReader


I’ve got some code that reads from an audio file and puts it into a buffer.

    std::unique_ptr<juce::AudioFormatReader> formatReader(formatManager.createReaderFor(file));
    if(formatReader == nullptr) return false;

This works perfectly for macos, but I’ve been running into an issue on windows.

the program gets to the creation of the “format reader” object and enters “createReaderFor”.

AudioFormatReader* AudioFormatManager::createReaderFor (const File& file)
    // you need to actually register some formats before the manager can
    // use them to open a file!
    jassert (getNumKnownFormats() > 0);

    for (auto* af : knownFormats)
        if (af->canHandleFile (file))
            if (auto in = file.createInputStream())
                //manages to create the input stream
                if (auto* r = af->createReaderFor (in.release(), true))
                    return r; //but fails to arrive here
//so it returns a nullptr
    return nullptr;

inside “af->createReaderFor()”

//im testing with aiff format
AudioFormatReader* AiffAudioFormat::createReaderFor (InputStream* sourceStream, bool deleteStreamIfOpeningFails)
    std::unique_ptr<AiffAudioFormatReader> w (new AiffAudioFormatReader (sourceStream));

    if (w->sampleRate > 0 && w->numChannels > 0)
        return w.release(); // does not reach this point. So the sample rate of input stream is 0 and so is num channels

    if (! deleteStreamIfOpeningFails)
        w->input = nullptr;

    return nullptr;

It looks like it is not reading the file properly, namely the samplerate and num channels. But the num of samples seems to loading fine.
This works perfectly for WAV on windows, but not any other file type. MP3 causes a crash everytime.
Not sure whats going on here. Any Windows weirdness i need to look out for?

UPDATE: I have found out that it is only when i build with CMake that I have these problems. If I build the project directly with visual studio then it works fine. not sure what to make of this.


So when I build on my machine using Visual Studio it all works fine right. i.e. i can load wavs, mp3, aif, whatever. However, when i build using cmake, just the mp3 loading that breaks. I read in the documentation for MP3AudioFormat “use at your own risk” but i am still confused and would like to know if i can fix this.

the reader that gets created for the mp3 file ends up being a “WindowsMediaAudioFormat” instead of “MP3AudioFormat” which is what I expect. how is it the the WindowsMediaFormat gets created instead of the mp3? Also since i am building with cmake i’m unsure how to set the “JUCE_USE_MP3AUDIOFORMAT” definition. I am also unsure if i need to because it works fine on macos without including this definition