I have a simple audio player that loads files with a waveform display.
Is there a way to somehow unload or revert the audio reader source to its blank starting state? I’d like a user to be able to clear the song from the the transport so they can’t play it and also clear the audio thumb with a “clear” button.
In the audio tutorial for playing files they have the code as follows to load the file, but you can’t set a readersource to nullptr or it will crash out.
void openButtonClicked()
{
chooser = std::make_unique<juce::FileChooser> ("Select a Wave file to play...",
juce::File{},
"*.wav"); // [7]
auto chooserFlags = juce::FileBrowserComponent::openMode
| juce::FileBrowserComponent::canSelectFiles;
chooser->launchAsync (chooserFlags, [this] (const FileChooser& fc) // [8]
{
auto file = fc.getResult();
if (file != File{}) // [9]
{
auto* reader = formatManager.createReaderFor (file); // [10]
if (reader != nullptr)
{
auto newSource = std::make_unique<juce::AudioFormatReaderSource> (reader, true); // [11]
transportSource.setSource (newSource.get(), 0, nullptr, reader->sampleRate); // [12]
playButton.setEnabled (true); // [13]
readerSource.reset (newSource.release()); // [14]
}
}
});
}