Get track length before it starts playing

Hi everyone,
I’m designing a library where the user can load a track and it shows the title and track length. I am having trouble figuring out how to get the track length before the track actually starts playing. Once it’s playing, I think I can get it from either the AudioThumbnail (getTotalLength) or the AudioTransportSource(getLengthInSeconds). Is there a way to get that information before the track as started playing?
Thanks!

You can use the AudioFormatReader class to get this info. Something like this should work:

juce::File audioFile { "/My/Audio/File" };

juce::AudioFormatManager formatManager;
formatManager.registerBasicFormats();

if (auto reader = formatManager.createReaderFor (audioFile))
{
    auto lengthInSeconds = reader->lengthInSamples / reader->sampleRate;
    // lengthInSeconds now contains the length of the audio file in seconds...
}
2 Likes

That works! Thanks @connorreviere