2011-01-28 208 views
1

我有一点点的声音流脚本在这里,但有时如果按播放下一首曲目之前,在当前已经加载完成的下轨不comeplete装载声音流停止装载

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

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

试试这个:

创建一个布尔像hasLoaded,其设置为false。当声音成功载入时,将其设置为true。

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

此外:

关于负荷不正确的报告,你有你的进步逻辑设置错误。尝试:

1)进口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()方法中的条件内。

注 - 我没有真正编译这个,但我认为它很好。希望有所帮助。 :)

+0

谢谢..现在它加载整个轨道:)但你可以告诉我为什么已经完成加载轨道返回0 this._Sound.bytesLoaded?加载器状态即使已完成加载(chached),也会给出0%..我已更新我的代码:) – clarkk 2011-01-29 09:46:00