萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 軟體知識 >  >> 影像處理 >> Flash 教學

Flash AS3 控制聲音播放/暫停完整代碼範例

針對 AS3 聲音控制常見需求,提供完整可執行的類別代碼,包含載入進度監聽、ID3 讀取、播放進度計時、播放/暫停按鈕邏輯。

完整代碼 (As3Sound.as)

package {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.utils.Timer;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.filters.DropShadowFilter;

    public class As3Sound extends Sprite {
        private var url:String = "https://XXX.com/music/XXX.mp3";
        private var soundFactory:Sound;
        private var channel:SoundChannel;
        private var positionTimer:Timer;
        private var play_btn:Sprite;
        private var stop_btn:Sprite;
        private var d_filters:DropShadowFilter = new DropShadowFilter(5,45,0x000000,0.8,8,8);
        private var bSoundStop:Boolean = false; // 記錄是否暫停

        public function As3Sound() {
            // 標題文字
            var sxl_txt:TextField = new TextField();
            sxl_txt.text = "CS4中如何控制聲音的播放或暫停";
            sxl_txt.autoSize = TextFieldAutoSize.LEFT;
            sxl_txt.x = stage.stageWidth/2 - sxl_txt.width/2;
            sxl_txt.y = 20;
            addChild(sxl_txt);

            // 載入音樂
            var mp3_request:URLRequest = new URLRequest(url);
            soundFactory = new Sound();
            soundFactory.addEventListener(Event.COMPLETE, completeHandler);
            soundFactory.addEventListener(Event.ID3, id3Handler);
            soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            soundFactory.load(mp3_request);

            channel = soundFactory.play();
            channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);

            // 計時器監聽播放進度
            positionTimer = new Timer(1000);
            positionTimer.addEventListener(TimerEvent.TIMER, positionTimerHandler);
            positionTimer.start();

            // 建立播放按鈕
            play_btn = new Sprite();
            play_btn.graphics.beginFill(0xFFCC32);
            play_btn.graphics.drawRoundRect(0, 0, 70, 18, 10, 10);
            play_btn.graphics.endFill();
            var play_txt:TextField = new TextField();
            play_txt.text = "播放";
            play_txt.x = 18;
            play_txt.selectable = false;
            play_btn.addChild(play_txt);
            play_btn.x = 50; play_btn.y = 100;
            play_btn.filters = [d_filters];
            play_btn.addEventListener(MouseEvent.CLICK, soundPlay);
            addChild(play_btn);

            // 建立暫停按鈕
            stop_btn = new Sprite();
            stop_btn.graphics.beginFill(0xFFCC32);
            stop_btn.graphics.drawRoundRect(0, 0, 70, 18, 10, 10);
            stop_btn.graphics.endFill();
            stop_btn.x = 130; stop_btn.y = 100;
            var stop_txt:TextField = new TextField();
            stop_txt.text = "暫停";
            stop_txt.x = 18; stop_txt.selectable = false;
            stop_btn.addChild(stop_txt);
            stop_btn.filters = [d_filters];
            stop_btn.addEventListener(MouseEvent.CLICK, soundStop);
            addChild(stop_btn);
        }

        private function positionTimerHandler(e:TimerEvent):void {
            var played:int = channel.position.toFixed(0);
            var total:int = soundFactory.length;
            var percent:int = Math.floor(played/total*100);
            // trace("進度: " + percent + "%");
        }

        private function completeHandler(e:Event):void { /* trace("載入完成"); */ }
        private function id3Handler(e:Event):void {
            for (var s in soundFactory.id3) { /* trace(s + ": " + soundFactory.id3[s]); */ }
        }
        private function ioErrorHandler(e:IOErrorEvent):void {
            positionTimer.stop();
        }
        private function progressHandler(e:ProgressEvent):void {
            var loaded:int = e.bytesLoaded;
            var total:int = e.bytesTotal;
            var percent:int = Math.floor(loaded/total*100);
            // trace("載入進度: " + percent + "%");
        }
        private function soundCompleteHandler(e:Event):void {
            positionTimer.stop();
        }

        private function soundPlay(e:MouseEvent):void {
            if (bSoundStop) {
                bSoundStop = false;
                channel = soundFactory.play(channel.position.toFixed(0)); // 從暫停處續播
            }
        }

        private function soundStop(e:MouseEvent):void {
            if (!bSoundStop) {
                bSoundStop = true;
                channel.stop(); // 暫停並記住位置
            }
        }
    }
}

關鍵邏輯說明

  • Sound + SoundChannel:Sound 負責載入解碼,SoundChannel 控制實際播放實例。
  • 暫停機制:channel.stop() 暫停,soundFactory.play(channel.position) 從記錄位置續播。
  • 狀態旗標:bSoundStop 防止重複點擊導致邏輯錯誤。
  • 進度監控:Timer 每秒讀取 channel.position 計算百分比,可用於進度條顯示。
  • 事件完整:COMPLETE/ID3/IO_ERROR/PROGRESS/SOUND_COMPLETE 全覆蓋,健壯性佳。

將此類別作為文件類別或實例化添加到舞台即可運行,自行替換 MP3 網址測試。

copyright © 萬盛學電腦網 all rights reserved