Skip navigation
Santu
Currently Being Moderated

Flex mobile live streaming

Jan 14, 2014 7:27 PM

Tags: #flex-mobile #ios #flex_mobile #actionscript_3.0 #4.6

Hi All,

  I am trying to display a live stream from rtmp server. Below is my code.

 

<?xml version="1.0" encoding="utf-8"?>

<s:Group height="100%" width="100%"

        

         xmlns:fx="http://ns.adobe.com/mxml/2009"

         xmlns:s="library://ns.adobe.com/flex/spark"

         xmlns:mx="library://ns.adobe.com/flex/mx">

   

   

    <fx:Script>

        <![CDATA[

            import mx.core.FlexGlobals;

           

            private const URL:String    = "rtmp://eigr2.flashlive.bigCDN.com/20175D";//"http://www.helpexamples.com/flash/video/cuepoints.flv";

            private const STREAM_KEY:String = "eigr8";

           

            private var _video:Video = new Video();

           

            private var _nc:NetConnection;

            private var _custClient:Object;

            private var _ns:NetStream;

            private var _streamKey:String;

           

            public function playVideo():void

            {

                var  url:String = URL;

               

                if(videoHandler.numChildren == 0)

                {

                    _nc    = new NetConnection();

                    _nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

                    _nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

                    _nc.connect(URL);

                }       

            }

           

            private function netStatusHandler(event:NetStatusEvent):void

            {

                trace("NC " + event.info.code);

                switch (event.info.code)

                {

                    case "NetConnection.Connect.Success":

                        _video                    = new Video();

                        _video.smoothing        = true;

                       

                        var custClient:Object    = new Object();

                        custClient.onMetaData    = metaDataHandler;

                       

                        _ns                = new NetStream(_nc);

                        _ns.client        = custClient;

                        _ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

                        _ns.addEventListener(DRMErrorEvent.DRM_ERROR, drmerror);

                        _ns.addEventListener(DRMStatusEvent.DRM_STATUS, drmStatus);

                        _ns.addEventListener(StatusEvent.STATUS, status);

                        _ns.addEventListener(NetStatusEvent.NET_STATUS, target);

                        _ns.bufferTime = 2;

                        _ns.receiveVideo(true);

                       

                        var net:NetStreamPlayOptions = new NetStreamPlayOptions();

                        //net.len = 1;

                        //net.transition = NetStreamPlayTransitions.APPEND;

                        net.streamName = STREAM_KEY;

                        _ns.play(STREAM_KEY);

                       

                        _video.attachNetStream(_ns);

                        videoHandler.addChild(_video);

                        videoHandler.visible = true;

                        //background.visible = true;

                        break;

                   

                    case "NetConnection.Connect.Failed":    //shownoVideoText();

                        break;

                   

                    case "NetStream.Play.StreamNotFound":    trace("Unable to locate video: ");

                        break;

                }

            }

           

            private function status(event:StatusEvent):void

            {

                trace("Status Event : " + event.toString());

            }

           

            private function drmerror(event:DRMErrorEvent):void

            {

                trace("DRM Error : " + event.toString());

            }

           

            private function drmStatus(event:DRMStatusEvent):void

            {

                trace("DRM Status : " + event.toString());

            }

           

            private function videoStates(event:Event):void

            {

                trace(event.toString());

            }

           

            public function stopVideo():void

            {

                _nc.close();

                _nc = null;

                _ns = null;

               

                this.videoHandler.removeChild(_video);

            }

           

            private function asyncErrorHandler(event:AsyncErrorEvent):void

            {

                trace("Async errrorrrrrrrrrrrrrrrrrrrr");

            }

           

            private function target(e:NetStatusEvent):void

            {

                trace("Started playing : " + e.info.code);           

            }

           

            private function metaDataHandler(infoObject:Object):void

            {

                this.setVideoSize(infoObject.height, infoObject.width);

            }

           

            /*public function playVideo():void

            {

                custClient    = new Object();

                custClient.onMetaData    = metaDataHandler;

               

                nc    = new NetConnection();

                nc.connect(null);

                nc.client = { onBWDone: function():void{} };

                //nc.addEventListener(NetStatusEvent.NET_STATUS, target);

                nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

               

                ns        = new DynamicCustomNetStream(nc);

                ns.client                = custClient;

                ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

                ns.addEventListener(NetStatusEvent.NET_STATUS, target);

                ns.play(URL);

               

                videoPlayer.attachNetStream(ns);

                videoPlayer.smoothing        = true;

               

                if(!videoHandler.contains(videoPlayer))

                    videoHandler.addChild(videoPlayer);

            }

           

            private function metaDataHandler(infoObject:Object):void

            {

                this.setVideoSize(infoObject.height, infoObject.width);

            }*/

           

            override public function set currentState(value:String):void

            {

                super.currentState = value;

                this.videoHandler.percentHeight = value == "fullScreen" ? 100 : 50;

            }

           

            public function onFCSubscribe(info:Object):void

            {

                trace("onFCSubscribe - succesful");

            }

           

            protected function goBack():void

            {

                FlexGlobals.topLevelApplication.showVideo(false);

            }

           

        ]]>

    </fx:Script>

   

    <fx:Declarations>

        <!-- Place non-visual elements (e.g., services, value objects) here -->

    </fx:Declarations>

   

    <s:Button click="goBack()"

              horizontalCenter="0"

              label="Go Back" bottom="50"/>

   

    <mx:UIComponent width="100%"

                    height="50%"

                    top="0" left="0"

                    id="videoHandler"/>

</s:Group>

 

When I try to run this, I could successfully connect to server but not getting the data. If I open the url in browser PublishNotify gets dispatched and I get the stream. As soon as I close UnpublishNotify gets dispatched and I wont get the stream. Can I know the possible reasons and solution?

 

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points