Skip navigation
Currently Being Moderated

Stage Video Issue

Jun 15, 2011 2:20 AM

Hi All,

              I have tried this sample for stage video.

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageDisplayState;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.FullScreenEvent;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.events.NetStatusEvent;
    import flash.events.StageVideoAvailabilityEvent;
    import flash.events.StageVideoEvent;
    import flash.events.TimerEvent;
    import flash.events.VideoEvent;
    import flash.geom.Rectangle;
    import flash.media.SoundTransform;
    import flash.media.StageVideo;
    import flash.media.StageVideoAvailability;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.system.LoaderContext;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.ui.Keyboard;
   
    /**
     *
     * @author Thibault Imbert
     *
     */   
    [SWF(frameRate="1", backgroundColor="#000000")]
    public class SimpleStageVideo extends Sprite
    {
        private static const FILE_NAME:String = "TwinSpiresTV_intro.flv";
        private static const INTERVAL:Number = 500;
        private static const BORDER:Number = 20;
       
        private var legend:TextField = new TextField();
        private var sv:StageVideo;
        private var nc:NetConnection;
        private var ns:NetStream;
        private var rc:Rectangle;
        private var video:Video;
        private var thumb:Shape;
        private var interactiveThumb:Sprite;
        private var totalTime:Number;
       
        private var videoWidth:int;
        private var videoHeight:int;
        private var outputBuffer:String = new String();
        private var rect:Rectangle = new Rectangle(0, 0, 0, BORDER);
        private var videoRect:Rectangle = new Rectangle(0, 0, 0, 0);
        private var gotStage:Boolean;
        private var stageVideoInUse:Boolean;
        private var classicVideoInUse:Boolean;
        private var accelerationType:String;
        private var infos:String = new String();
        private var available:Boolean;
        private var inited:Boolean;
        private var played:Boolean;
        private var container:Sprite;
       
        /**
         *
         *
         */       
        public function SimpleStageVideo()
        {
            // Make sure the app is visible and stage available
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }
       
        /**
         *
         * @param event
         *
         */       
        private function onAddedToStage(event:Event):void
        {
            // Scaling
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            legend.autoSize = TextFieldAutoSize.LEFT;
           
            // Debug infos
            legend.multiline = true;
            legend.background = true;
            legend.backgroundColor = 0xFFFFFFFF;
            addChild(legend);
           
            // Thumb seek Bar
            thumb = new Shape();
           
            interactiveThumb = new Sprite();
            interactiveThumb.addChild(thumb);
            addChild(interactiveThumb);
           
            // Connections
            nc = new NetConnection();
            nc.connect(null);
            ns = new NetStream(nc);
            ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
            ns.client = this;
           
            // Screen
            video = new Video();
            video.smoothing = true;
           
            // Video Events
            // the StageVideoEvent.STAGE_VIDEO_STATE informs you if StageVideo is available or not
            stage.addEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILA BILITY, onStageVideoState);
            // in case of fallback to Video, we listen to the VideoEvent.RENDER_STATE event to handle resize properly and know about the acceleration mode running
            video.addEventListener(VideoEvent.RENDER_STATE, videoStateChange);
           
            // Input Events
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            stage.addEventListener(Event.RESIZE,  onResize);
            stage.addEventListener(MouseEvent.CLICK, onClick);
        }
       
        /**
         *
         * @param event
         *
         */       
        private function onNetStatus(event:NetStatusEvent):void
        {
            if ( event.info == "NetStream.Play.StreamNotFound" )
                legend.text = "Video file passed, not available!";
        }
       
        /**
         *
         * @param event
         *
         */       
        private function onFrame(event:Event):void
        {
            var ratio:Number = (ns.time / totalTime) * (stage.stageWidth - (BORDER << 1));
            rect.width = ratio;
            thumb.graphics.clear();
            thumb.graphics.beginFill(0xFFFFFF);
            thumb.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);   
        }
       
        /**
         *
         * @param event
         *
         */       
        private function onClick(event:MouseEvent):void
        {
            if ( event.stageY >= interactiveThumb.y - BORDER && event.stageX <= stage.stageWidth - BORDER )
            {
                var seekTime:Number = (stage.mouseX - BORDER) * ( totalTime / (stage.stageWidth - (BORDER << 1) ) );
                ns.seek( seekTime );   
            }
        }
       
        /**
         *
         * @param event
         *
         */       
        private function onKeyDown(event:KeyboardEvent):void
        {   
            if ( event.keyCode == Keyboard.O )
            {
                if ( available )
                    // We toggle the StageVideo on and off (fallback to Video and back to StageVideo)
                    toggleStageVideo(inited=!inited);
               
            } else if ( event.keyCode == Keyboard.F )
            {
                stage.displayState = StageDisplayState.FULL_SCREEN;
            } else if ( event.keyCode == Keyboard.SPACE )
            {
                ns.togglePause();
            }
        }
       
        /**
         *
         * @param width
         * @param height
         * @return
         *
         */       
        private function getVideoRect(width:uint, height:uint):Rectangle
        {   
            var videoWidth:uint = width;
            var videoHeight:uint = height;
            var scaling:Number = Math.min ( stage.stageWidth / videoWidth, stage.stageHeight / videoHeight );
           
            videoWidth *= scaling, videoHeight *= scaling;
           
            var posX:uint = stage.stageWidth - videoWidth >> 1;
            var posY:uint = stage.stageHeight - videoHeight >> 1;
           
            videoRect.x = posX;
            videoRect.y = posY;
            videoRect.width = videoWidth;
            videoRect.height = videoHeight;
           
            return videoRect;
        }
       
        /**
         *
         *
         */       
        private function resize ():void
        {   
            if ( stageVideoInUse )
            {
                // Get the Viewport viewable rectangle
                rc = getVideoRect(sv.videoWidth, sv.videoHeight);
                // set the StageVideo size using the viewPort property
                sv.viewPort = rc;
            } else
            {
                // Get the Viewport viewable rectangle
                rc = getVideoRect(video.videoWidth, video.videoHeight);
                // Set the Video object size
                video.width = rc.width;
                video.height = rc.height;
                video.x = rc.x, video.y = rc.y;
            }
           
            interactiveThumb.x = BORDER, interactiveThumb.y = stage.stageHeight - (BORDER << 1);
            legend.text = infos;
        }
       
        /**
         *
         * @param evt
         *
         */       
        public function onMetaData ( evt:Object ):void
        {
            totalTime = evt.duration;
            stage.addEventListener(Event.ENTER_FRAME, onFrame);
        }
       
        /**
         *
         * @param event
         *
         */       
        private function onStageVideoState(event:StageVideoAvailabilityEvent):void
        {   
            // Detect if StageVideo is available and decide what to do in toggleStageVideo
            toggleStageVideo(available = inited = (event.availability == StageVideoAvailability.AVAILABLE));
        }
       
        /**
         *
         * @param on
         *
         */       
        private function toggleStageVideo(on:Boolean):void
        {   
            infos = "StageVideo Running (Direct path) : " + on + "\n";
           
            // If we choose StageVideo we attach the NetStream to StageVideo
            if (on)
            {
                stageVideoInUse = true;
                if ( sv == null )
                {
                    sv = stage.stageVideos[0];
                    sv.addEventListener(StageVideoEvent.RENDER_STATE, stageVideoStateChange);
                }
                sv.attachNetStream(ns);
                if (classicVideoInUse)
                {
                    // If we use StageVideo, we just remove from the display list the Video object to avoid covering the StageVideo object (always in the background)
                    stage.removeChild ( video );
                    classicVideoInUse = false;
                }
            } else
            {
                // Otherwise we attach it to a Video object
                if (stageVideoInUse)
                    stageVideoInUse = false;
                classicVideoInUse = true;
                video.attachNetStream(ns);
                stage.addChildAt(video, 0);
            }
           
            if ( !played )
            {
                played = true;
                ns.play(FILE_NAME);
            }
        }
       
        /**
         *
         * @param event
         *
         */       
        private function onResize(event:Event):void
        {
            resize();       
        }
       
        /**
         *
         * @param event
         *
         */       
        private function stageVideoStateChange(event:StageVideoEvent):void
        {   
            infos += "StageVideoEvent received\n";
            infos += "Render State : " + event.status + "\n";
            resize();
        }
       
        /**
         *
         * @param event
         *
         */       
        private function videoStateChange(event:VideoEvent):void
        {   
            infos += "VideoEvent received\n";
            infos += "Render State : " + event.status + "\n";
            resize();
        }
    }
}

 

I was able to play only the audio. The video is not playing.

 

Regards,

Jayagopal.

 
Replies
  • Currently Being Moderated
    Jun 26, 2011 5:14 AM   in reply to Flex Rock

    Does anyone know if this really works - I was unable to detect a stage video object using:

    var stageVideo:StageVideo;

     

                if ( stage.stageVideos.length >= 1 ) {
                    trace("true");
                    stageVideo = stage.stageVideos[0];
                }else{
                    trace("false");
                   
                }

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2011 3:19 PM   in reply to Flex Rock

    I used the same code you gave as an actionscript web application, and it worked when uploaded to the web. However when playing/debugging from flex it cannot find the stage video (at sv = stage.stageVideos[0]; it says the index is out of range). I also managed to get it to play live streaming video. The processor use was 6% compared to 50% with the normal video, so it does make a big difference.

    I don't think StageVideo is supported yet in air (except for TV) or flex, so I am using the option to make an actionscript mobile application in Flexbuilder (utilising the above code) and exporting it to ios etc - perhaps it is impossible, but I cannot find clear guidance on the matter.

    Anyone know if it is possible to make a mobile video application in flexbuilder 4.5 this way??

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 16, 2011 6:21 AM   in reply to Flex Rock

    If you copy the following code

     

    import flash.events.StageVideoAvailabilityEvent;
    import flash.events.Event;
    import flash.text.TextField;
    
    var tf:TextField = new TextField();
    tf.text = "STAGE_VIDEO_AVAILABILITY not called";
    addChild(tf);
    
    stage.addEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, onStageVideoAvailable);
    
    function onStageVideoAvailable(e:Event):void {
      tf.text = stage.stageVideos.length.toString();
    }
    

     

    into the flash and compile it in different ways here are the results on my machine

     

    TypeResult
    As flash player v10.3 in test/debug modeSTAGE_VIDEO_AVAILABILITY not called
    As flash player v10.3 running in the browser4
    As AIR 3 in test/debug mode0
    As AIR 3 published *.air file0

     

    Message was edited by: CyberProdigy

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 15, 2011 6:36 AM   in reply to CyberProdigy

    Did somebody managed to get stageVideo running on Air 3 for desktop?

     

    I have the rendermode in the app.xml set to direct, and the compiler settings set to swf-version 13, but still no luck. I'm also using the latest air sdk.

     

    BTW.: When running the exact same application on Andoird 3.2 (EEE Pad), stage Video works fine. In the Brwoser also, but not in Air 3 release candidate.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 11, 2012 7:56 PM   in reply to Flex Rock

    have you found the answer,i meet the same problem

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 19, 2013 3:01 AM   in reply to heartgoon

    Any update  guys ?

     

    Cheers

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 19, 2013 8:41 PM   in reply to Pedro Leonardo Sousa

    I have not used stageVideo, but for traditional displaylist video flash.media.Video, inside the <android> tag inside application.xml add this line "<containsVideo>true</containsVideo>", this should not be in <manifestAdditions> tag just inside <android>. If you have no luck with that, someone used "<application android:hardwareAccelerated="true"/>" in the android <manifest> tag and set the initialWindow tags renderMode to gpu. If <containsVideo> works let us know, many people have problem with video on android and this tag is not documented so not much is known about it.

     

    This is what the 3.5 release note says, so I imagine it is for all video types with air on android.

     

    "We have added a new Android-specific xml element <containsVideo> in the application.xml. If an app uses Video, then the app is expected to add <containsVideo>true</containsVideo> in the application.xml as an indication to the runtime. This is necessary to maintain correct ordering of video with other Flash content across different Android OS versions."

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 26, 2013 3:58 PM   in reply to BKin2013

    Just adding my name to this thread in case there is any update.

     
    |
    Mark as:

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