AS3暂停按钮不会播放来自阵列的暂停歌曲

时间:2013-10-07 13:20:43

标签: arrays actionscript-3 flash button soundchannel

我有一个flash MP3播放器,在一帧上有播放,暂停和下一个按钮 歌曲存储在一个数组中。

除了。之外,一切都很好 暂停按钮。它会暂停音乐并将歌曲位置保存在变量中。 当我再次点击暂停
按钮时,应播放该歌曲 从保存的歌曲位置确定它。问题是它也是 播放阵列中的第一首歌。不是那首被停顿的歌。我试过了 在Google上找到解决方案。但是我可以找到关于只播放一首歌的mp3播放器的话题 这是代码。谢谢。

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

1 个答案:

答案 0 :(得分:0)

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

暂停时会激活SOUND_COMPLETE事件。

只需添加一次这个eventlistener就足够了。