PDA

View Full Version : load new sound into existing sound object


formasfunction
01-18-2008, 04:17 PM
I have an extremely simple AS3 mp3 player that I'm trying to create but the problem I'm running into is that I can't seem to load a new song into my sound object when I switch tracks. Here's the basic code I'm using:


var sound:Sound = new Sound();
var sControl:SoundChannel = new SoundChannel();
var isPlaying:Boolean = false;

function triggerAudio(id:Number, path:String, type:String):void {
if(isPlaying){
sControl.stop();
sound.close();
isPlaying = false;
} else {
sound.load(new URLRequest(path));
sControl = sound.play();
sControl.addEventListener(Event.SOUND_COMPLETE, completeHandler);
isPlaying = true;
}
}


Right now, playing an initial song works
Stopping a song works
But loading a new song over top of a previously loaded song doesn't.

I'm brand new to AS3, what am I missing?

Dom_
01-18-2008, 05:02 PM
If I'm not missing anything, from what I know, in AS3 you create one sound object for one sound, so you should stop the sound channel like you did, and then what I do is set the sound object = null (sound == null);
then before you load a new song, you need to re instanciate the sound Object (sound = new Sound ());
and I'm not sure about this, but i think you can lose the sound.close (); statement

xwielder
01-18-2008, 05:13 PM
var sound:Sound = new Sound ();
var sControl:SoundChannel = new SoundChannel ();
var isPlaying:Boolean = false;

function triggerAudio (id:Number, path:String, type:String):void
{
if (isPlaying)
{
SoundMixer.stopAll ();
}
else
{
SoundMixer.stopAll ();
var s:Sound = new Sound();
s.load (new URLRequest(path));
sound = s;
s.addEventListener (Event.COMPLETE, completeHandler);
isPlaying = true;
}
}

function completeHandler (event:Event)
{
sControl = ss.play ();
s.removeEventListener (Event.COMPLETE, completeHandler);
}

formasfunction
01-18-2008, 05:17 PM
Thanks a bunch, the sound == null solved it. I'd been trying to use delete() previously but that was throwing an error.

As for sound.close(), I'm calling that because of this note about streaming sound:
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000305.html

Though reading it again, it looks like the stop() is unnecessary

[EDIT]
As far as I can tell, it looks like you were right about the close() statement which is confusing to me in regards to that livedoc. With stop() only, it stops fine when the sound has and hasn't finished loading. However, close() causes problems if the sound has completely loaded.

Dom_
01-18-2008, 06:27 PM
Yeah I had the same problem with close (), if the sound is loaded completely, then there is not stream to close, so close is only used to interupt a loading process.