5

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.

Vipul
  • 431
  • 1
  • 5
  • 16
  • I would add event handler `nc.addEventListener` before calling `nc.connect`, also put some trace in the `onConnectionStatus`. – Lukasz 'Severiaan' Grela Apr 12 '13 at 09:22
  • I put nc.addEventListener above nc.connect, but still nothing happens, and there is a trace trace("Creating NetStream"), I'm getting that trace in my output. – Vipul Apr 12 '13 at 09:26
  • Good that you are getting the creating net stream but it is not the only message you can get, trace the `e.info.code` as first line in handler. – Lukasz 'Severiaan' Grela Apr 12 '13 at 12:05
  • I put trace(e.info.code) at first line and it traces "NetConnection.Connect.Success". but still nothing happening. – Vipul Apr 12 '13 at 13:02
  • Similar question here: http://stackoverflow.com/questions/8971272/as3-rtmp-video-stream-on-localhost-red5-path-issue – Paul Gregoire Jul 14 '13 at 06:10
  • That is not path issue, Mondain. and anyways I have found my answer, and you are very very late for your "not needed" comment. thank you – Vipul Jul 15 '13 at 05:57

2 Answers2

10

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).
VC.One
  • 14,790
  • 4
  • 25
  • 57
  • Thank you VC.one, it is working. But when i put my RTMP URL, it not works, seeme like there is some problem in my URL, anyways thanks a lot. – Vipul Apr 13 '13 at 04:20
  • Hard to say what's wrong with your own url but this line `streamID = "mp4:myVideo";` doesn't sound like it could connect to a stream. Try it like this `streamID = "myVideo";` or even try this.. `streamID = "*********.mp4"; videoURL = "rtmp://fms.xstream.dk/";` Beyond this I cannot suggest without seeing full url. Also if this is just some "found on the net" link then the stream could be protected somehow. Ex: their own player adjusts something in public visible link to get actual stream. Since you said it's a demo prototype maybe any other link is good enough to show as part of your concept?? – VC.One Apr 13 '13 at 09:57
  • thank you VC.One, it is working now, but i have one another question, i want to play it on my device (iPad2), but when i crate IPA file of it and installs it on ipad, it shows blank white screen, do you have any idea about it that can MP4 video play on IOS or not, if not what is the alternate ?? – Vipul Apr 15 '13 at 07:08
  • Unfortunately I don't use IOS so I have no clues about technical issues. Maybe it doesn't like MP4 files? Try my link in your IPA file It's .FLV format I think. Alternate is to search Stack Overflow.. – VC.One Apr 15 '13 at 09:11
  • how can i implement it in android using adobe air? – Shani Goriwal Jan 28 '14 at 11:41
0

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

}

}

mohsen
  • 1