Issues with FlxG.sound.playMusic

318 views
Skip to first unread message

Justin Bell

unread,
Jul 25, 2015, 8:51:53 PM7/25/15
to HaxeFlixel
So I'm trying to basically play a queue of songs for the music. Here is my setup:

-----In a method somewhere-----
for (song in musicList) {
soundManager.addToMusicQueue(song);
}
---------------------
class SoundManager
{
private var musicQueue:Array<String>;

public function addToMusicQueue(songPath:String):Void {
if (FlxG.sound.music == null || !FlxG.sound.music.playing) {
playMusic(songPath);
} else {
musicQueue.push(songPath);
}
}

public function playMusic(songPath:String):Void {
FlxG.sound.playMusic(songPath,1,false);
FlxG.sound.music.onComplete = musicEnd;
}

private function musicEnd():Void {
if (musicQueue.length > 0) {
var nextSong = musicQueue.shift();
playMusic(nextSong);
}
}
}

All of this gets called correctly, but after the first song ends, the next song never starts playing. my playMusic method is called, and the musicQueue is cycling through as I'd expect.

What am I doing wrong? Is there an easier way to handle the music?

(General info, I'm targeting CPP, using HaxeFlixel/OpenFL)

Justin Bell

unread,
Jul 30, 2015, 12:06:43 PM7/30/15
to HaxeFlixel, 4just...@gmail.com
Anyone have any ideas on this? I feel like I'm doing sound wrong, but I can't figure out how the proper way to handle it is.

Seb Jones

unread,
Jul 30, 2015, 2:57:04 PM7/30/15
to HaxeFlixel, 4just...@gmail.com
I tried running your code to recreate the problem and indeed it didn't work. I think I managed to fix it by setting the music object to null before starting the next song, like so:

private function musicEnd():Void {
if (musicQueue.length > 0) {
FlxG.sound.music = null;
var nextSong = musicQueue.shift();
playMusic(nextSong);
}
}

It seems that for some reason the FlxG.sound.playMusic function won't work if there is already music playing, and for some reason FlxG.sound.music.playing was still set to true when onComplete was called.

Also, in the code you provided you missed out constructing the array. I'm assuming you just did that for the sake of reducing clutter, but just in case, thought I would mention it too.

Hope this helps! :)

Justin Bell

unread,
Jul 30, 2015, 9:14:29 PM7/30/15
to HaxeFlixel, thereal...@gmail.com
Huh, interesting that does fix it. Seems like odd behavior.

And you were correct, I was just reducing clutter. :)

Thanks a bunch!
Reply all
Reply to author
Forward
0 new messages