0

I have a flash mp3 player with a play, pause and next button on one frame.
The songs are stored in an array.

Everything works fine except for the pause button. It pauses the music
and save the song position in a variable. When i click on the pause
button again the song is supposed to play from the saved song position
which it does. The problem is it also play the first song in the array. Not
the song that was paused. I have tried to find a solution on Google. But
the topics i could find where about mp3 players that only played one song.
Here is the code. Thanks.

var i:Number = 0;
var myMusic:Sound = new Sound();
var mySongs:Array = ["Kalimba.mp3","Sleep Away.mp3","Maid with the Flaxen Hair.mp3"];
var soundFile:URLRequest = new URLRequest(mySongs[i++]);
var channel:SoundChannel = new SoundChannel();
var sTransform:SoundTransform = new SoundTransform();
var songPosition:Number;
var myContext:SoundLoaderContext = new SoundLoaderContext(5000);
myMusic.load(soundFile, myContext);

btnPlay.addEventListener(MouseEvent.CLICK, playMusic);
btnNext.addEventListener(MouseEvent.CLICK, nextMusic);
btnPause.addEventListener(MouseEvent.CLICK, pauseMusic);
channel.addEventListener(Event.SOUND_COMPLETE, nextMusic);

function playMusic(evt:MouseEvent):void
{
   channel = myMusic.play(songPosition);
   channel.addEventListener(Event.SOUND_COMPLETE, nextMusic);
}

function nextMusic(evt:Event):void
{
    channel.stop();
    var myMusic:Sound = new Sound();
    var mySongs:Array = ["Kalimba.mp3","Sleep Away.mp3","Maid with the Flaxen Hair.mp3"];
    var soundFile:URLRequest = new URLRequest(mySongs[i]);
    myMusic.load(soundFile, myContext);
    channel = myMusic.play(i);
    channel.addEventListener(Event.SOUND_COMPLETE, nextMusic);
    if(i==mySongs.length-1) {
    i=0;
    }
    else {
        i++;
    }
}

var Paused:Boolean = false;

function pauseMusic(evt:MouseEvent):void
{
    if(Paused==false) {
    songPosition = channel.position;
    channel.stop();
    Paused = true;
    }
    else if(Paused==true) {
        channel = myMusic.play(songPosition);
        Paused = false;
    }
}
Nax
  • 1

1 Answers1

0

You shouldn't keep adding the channel.addEventListener(Event.SOUND_COMPLETE, nextMusic);

Beacuse SOUND_COMPLETE events get fired when you pause.

Adding this eventlistener just once should suffice anyway.

andygoestohollywood
  • 1,285
  • 1
  • 10
  • 14
  • I removed channel.addEventListener(Event.SOUND_COMPLETE, nextMusic); – Nax Oct 07 '13 at 15:10
  • To be sure i removed all the channel.addEventListener(Event.SOUND_COMPLETE, nextMusic); But still no. – Nax Oct 07 '13 at 15:23
  • give songPosition a default value as well, as it isn't defined when you first press play. – andygoestohollywood Oct 07 '13 at 15:27
  • Also not exactly sure why you;re using i everywhere, normally you only do that inside loops.. but u at least should remove channel = myMusic.play(i);.. to channel = myMusic.play(); – andygoestohollywood Oct 07 '13 at 15:34
  • songPosition now have a default value and myMusic.play(i) is now myMusic.play(). – Nax Oct 07 '13 at 15:51
  • I use i as a counter for the array. So when click the next button the i increment and plays the song. – Nax Oct 07 '13 at 15:52
  • I actually think the var i:Number = 0; is the problem – Nax Oct 07 '13 at 15:54
  • I tried to change var i:Number = 0; to var i:Number = 1; When i click on play it plays the second song in the array. – Nax Oct 07 '13 at 15:55
  • When i click on the pause to start the sound again it plays from the position. But it also plays the second song in the array as defined in var i:Number = 1. – Nax Oct 07 '13 at 15:58
  • If i remove var i:Number = 0; the next button doesn't work anymore. There has to be a better way. – Nax Oct 07 '13 at 15:59