# Play Sound File

**URL:** <https://forum.juce.com/t/play-sound-file/15384>\
**Category:** General JUCE discussion\
**Created:** [August 21, 2015, 8:03pm UTC](https://forum.juce.com/t/play-sound-file/15384 "2015-08-21T20:03:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![polypx](https://avatars.discourse-cdn.com/v4/letter/p/a183cd/32.png) [@polypx](https://forum.juce.com/u/polypx)\
**Post date:** [August 21, 2015, 8:03pm UTC](https://forum.juce.com/t/play-sound-file/15384/1 "2015-08-21T20:03:04Z")

</div>

&nbsp;

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!

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![ChrisNilson](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@ChrisNilson](https://forum.juce.com/u/ChrisNilson)\
**Post date:** [August 22, 2015, 12:51pm UTC](https://forum.juce.com/t/play-sound-file/15384/2 "2015-08-22T12:51:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![polypx](https://avatars.discourse-cdn.com/v4/letter/p/a183cd/32.png) [@polypx](https://forum.juce.com/u/polypx)\
**Post date:** [August 22, 2015, 2:09pm UTC](https://forum.juce.com/t/play-sound-file/15384/3 "2015-08-22T14:09:51Z")

</div>

Thanks Chris,&nbsp; that both works and makes sense.&nbsp; 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.&nbsp;&nbsp; I wonder if I'm missing a demo resource from somewhere else ?&nbsp;&nbsp;

- Dan

---

<div class="post-metadata">

**Author:** ![ChrisNilson](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@ChrisNilson](https://forum.juce.com/u/ChrisNilson)\
**Post date:** [August 22, 2015, 2:19pm UTC](https://forum.juce.com/t/play-sound-file/15384/4 "2015-08-22T14:19:26Z")

</div>

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