Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

actionscript 3 - How can I play a RTMP video through netConnection and netStream

I am working on a prototype in which I have to play a video through RTMP protocol. My code is following :

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

}

But it not playing, not occurring any error and not plying, If anyone have any idea, please help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


  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).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...