更新MP3播放器栏

时间:2011-01-20 00:22:18

标签: actionscript-3 actionscript actionscript-2

我正在尝试在播放mp3时更新播放器栏。栏应该代表歌曲的长度并更新以显示到目前为止播放的歌曲的数量。

有点丢失bytesLoaded bytesTotal或?

假设条的宽度为200px;

我已经尝试将此分开了。没有运气

1 个答案:

答案 0 :(得分:1)

你走了。

package  
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    public class Engine extends Sprite
    {
        private var _sound:Sound;
        private var _soundBar:Sprite;
        private var _soundChannel:SoundChannel;
        public function Engine() 
        {
            addSoundChannel();
            drawSoundBar();
            //load sound
            loadSound("sound.mp3");
        }

        private function addSoundChannel():void
        {
            _soundChannel = new SoundChannel();
        }

        private function drawSoundBar():void
        {
            //draw box 200px x 10px with a red fill
            _soundBar = new Sprite();
            _soundBar.graphics.beginFill(0xFF0000);
            _soundBar.graphics.drawRect(0, 0, 200, 10);
            _soundBar.graphics.endFill();
            addChild(_soundBar);
            _soundBar.x = (stage.stageWidth/2) - (_soundBar.width/2);
            _soundBar.y = (stage.stageHeight/2) - (_soundBar.height/2);
        }

        private function loadSound(url:String):void
        {
            var toLoad:URLRequest = new URLRequest(url);
            _sound = new Sound();
            _sound.load(toLoad);
            _sound.addEventListener(Event.COMPLETE, soundLoaded, false, 0, true);
        }

        private function soundLoaded(evt:Event):void
        {
            trace("loaded");
            _soundChannel = _sound.play();
            addListeners();
        }

        private function addListeners():void
        {
            stage.addEventListener(Event.ENTER_FRAME, checkSound, false, 0, true);
            _soundChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, true);
        }

        private function checkSound(evt:Event):void
        {
            //adjust scaleX from 0 to 1 based on sound position
            _soundBar.scaleX = _soundChannel.position / _sound.length;
        }

        private function soundComplete(evt:Event):void
        {
            trace("done");
            stage.removeEventListener(Event.ENTER_FRAME, checkSound);
        }

    }

}

SoundChannel.position以毫秒为单位抓取当前位置,而Sound.length是声音长度(以毫秒为单位)。