PDA

View Full Version : Playing a sound


The Little Guy
08-13-2009, 05:12 AM
For some reason, I can only run this method one time:
public function loadSong(file:String){
var request:URLRequest = new URLRequest("http://www.dudeel.com/process/flash/getAudio.php?file="+file);
soundFactory.addEventListener(Event.COMPLETE, completeHandler);
soundFactory.addEventListener(Event.ID3, id3Handler);
soundFactory.addEventListener(IOErrorEvent.IO_ERRO R, ioErrorHandler);
soundFactory.addEventListener(ProgressEvent.PROGRE SS, progressHandler);
soundFactory.load(request);
song = soundFactory.play();
}

When I try to run it a second time, I get the following error:
Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.
at flash.media::Sound/_load()
at flash.media::Sound/load()
at Audio/loadSong()
at Audio/onTxtClick()

What is the reason for that?

henke37
08-13-2009, 05:17 AM
If soundFactory is a Sound object, the error makes sense, you can't load a sound into the object more than once. Load each sound once and keep the objects separate.

The Little Guy
08-13-2009, 05:25 AM
Can I delete an existing sound then make a new one using the same variable soundFactory?

Mazoonist
08-13-2009, 06:27 AM
Put this line at the beginning of your function:
soundFactory = new Sound();
Re-instantiating the variable like this will give you a fresh start each time.

The Little Guy
08-13-2009, 07:36 AM
OK, doing that fixes my problem, but for some reason, the current playing song doesn't stop, and the new one start up. The function is running, but nothing happens...

henke37
08-13-2009, 09:47 AM
Why would the existing sound stop? You never asked it to! All you did was to load a new sound and begin playback of that.

Mazoonist
08-13-2009, 12:14 PM
The starting and stopping of the sound is controlled by the SoundChannel class. In your file, it's instance is "song." So to stop the sound, it would just be:
song.stop();