Play Sound File


#1

 

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:

http://learn.juce.com/doc/tutorial_playing_sound_files.php

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!

 

 


#2

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.stop();

            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.


#3

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


#4

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...