2011-06-09 81 views
0

我在這個網站上發現了Flash AS3中的shoutcast streaming的代碼。 這是代碼:shoutcast streaming Flash AS3?

錯誤打開URL的 'https:// MYHOST:MyPort上' 的SecurityError:錯誤#2000:

package { 
    import flash.display.Sprite; 
    import flash.events.NetStatusEvent; 
    import flash.events.SecurityErrorEvent; 
    import flash.media.Video; 
    import flash.net.NetConnection; 
    import flash.net.NetStream; 
    import flash.events.Event; 

    public class NetConnectionExample extends Sprite { 
     private var streamURL:String = "https://myHost:myPort"; 
     private var connection:NetConnection; 
     private var stream:NetStream; 

     public function NetConnectionExample() { 
      connection = new NetConnection(); 
      connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
      connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); 
      connection.connect(null); 
     } 

     private function netStatusHandler(event:NetStatusEvent):void { 
      switch (event.info.code) { 
       case "NetConnection.Connect.Success": 
        connectStream(); 
        break; 
       case "NetStream.Play.StreamNotFound": 
        trace("Stream not found: " + streamURL); 
        break; 
      } 
     } 

     private function securityErrorHandler(event:SecurityErrorEvent):void { 
      trace("securityErrorHandler: " + event); 
     } 

     private function connectStream():void { 
      stream = new NetStream(connection); 
      stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
      stream.client = new CustomClient(); 
      stream.play(streamURL); 
     } 
    } 
} 


class CustomClient { 
    public function onMetaData(info:Object):void { 
     trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate); 
    } 
    public function onCuePoint(info:Object):void { 
     trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type); 
    } 
} 

但是當我編譯SWF我得到這個錯誤沒有活動的安全性上下文。

任何人都可以請告訴我如何正確地做到這一點?我一直在尋找兩天,無濟於事!

感謝

回答

2

的網址根本不存在,因爲這只是一個樣本,但也有一些其他的問題與此代碼。例如:connection.connect(null),它不會連接到任何地方,所以netconnection實例根本不會調度netstatus事件,這就是connectStream方法不會被執行的原因。 如果你想玩從Shoutcast的流,使用此代碼:

var soundOBJ : Sound = new Sound(); 
    soundOBJ.load(new URLRequest("http://78.159.104.147:80/")); 
    soundOBJ.play(); 

從這裏選擇一個流: http://callear.org/radio-script/demo-flash/xxqpupruq/pickstream.php?id=1377200&title=TechnoBase.FM+-+24h+Techno%2C+Dance%2C+Trance%2C+House+and+More+-+128k+MP3+-+a+SHOUTcast.com+member+station

(簡單地替換URLRequest的構造函數中的URL)

歡呼聲

Tamas Gronas

+0

此方法受嚴重內存泄漏。嘗試使用低內存設備,並在幾分鐘內崩潰。或者,讓它在高比特率的數據流上運行幾個小時,然後觀看它在電腦上吃掉所有的RAM。 – Brad 2011-06-10 13:19:24