我是AS3的新手。我可以用一些帮助。我正在创建一个我喜欢的Flash应用程序来开始自动播放我的(导入的)音乐文件(名为MySound)。我想要一个按钮来选择停止音乐,然后切换选项以再次启动音乐。当音乐播放时,我会喜欢我的mp3循环播放。
我设法让切换和音乐播放工作。但是,单击按钮时音乐才会开始播放。我在使用其他功能时遇到了麻烦。帮助
toggle_btn.stop();
var clickOnce:uint=0;
var aSound:Sound = new MySound();
var aChannel:SoundChannel = new SoundChannel();
toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);
function togglePlay(event:MouseEvent):void {
clickOnce++;
if (clickOnce==1) {
aChannel=aSound.play();
toggle_btn.gotoAndStop(2);
}
if (clickOnce==2) {
SoundMixer.stopAll();
toggle_btn.gotoAndStop(1);
clickOnce=0;
}
}
aChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleted);
function soundCompleted(event:Event):void{
toggle_btn.gotoAndStop(1);
}
答案 0 :(得分:0)
试试这个:
var pP:Number = 0;//playback position
var playing:Boolean = false;
var aSound:Sound = new MySound();
var aChannel:SoundChannel;
toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);
togglePlay(null);
function togglePlay(event:MouseEvent):void {
playing = !playing;
if (playing) playS();
else pauseS();
}
function playS() {
aChannel = aSound.play(pP);
aChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleted);
toggle_btn.gotoAndStop(2);
}
function pauseS() {
pP = aChannel.position;
aChannel.stop();
aChannel.removeEventListener(Event.SOUND_COMPLETE, soundCompleted);
toggle_btn.gotoAndStop(1);
}
function soundCompleted(event:Event):void {
pauseS();
pP = 0;
playS();
}