使用 pause/play 按钮自动播放 (Flash AS3)

Autoplay with pause/play button (Flash AS3)

我是 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);
}

试试这个:

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