声音流停止加载

时间:2011-01-28 10:37:02

标签: actionscript-3

我这里有一个小小的声音流脚本,但有时如果你在当前加载之前按下播放到下一首曲目,则下一首曲目不会加载

package player {
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.external.ExternalInterface;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;

    import player.Loader_bar;

    public class Stream extends Sprite {
        private var _Sound = null;
        private var _Channel = null;

        private var isLoading = false;

        private var _Loader_bar = null;
        public var loader_color = null;
        public var loader_width = 0;
        public var loader_height = 0;

        private var i = 0;

        public function Stream(){
            this._Loader_bar = new Loader_bar();
            addChild(this._Loader_bar);
        }

        public function cnstr(){
            this._Loader_bar.color = this.loader_color;
            this._Loader_bar.w = this.loader_width;
            this._Loader_bar.h = this.loader_height;
            this._Loader_bar.cnstr();
        }

        public function play(url){
            this.stop();
            this.close();

            this._Sound = new Sound();
            this.isLoading = true;

            this.addEventListener(Event.ENTER_FRAME, listener_bytesLoaded);
            this._Sound.addEventListener(Event.COMPLETE, listener_loadedComplete);

            this._Sound.load(new URLRequest(url));
            this._Channel = this._Sound.play();
        }

        public function stop(){
            if(this._Channel){
                this._Channel.stop();
            }
        }

        private function close(){
            if(this.isLoading){
                this._Sound.close();
            }
        }

        private function listener_loadedComplete(event){
            this.close();

            this.isLoading = false;

            this.removeEventListener(Event.ENTER_FRAME, listener_bytesLoaded);
        }

        private function listener_bytesLoaded(event){
            var float = this._Sound.bytesLoaded / this._Sound.bytesTotal;
            this._Loader_bar.progress(float);

            var data = {
                i : this.i,
                float : float,
                loaded : this._Sound.bytesLoaded,
                total : this._Sound.bytesTotal
                };
            ExternalInterface.call('swf2js', 'tst_progress', data);
            this.i++;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

接近():无效 关闭流,导致任何数据下载停止。

试试这个:

创建一个类似 hasLoaded 的布尔值,将其设置为false。成功加载声音后,将其设置为true。

然后,当您播放声音时,您可以在 play()功能中测试 hasLoaded 。如果您在加载上一个声音之前调用 play(),则 hasLoaded 将为false,在这种情况下,您调用 this._Sound.close()在创建和加载新声音之前。测试而不是仅仅调用 close()的原因是,如果您暂停了流,则无需重新加载它以再次播放它。

此外:

关于未正确报告的负载,您的进度逻辑设置不正确。试试这个:

1)import flash.events.ProgressEvent

2)对于监听器,替换this.addEventListener(Event.ENTER_FRAME,listener_bytesLoaded);在你的play()方法中使用this._Sound.addEventListener(ProgressEvent.PROGRESS,listener_bytesLoaded);

3)将listener_bytesLoaded()方法更改为以下内容:

private function listener_bytesLoaded(event:ProgressEvent)
{
    var percent = event.bytesLoaded / event.bytesTotal;
    this._Loader_bar.progress(percent);

    var data = {
            i : this.i,
            float : percent,
            loaded : event.bytesLoaded,
            total : event.bytesTotal
            };

        ExternalInterface.call('swf2js', 'tst_progress', data);
        this.i++;
    }

4)更改this.removeEventListener(Event.ENTER_FRAME,listener_bytesLoaded); 在你的listener_loadedComplete()方法中的this._Sound.removeEventListener(ProgressEvent.PROGRESS,listener_bytesLoaded);然后将其移出该方法并将其放在close()方法中的条件内。

注意 - 我实际上并没有编译它,但我觉得很好。希望有所帮助。 :)

相关问题