Hello,
I’m exploring using a background thread for downloading audio data. So far all the downloading is working, and I’ve created an object which handles the download which inherits from the Thread class and implements the run() method. I then have a button on my UI which calls ->startThread(); on the object. However, it doesn’t seem to be doing the downloading on a separate thread as the UI still hangs until the download completes.
Here is my run method:
void ThreadStream::run()
{
while (! threadShouldExit())
{
// because this is a background thread, we mustn't do any UI work without
// first grabbing a MessageManagerLock..
const MessageManagerLock mml (Thread::getCurrentThread());
if (! mml.lockWasGained()) // if something is trying to kill this job, the lock
return; // will fail, in which case we'd better return..
URL theURL("https://jakemumu.github.io/HH.wav");
audioInputStream = theURL.createInputStream(false);
AudioFormatReader *reader = formatManager.createReaderFor(audioInputStream);
if (reader != nullptr)
{
fileLoaded = true;
downloadBuffer.setSize(reader->numChannels, reader->lengthInSamples);
reader->read(&downloadBuffer, 0, reader->lengthInSamples, 0, true, true);
}
signalThreadShouldExit();
}
}
Sorry if this is a bit naive as i’ve not got a lot of experience with threads nor MessageManagerLocks
Thanks for any replies!