2

I have tried this for HOURS. There must be a simple solution to stop the sound and unload it in as3.

This is not all of my code, but in short I am loading random sounds. I need certian vars to be outside of the function so I can reference them with other functions for a progress bar.

How do I unload a sound so I can load a new one using the same names without getting an error the second time its called?

I have two buttons in this test. A play Sound and a Stop Sound Button.

here is my code:

var TheSound:Sound = new Sound();
var mySoundChannel:SoundChannel = new SoundChannel();

PlayButton.addEventListener(MouseEvent.CLICK, PlaySound);
StopButton.addEventListener(MouseEvent.CLICK, Stopsound);

function PlaySound(e:MouseEvent)
        {
        TheSound.load(new URLRequest("http://www.MyWebsite.com/Noel.mp3"));
        mySoundChannel = TheSound.play(0);
        }

function StopSound(e:MouseEvent)
        {
        delete TheSound;
        }

HERE IS THE ERROR I GET:

Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.
    at flash.media::Sound/_load()
    at flash.media::Sound/load()
    at Untitled_fla::MainTimeline/PlaySound()[Untitled_fla.MainTimeline::frame1:21]

UPDATE.... I TRIED STOPING THE SOUND AND THEN UNLOADING IT AS FOLLOWS

mySoundChannel.stop();
TheSound.close();

BUT NOW I GET THIS ERROR:

Error: Error #2029: This URLStream object does not have a stream opened.
    at flash.media::Sound/close()
    at Untitled_fla::MainTimeline/shut1()[Untitled_fla.MainTimeline::frame1:35]

I believe I am closer. Thanks so much for your help so far.

Papa De Beau
  • 3,744
  • 18
  • 79
  • 137

2 Answers2

3

In order to stop the sound from playing, you first have to tell the SoundChannel instance to stop like so :

mySoundChannel.stop();

Once you did that, you can close the stream used by the sound instance by invoking the close method like so :

TheSound.close();

Also, the delete keyword is rarely used in as3 and you should not it while some methods are trying to access the variable you are deleting. If you want to dispose of the instance that is currently assigned to your TheSound variable, you should set its value to null. This way, flash will properly garbage collect the old Sound instance that is no longer used when it finds the appropriate time to do so.

joelrobichaud
  • 665
  • 4
  • 19
  • hey thanks for the quick help. ok I tried your code but I get a new error. I posted it above. – Papa De Beau Dec 27 '11 at 03:59
  • I believe it should be fine if you just remove the part where you close the stream. – joelrobichaud Dec 27 '11 at 04:15
  • Well it stops the stream but when I click to reload it again it gets the first error mentioned above "Functions called in incorrect sequence, or earlier call was unsuccessful." – Papa De Beau Dec 27 '11 at 04:17
  • Try to assign a new sound instance to the TheSound variable before loading another sound into it. Something like that : TheSound = new Sound(); – joelrobichaud Dec 27 '11 at 04:22
  • That does work. However, then I do not know how to reference TheSound outside of the function. I need it because of a progress bar that follows the progress of it playing. If I call this code again it replaces the one that is called outside of the function and again, I don't know how to access it. – Papa De Beau Dec 27 '11 at 04:30
  • You can still access the newest sound instance in other functions because the TheSound variable is declared outside the scope of the function. Thus, it's also available in your other functions. When you assign a new instance to the variable, it is still the same variable that holds the new instance. – joelrobichaud Dec 27 '11 at 04:36
  • I think that did it. Well there are a few bugs but it was what I was looking for. I was able to call the vars TheSound and mySoundChannel since they were already declared outside the function. Making new ones in the function gave it some bugs but it is working. Thanks so much my friend. Best to you in 2012 – Papa De Beau Dec 27 '11 at 05:14
1

You can initialise the variable outside of the function, but define it as a new Sound object each time the function is called. That way it has global scope, and you can load a new URL at any time.

var TheSound:Sound;
var mySoundChannel:SoundChannel = new SoundChannel();

PlayButton.addEventListener(MouseEvent.CLICK, PlaySound);
StopButton.addEventListener(MouseEvent.CLICK, StopSound);

function PlaySound(e:MouseEvent)
        {
        TheSound = new Sound();
        TheSound.load(new URLRequest("http://www.MyWebsite.com/Noel.mp3"));
        mySoundChannel = TheSound.play(0);
        }

function StopSound(e:MouseEvent)
        {
        mySoundChannel.stop();
        TheSound.close()
        }
Anthony Manning-Franklin
  • 4,408
  • 1
  • 18
  • 23