【问题标题】:Update MP3 Player bar更新 MP3 播放器栏
【发布时间】:2011-01-20 00:22:17
【问题描述】:

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

有点丢失 bytesLoaded bytesTotal 还是?

假设栏的宽度为 200px;

我试过把这个和那个分开。没有运气

【问题讨论】:

    标签: actionscript-3 actionscript actionscript-2


    【解决方案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 是以毫秒为单位的声音长度。

    【讨论】:

    • 对,我假设他能够通过ENTER_FRAME 事件检查进度,这似乎是合理的,因为他之前曾处理过加载程序进度。我添加了一些 cmets 来帮助缩小差距,但目前我不知道我还能走多远。
    • 他甚至不知道如何从 bytesLoaded 和 bytesTotal 中获取百分比。
    • 我不明白为什么只给我代码没有帮助。我完全理解一切。就像我在帖子中所说的那样,我只是没有如何获得百分比。并不意味着我没有不知道如何使用 eventListeners 和使用绘图 API。谢谢!
    • 当然。不要以封面来判断一本书。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多