Play Sound File


I'm a beginner and this is my first post here, so apologies if this is a complete newbie "doh" question.

I've been experimenting with the play sound file tutorial on this site, here:

The app I compile works fine the first time I load a WAV file, but the SECOND time I load a WAV the application hangs.

I wonder if it's something to do with the transportSource.setSource reader being assigned a new one each time a WAV is loaded without the previous source being released?

Thanks for any advice!



You have to unload the previous file source and delet it

The code will be the following :

if (chooser.browseForFileToOpen())

            /* Start lines to add */


            transportSource.setSource (nullptr);

            readerSource = nullptr;

           /* End lines to add */

            File file (chooser.getResult());

            AudioFormatReader* reader = formatManager.createReaderFor (file);


            if (reader != nullptr)


                readerSource = new AudioFormatReaderSource (reader, true);

                transportSource.setSource (readerSource, 0, nullptr, reader->sampleRate);

                playButton.setEnabled (true);



The solution is in the full demo.

Thanks Chris,  that both works and makes sense.  Much appreciated.

You say the solution is in the full demo, but I can't find that in any of the included MainComponent_xx.cpp files.   I wonder if I'm missing a demo resource from somewhere else ?  

- Dan

You're welcome.

I found it in the JuceDemo project, AudioPlaybackDemo.cpp in the method loadFileIntoTransport .

I start to know well this project because I'm looking for a way to play N-Audio file at the same time but I can't find any solutions here. So I hope to find an anwser in the demo...