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;
    }
}
4

1 回答 1

0

您不应该继续添加 channel.addEventListener(Event.SOUND_COMPLETE, nextMusic);

因为暂停时会触发 SOUND_COMPLETE 事件。

无论如何,只添加一次这个事件监听器就足够了。

于 2013-10-07T14:48:31.857 回答