2013-10-07 85 views
0

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

一切正常,除了 暂停按钮。它暂停音乐
并将歌曲位置保存在变量中。 当我再次点击暂停
按钮时,歌曲应该从已保存的歌曲位置
播放 。问题在于它也是 播放数组中的第一首歌曲。不是
已暂停的歌曲。我试过 在Google上寻找解决方案。但
我可以找到哪些关于只播放一首歌曲的MP3播放器的主题。
这是代码。谢谢。AS3暂停按钮不播放阵列中暂停的歌曲

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

回答

0

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

当您暂停时,会导致发生SOUND_COMPLETE事件。

无论如何,只需添加此eventlistener就足够了。

+0

我删除了channel.addEventListener(Event.SOUND_COMPLETE,nextMusic); – Nax

+0

不幸的是,它并没有解决暂停问题。 – Nax

+0

为了确保我删除了所有的channel.addEventListener(Event.SOUND_COMPLETE,nextMusic);但仍然没有。 – Nax