Streaming audio player


#1

Any idea why this doesn’t play the file from the beginning? It seems to only want to start half way through. The URL is an mp3.

MainContentComponent::MainContentComponent()
{
    setSize (600, 400);
    audioDeviceManager.initialiseWithDefaultDevices(0, 2);

    AudioFormatManager audioFormatManager;
    audioFormatManager.registerBasicFormats();

    auto inputStream = url.createInputStream(false);
    auto bufferedInputStream = new BufferedInputStream(inputStream, 1024 * 128, true); // required otherwise createReaderFor doesn't work!

    auto reader = audioFormatManager.createReaderFor(bufferedInputStream);
    readerSource = new AudioFormatReaderSource(reader, true);

    transportSource = new AudioTransportSource();
    transportSource->setSource(readerSource);

    startTimer(2000);

    player.setSource(transportSource);
    audioDeviceManager.addAudioCallback(&player);
}

void MainContentComponent::timerCallback ()
{
    transportSource->setPosition(0);
    transportSource->start();
}

#2

How far it starts from the beginning depends on the size of the BufferInputStream buffer.


#3

It’s probably something to do with seeking - URL streams aren’t so great at seeking, and to parse an MP3 a lot of codecs will try to read ahead to find out its length and layout before seeking back and starting.


#4

Ah right, so the seek instructions are getting back to the URL object. Maybe I can pop a layer in that buffers and prevents this somehow… will look