How can I play a RTMP video through netConnection and netStream

痞子三分冷 提交于 2019-11-28 11:32:16

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.." + "\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

UPDATED CODE:


  1. New demo link: Now QVC (UK shopping) instead of Russia Today (World News).
  2. Added line: nc.client = { onBWDone: function():void{} }; (since Flash Player is now more strict. Before it worked fine without this line).

Perhaps a more complete version of the code is like this. it should play RT channel live.

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

}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!