如何通过netConnection和netStream播放RTMP视频

时间:2013-04-12 09:20:24

标签: actionscript-3 flash rtmp flash-player-11

我正在开发一个原型,我必须通过RTMP协议播放视频。我的代码如下:

private function init():void
    {
        streamID:String = "mp4:myVideo";
        videoURL = "rtmp://fms.xstream.dk/*********.mp4";
        vid = new video();
        vid.width = 480;
        vid.height = 320;

        nc = new NetConnection();
        nc.client = {onBWDone: function():void
            {
            }};
        nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
        nc.connect(videoURL);           
    }

    private function onConnectionStatus(e:NetStatusEvent):void
    {
        if (e.info.code == "NetConnection.Connect.Success")
        {
            trace("Creating NetStream");
            netStreamObj = new NetStream(nc);
            netStreamObj.client = new CustomClient();
            netStreamObj.play(streamID);
            vid.attachNetStream(netStreamObj);
            addChild(vid);
            intervalID = setInterval(playback, 1000);
        }
    }

    private function playback():void
    {
        trace((++counter) + " Buffer length: " + netStreamObj.bufferLength);
    }





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);
}

}

但它没有播放,没有发生任何错误而且没有合作,如果有人有任何想法,请帮助我。

2 个答案:

答案 0 :(得分:10)

这样做对我有用。我只是使用新闻频道的链接作为示例,因此请尝试使用您自己的流网址替换它。 (ps:忽略像素化,这是一个低分辨率的示例链接)。

另外..首先你有一个错字,你说vid = new video();(意思是= new Video ??)。这可能是addChild(vid)行的问题吗?其次,您需要使用RTMP中包含的 asyncErrorHandler onFCSubscribe onBWDone 等函数来阻止某些流丢失的错误(在我过去的经历中无论如何)。此示例代码位于名为RTMP_test.as的文档类中(重命名为首选)...

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.." + "\r"); }

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

更新代码:


  1. 新的演示链接:现在是QVC(英国购物),而不是今日俄罗斯(世界新闻)。
  2. 添加了一行: nc.client = {onBWDone:function():void {}}; (因为Flash Player现在更加严格。在没有此行的情况下工作正常之前)。

答案 1 :(得分:0)

也许更完整的代码版本是这样的。它应该现场播放RT频道。

package  {
import flash.events.NetStatusEvent;
import flash.events.AsyncErrorEvent;
import flash.display.MovieClip;
import flash.net.NetStream;
import flash.net.NetConnection;
import flash.media.Video;
import flash.utils.setInterval;


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 var intervalID:uint;
    public var counter:int;

    public function RTMP_test ()
    { init_RTMP(); }

    function init_RTMP():void
    {

            streamID  = "RT_2";
            videoURL = "rtmp://fms5.visionip.tv/live/RT_2";


            vid = new Video(); //typo! was "vid = new video();"

            nc = new NetConnection();
            nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
            nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
            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.." + "\r"); }

    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 _aspectH:int;
        var _videoW:int;
        var _videoH:int;

        var relationship:Number;
        relationship = data.height / data.width;

        //Aspect ratio calculated here..
        _videoW = _stageW;
        _videoH = _videoW * relationship;
        _aspectH = (_stageH - _videoH) / 2;

        vid.x = 0;
        vid.y = _aspectH;
        vid.width = _videoW;
        vid.height = _videoH;
    }

}

}