doing it this way worked for me. I just used a link to a news channel as example so try replacing it with your own stream url. (ps: ignore the pixelation, it's a low-res example link).
Also.. first you had a typo whereby you said vid = new video();
(meant = new Video
??). Could that be an issue for the addChild(vid)
line further on? Second you need functions like the asyncErrorHandler, onFCSubscribe and onBWDone that I've included when working with RTMP to stop errors that some streams throw out (in my past experiences anyway). This example code goes in a document class called RTMP_test.as (rename as preferred)...
package {
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.media.*;
import flash.system.*;
import flash.utils.ByteArray;
public class RTMP_test extends MovieClip
{
public var netStreamObj:NetStream;
public var nc:NetConnection;
public var vid:Video;
public var streamID:String;
public var videoURL:String;
public var metaListener:Object;
public function RTMP_test ()
{ init_RTMP(); }
function init_RTMP():void
{
/*
streamID = "mp4:myVideo";
videoURL = "rtmp://fms.xstream.dk/*********.mp4";
*/
streamID = "QVCLive1@14308";
videoURL = "rtmp://cp79650.live.edgefcs.net/live/";
vid = new Video(); //typo! was "vid = new video();"
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
nc.client = { onBWDone: function():void{} };
nc.connect(videoURL);
}
private function onConnectionStatus(e:NetStatusEvent):void
{
if (e.info.code == "NetConnection.Connect.Success")
{
trace("Creating NetStream");
netStreamObj = new NetStream(nc);
metaListener = new Object();
metaListener.onMetaData = received_Meta;
netStreamObj.client = metaListener;
netStreamObj.play(streamID);
vid.attachNetStream(netStreamObj);
addChild(vid);
//intervalID = setInterval(playback, 1000);
}
}
private function playback():void
{
//trace((++counter) + " Buffer length: " + netStreamObj.bufferLength);
}
public function asyncErrorHandler(event:AsyncErrorEvent):void
{ trace("asyncErrorHandler.." + "
"); }
public function onFCSubscribe(info:Object):void
{ trace("onFCSubscribe - succesful"); }
public function onBWDone(...rest):void
{
var p_bw:Number;
if (rest.length > 0)
{ p_bw = rest[0]; }
trace("bandwidth = " + p_bw + " Kbps.");
}
function received_Meta (data:Object):void
{
var _stageW:int = stage.stageWidth;
var _stageH:int = stage.stageHeight;
var _videoW:int;
var _videoH:int;
var _aspectH:int;
var Aspect_num:Number; //should be an "int" but that gives blank picture with sound
Aspect_num = data.width / data.height;
//Aspect ratio calculated here..
_videoW = _stageW;
_videoH = _videoW / Aspect_num;
_aspectH = (_stageH - _videoH) / 2;
vid.x = 0;
vid.y = _aspectH;
vid.width = _videoW;
vid.height = _videoH;
}
} //end class
} //end package
UPDATED CODE:
- New demo link: Now QVC (UK shopping) instead of Russia Today (World News).
- Added line: nc.client = { onBWDone: function():void{} }; (since Flash Player is now more strict. Before it worked fine without this line).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…