我怎样才能通过NetConnection和NetStream播放RTMP视频(How can I p

2019-08-22 05:41发布

我的工作中,我要通过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);
}

}

但它不是在玩,没有发生任何错误,而不是行走,如果任何人有任何想法,请帮助我。

Answer 1:

做这种方式为我工作。 我只是用一个链接到新闻频道为例所以尽量用自己的直播网址替换它。 (PS:忽略该像素化,这是一个低分辨率例如链接)。

还..首先你有一个错字,让你说vid = new video(); (意味着= new Video ?)。 莫非是对一个问题addChild(vid)线进一步上? 其次,你需要像使用RTMP合作,以制止一些流抛出(在我过去的经验反正)的错误时,我已经包括了asyncErrorHandler,onFCSubscribeonBWDone功能。 本实施例中代码放在称为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(UK购物),而不是今日俄罗斯(世界新闻报)。
  2. 添加的行:nc.client = {onBWDone:功能():无效{}}; (自Flash Player现在更加严格。之前,它没有这一行工作的罚款)。


Answer 2:

或许代码的更完整的版本是这样的。 它应该发挥直播频道​​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;
    }

}

}



文章来源: How can I play a RTMP video through netConnection and netStream