views:

206

answers:

3

Hello all. I'm working on a mp3 player and I'm super new at all things flash so there are lots of questions. Currently I'm getting stuck on the track change. My variable declaration look like this:

var index:int = -1;  
var music:Sound = new Sound(new URLRequest("moe2008-05-24d02t02_vbr.mp3"));  
var sc:SoundChannel;  
var isPlaying:Boolean = false;

and my change track function looks like this:

function changeTrack(newTrack){  
    sc.stop();
    isPlaying = false;
    music = new Sound(new URLRequest(newTrack));
    sc = music.play();
    isPlaying = true;
    index++; 
}

Does anyone see any obvious errors??? Thanks

A: 

As Flash Gordon said: "You're actually redefing the local variables when you reset its property."http://www.actionscript.org/forums/archive/index.php3/t-181659.html

This line looks a bit suspicious.

sc = music.play();

Shouldn't that be:

var musicPlay = music.play();
sc = musicPlay;
sorry, that makes no sense... the situation in that thread is very different...
Cay
those are the exact same. in both cases, sc is assigned the result of music.play() which is a SoundChannel reference.
Typeoneerror
A: 

I think you should try to close the Sound connection (Sound.close()) before creating a new one. Also, I would use the same Sound object to load the new file (Sound.load()) to avoid possible GC problems (unless you need to fade in between sounds)...

Cay
A: 

Looks to me like you're missing the part where you actually load the external sound into a new sound object. Your example seems to be reusing the same sound object. Should be something like:

var sound:Sound = new Sound();
var request:URLRequest = new URLRequest("path/to/your/sound");

sound.load(request);

sc = sound.play();

You need the local sound variable to create a new sound as another sound instance cannot be loaded into an existing one:

Once load() is called on a Sound object, you can't later load a different sound file into that Sound object. To load a different sound file, create a new Sound object.

You'll probably want to use a Dictionary to keep track of which Sounds are loaded already. So when this method is called, you check if a sound object is registered in the dictionary and if it is, play that instead of loading a file.

Typeoneerror