2011-01-28 212 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