Skip to main content
Inspiring
June 5, 2012
Question

Cannot Get Video Playing On IOS

  • June 5, 2012
  • 2 replies
  • 1114 views

Hello,

I am working on a Flex mobile project that requires me to play mp4 videos stored locally within the ipa file. I'm trying to use a simple class that utilizes NetStream, basically it's the same as the example posted in the docs: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetStream.html#play()

Those docs imply that the NetStream class will play mp4 videos, but so far I can only get it to play flv videos on my iPad 2. I've read where many people are using StageVideo for this, but I have to get that to work either. Can anyone make a suggestion on the best approach for this, or at least confirm that this will work?

I've also read about others using StageWebView, but I don't see it listed anywhere in the offical AS3 documentation.

thanks

This topic has been closed for replies.

2 replies

leejkAuthor
Inspiring
June 6, 2012

Hi,

Well, I have looked at the example from smoothblur and implemented parts of it into my class, but I still have problems. If I implement this class into my main Flex mobile project, it crashes on the iPad when the video component is activated. If I use this class in a very basic Flex mobile view based template, it does not display anything. I'm beginning to think that the AIR SDK in Flash Builder 4.6 is broke. I can't figure it out... does anything look wrong with my class?

thx

package

{

          import flash.display.Bitmap;

          import flash.display.BitmapData;

          import flash.display.Sprite;

          import flash.display.StageAlign;

          import flash.display.StageScaleMode;

          import flash.events.Event;

          import flash.events.StageVideoAvailabilityEvent;

          import flash.events.StageVideoEvent;

          import flash.geom.Rectangle;

          import flash.media.StageVideo;

          import flash.media.StageVideoAvailability;

          import flash.media.Video;

          import flash.net.NetConnection;

          import flash.net.NetStream;

          import mx.core.FlexGlobals;

          import mx.core.UIComponent;

          public class CatVideoPlayer extends UIComponent

          {

                    private var _defaultHeight:Number = 360;

                    private var _defaultWidth:Number = 640;

                    private var _ns:NetStream;

                    private var _obj:Object;

                    private var _source:String;

                    private var _sourceChanged:Boolean;

                    private var _stageBitmap:Bitmap;

                    private var _stageVideoAv:Boolean = false;

                    private var _sv:StageVideo;

                    private var _vd:Video;

                    private var _vidMask:Sprite;

                    public function CatVideoPlayer()

                    {

                              super();

                              mouseEnabled = false;

                              addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

                    }

                    public function onCuePoint(info:Object):void

                    {

                    }

                    public function onMetaData(info:Object):void

                    {

                    }

                    public function onPlayStatus(info:Object):void

                    {

                    }

                    public function onXMPData(info:Object):void

                    {

                    }

                    public function set source(value:String):void

                    {

                              _source = value;

                              _sourceChanged = true;

                              invalidateProperties();

                    }

                    override protected function commitProperties():void

                    {

                              super.commitProperties();

                              if (_sourceChanged)

                              {

                                        setState("showVideoPlayer");

                                        _sourceChanged = false;

                              }

                    }

                    private function convertStageToBitmap():void

                    {

                              var bitmapData:BitmapData = new BitmapData(FlexGlobals.topLevelApplication.width, FlexGlobals.topLevelApplication.height);

                              _stageBitmap = new Bitmap();

                              _vidMask = new Sprite();

                              _vidMask.graphics.beginFill(0x000000);

                              _vidMask.graphics.moveTo(0, 0);

                              _vidMask.graphics.drawRect(0, 0, _defaultWidth, _defaultHeight);

                              _vidMask.graphics.endFill();

                              //bitmapData.draw(this);

                              bitmapData.draw(FlexGlobals.topLevelApplication.document);

                              _stageBitmap.bitmapData = bitmapData;

                              _stageBitmap.cacheAsBitmap = true;

                              _vidMask.cacheAsBitmap = true;

                              addChild(_vidMask);

                              _stageBitmap.mask = _vidMask;

                              addChild(_stageBitmap);

                              //backgroundImg_mc.visible = false;

                              //playerWindow_mc.visible = false;

                              initVideo();

                    }

                    private function handleInterfaceClick(e:Event):void

                    {

                              e.preventDefault();

                              switch (e.target.name)

                              {

                                        case "playBtn_mc":

                                        {

                                                  setState("showVideoPlayer");

                                                  break;

                                        }

                                        case "closeVideo_mc":

                                        {

                                                  setState("closePlayer");

                                                  break;

                                        }

                                        default:

                                        {

                                                  //do default

                                        }

                              }

                    }

                    private function initVideo():void

                    {

                              var nc:NetConnection = new NetConnection();

                              nc.connect(null);

                              _ns = new NetStream(nc);

                              _obj = new Object();

                              _ns.client = _obj;

                              _ns.bufferTime = 2;

                              _ns.client = _obj;

                              //_obj.onMetaData = MetaData;

                              //_obj.onCuePoint = CuePoint;

                              if (_stageVideoAv)

                              {

                                        //use stageVideo

                                        trace("using stage video in CatVideoPlayer.initVideo()");

                                        _sv = stage.stageVideos[0];

                                        _sv.addEventListener(StageVideoEvent.RENDER_STATE, onRender);

                                        _sv.attachNetStream(_ns);

                              }

                              else

                              {

                                        //fallback to video class

                                        trace("using normal video in CatVideoPlayer.initVideo()");

                                        _vd = new Video(_defaultWidth, _defaultHeight);

                                        //_vd.x = 152;

                                        //_vd.y = 143;

                                        addChild(_vd);

                                        _vd.attachNetStream(_ns);

                              }

                              //addChild(videoUI_mc);

                              _ns.play(_source);

                    }

                    private function onAddedToStage(event:Event):void

                    {

                              trace("onAddedToStage fired in CatVideoPlayer");

                              stage.scaleMode = StageScaleMode.NO_SCALE;

                              stage.align = StageAlign.TOP_LEFT;

                              stage.addEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, onStageVideoAvailability);

                    }

                    private function onRender(e:StageVideoEvent):void

                    {

//                              if (!videoUI_mc.fsMode)

//                              {

//                                        _sv.viewPort = new Rectangle(152, 143, 720, 480);

//                              }

//                              else

//                              {

                                        _sv.viewPort = new Rectangle(0, 0, _sv.videoWidth, _sv.videoHeight);

                              //}

                    }

                    private function onStageVideoAvailability(e:StageVideoAvailabilityEvent):void

                    {

                              _stageVideoAv = (e.availability == StageVideoAvailability.AVAILABLE);

                    }

                    private function setState(currentState:String):void

                    {

                              trace("setState fired in CatVideoPlayer");

                              switch (currentState)

                              {

                                        case "showVideoPlayer":

                                        {

                                                  //playBtn_mc.visible = false;

                                                  //addChild(playerWindow_mc);

                                                  //addChild(closeVideo_mc);

                                                  //playerWindow_mc.visible = true;

                                                  //closeVideo_mc.visible = true;

                                                  //TweenLite.from(playerWindow_mc, 1, { alpha:0, onComplete:function(){ convertStageToBitmap(); } });

                                                  //TweenLite.from(closeVideo_mc, 1, { alpha:0 });

                                                  convertStageToBitmap();

                                                  break;

                                        }

                                        case "closePlayer":

                                        {

                                                  //removeVideoPlayer();

                                                  //playerWindow_mc.visible = false;

                                                  //playBtn_mc.visible = true;

                                                  break;

                                        }

                                        default:

                                        {

                                                  //do default

                                        }

                              }

                    }

          }

}

Participating Frequently
June 7, 2012

Are you getting an error in the desktop simulator? Have you tried using the wifi debugger for iOS so you can see what might be causing the crash? Make sure you are using a version of the AIR SDK that supports StageVideo on mobile.

Participating Frequently
June 6, 2012

I posted an example of using StageVideo and NetStream in a custom video player on this thread:

http://forums.adobe.com/message/4351774#4351774

This example is from an iPad app that I built. Also using the Open Source Media Framework (OSMF) is a good option if you'd rather not build your own. http://www.osmf.org/

leejkAuthor
Inspiring
June 6, 2012

Awesome! I will take a look at it and see what I'm apparently doing wrong. Thank you.