public static void ToWebBitmap(this Uri uri, string Host, Action<Bitmap> handler)
        {
            var x = uri.ToString();

            if (x.StartsWith(Host))
            {
                handler(
                    KnownEmbeddedResources.Default[x.Substring(Host.Length + 1)].ToBitmapAsset()
                );
                return;
            }

            var e = new Loader();

            var url = new URLRequest(uri.ToString());

            e.contentLoaderInfo.complete +=
                ev =>
                {
                    try
                    {
                        var v = (Bitmap)e.content;

                        handler(v);
                    }
                    catch
                    {
                    }
                };

            e.load(url, Context);
        }
        public VerticalScrollerLoader()
        {
            // http://www.iheartactionscript.com/loading-an-external-swf-in-as3/
            // http://kb2.adobe.com/cps/141/tn_14190.html
            Security.allowDomain("*");
            Security.allowInsecureDomain("*");

            var myLoader = new ScriptCoreLib.ActionScript.flash.display.Loader();

            addChild(myLoader);

            var url = new URLRequest("http://zproxy.appspot.com.nyud.net/assets/Bulldog/VerticalScrollerFlash.swf");

            myLoader.load(url);
        }
        public VerticalScrollerLoader()
        {
            // http://www.iheartactionscript.com/loading-an-external-swf-in-as3/
            // http://kb2.adobe.com/cps/141/tn_14190.html
            Security.allowDomain("*");
            Security.allowInsecureDomain("*");

            var myLoader = new ScriptCoreLib.ActionScript.flash.display.Loader();

            addChild(myLoader);

            var url = new URLRequest("http://zproxy.appspot.com.nyud.net/assets/Bulldog/VerticalScrollerFlash.swf");


            myLoader.load(url);
        }
        public YouTubePlayer(
            string TargetContent = "http://www.youtube.com/apiplayer?version=3",
            int DefaultWidth = 1280,
            int DefaultHeight = 720,
            string DefaultVideo = "Z__-3BbPq6g",

            string suggestedQuality = "hd720",


            Action yield_init = null
            )
        {
            var ldr = new Loader();
            this.Loader = ldr;
            var urlReq = new URLRequest(TargetContent);

            var ctx_app = ApplicationDomain.currentDomain;
            var ctx_sec = SecurityDomain.currentDomain;

            // http://www.youtube.com/crossdomain.xml
            ctx_app = null;
            ctx_sec = null;

            // http://www.zedia.net/2010/using-the-actionscript-3-youtube-api/

            bool once = false;

            #region onReady
            Action<Event> onReady = e =>
            {
                if (once)
                    return;

                once = true;


#if JSC_FEATURE_dynamic
                        dynamic player = ldr.content;

                        player.setSize(160, 120);
#endif
                ldr.content.setSize(DefaultWidth, DefaultHeight);

                pauseVideo = delegate
                {
                    ldr.content.pauseVideo();
                };

                loadVideoById = x =>
                {
                    CurrentVideoId = x;

                    ldr.content.loadVideoById(x, suggestedQuality: suggestedQuality);
                };

                loadVideoById(DefaultVideo);

                if (yield_init != null)
                {
                    yield_init();
                    yield_init = null;
                }


            };
            #endregion


            var PreviousCurrentState = YouTubePlayerState.unknown;
            var CurrentState = YouTubePlayerState.unknown;

            DefaultScene = this["default", 0, 1000];
            var CurrentScene = DefaultScene;
            Action CurrentSceneDone = delegate { };

            #region onStateChange
            Action<Event> onStateChange = e =>
            {
                PreviousCurrentState = CurrentState;
                CurrentState = e.get_data_as_YouTubePlayerState();

                if (PreviousCurrentState != CurrentState)
                {
                    if (CurrentState == YouTubePlayerState.playing)
                    {
                        // notify other scenes of delinking?

                        if (this.Playing != null)
                            this.Playing(SceneTranslate(CurrentScene));

                        this.ReferencedScenes.WithEach(
                            k =>
                            {
                                k.RaiseLinkDenotification();
                            }
                        );
                    }
                    else
                    {
                        if (this.NotPlaying != null)
                            this.NotPlaying(SceneTranslate(CurrentScene));
                    }

                    if (CurrentState == YouTubePlayerState.paused)
                    {
                        if (this.Paused != null)
                            this.Paused(SceneTranslate(CurrentScene));

                    }
                }
            };
            #endregion


            var TimeToPause = 0.4;

            var t = new Timer(1000 / 100);

            var PlaySceneCounter = 0;

            t.timer +=
                delegate
                {
                    if (ldr.content == null)
                        return;

                    if (CurrentState == YouTubePlayerState.playing)
                    {
                        var time = ldr.content.getCurrentTime();

                        var time_index = (int)time;

                        var duration = ldr.content.getDuration();

                        var playall = CurrentScene.end > duration;

                        // flag4 = ((double0 < (double2 - 500)) == 0);
                        // 1 second is 1.0!! :)
                        var notending = time < (duration - 0.500);
                        //var xending = time >= (duration - 500);
                        var ending = !notending;

                        // ReferenceError: Error #1069: Property getDuration not found on flash.display.Loader and there is no default value.
                        var m = new { PlaySceneCounter, time, time_index, CurrentScene.end, duration, playall, ending }.ToString();

                        if (StatusToClients != null)
                            StatusToClients(m);

                        // phone activated

                        if (playall)
                        {
                            if (ending)
                            {
                                ldr.content.pauseVideo();
                                CurrentSceneDone();
                            }
                        }
                        else if (time >= (TimeToPause))
                        {
                            ldr.content.pauseVideo();
                            CurrentSceneDone();
                        }
                    }
                };

            t.start();

            #region PlayScene
            this.PlayScene =
                (e, Done) =>
                {
                    PlaySceneCounter++;

                    //if (e.end == 0)
                    //    e.end = ldr.content.getDuration() - 1000;

                    CurrentScene = e;
                    CurrentSceneDone = Done;

                    TimeToPause = e.end;

                    ldr.content.seekTo(e.start);
                    ldr.content.playVideo();
                };
            #endregion



            ldr.contentLoaderInfo.ioError +=
                delegate
                {

                };

            ldr.contentLoaderInfo.init +=
                delegate
                {
                    ldr.content.addEventListener("onReady", onReady.ToFunction(), false, 0, false);
                    ldr.content.addEventListener("onStateChange", onStateChange.ToFunction(), false, 0, false);

                };

            var ctx = new LoaderContext(true, ctx_app, ctx_sec);
            ldr.load(urlReq, ctx);


            this.Scenes = new SceneSequenzer { Owner = this };
        }
		public static void playSound(this Stream wave)
		{
			var swf = new BinaryWriter(new MemoryStream());

			// generate the file
			// swf.endian = flash.utils.Endian.LITTLE_ENDIAN;

			// SWF header
			swf.Write((byte)0x46);    // 'FWS' signature
			swf.Write((byte)0x57);
			swf.Write((byte)0x53);
			swf.Write((byte)0x07);    // version
			swf.Write((uint)0);// filesize (will be set later)
			swf.Write((byte)0x78);    // area size
			swf.Write((byte)0x00);
			swf.Write((byte)0x05);
			swf.Write((byte)0x5F);
			swf.Write((byte)0x00);
			swf.Write((byte)0x00);
			swf.Write((byte)0x0F);
			swf.Write((byte)0xA0);
			swf.Write((byte)0x00);
			swf.Write((byte)0x00);   // framerate (12fps)
			swf.Write((byte)0x0C);
			swf.Write((short)1);      // one frame

			// DefineSound tag
			swf.writeTagInfo(14, (int)(2 + 1 + 4 + wave.Length));
			swf.Write((short)1);      // sound (character) ID
			swf.Write((byte)0x3C);    // sound format (uncompressed) = 4 bits (3)
			// 44100 rate = 2 bits (3)
			// 8bit samples = 1 bit (0)
			// mono sound = 1 bit (0)
			// 00111100 = 0x3C
			swf.Write((uint)wave.Length); // sample count (one byte=one sample)

			wave.Position = 0;

			swf.Write(new BinaryReader(wave).ReadBytes((int)wave.Length));


			// StartSound tag
			swf.writeTagInfo(15, 2 + 1);
			swf.Write((short)1);      // character id of the sound
			swf.Write((byte)0);       // SOUNDINFO flags (all 0)

			// End tag
			swf.writeTagInfo(0, 0);

			// Set size
			swf.BaseStream.Position = 4;
			swf.Write((uint)swf.BaseStream.Length);
			swf.BaseStream.Position = 0;

			// "load" it
			var ldr = new Loader();
			var u = swf.BaseStream.ToByteArray();

			u.position = 0;

			ldr.loadBytes(u);

		}
        public MySprite1()
        {
            // http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/display/Loader.html


            InternalLoadTargetContent = TargetContent =>
            {
                this.OrphanizeChildren();

                // read more: http://www.senocular.com/flash/tutorials/contentdomains/
                Security.allowDomain("*");
                Security.allowInsecureDomain("*");
                //Security.loadPolicyFile("http://www.youtube.com/crossdomain.xml");

                // http://code.google.com/apis/youtube/flash_api_reference.html
                // http://code.google.com/p/gdata-samples/source/browse/trunk/ytplayer/actionscript3/com/google/youtube/examples/AS3Player.as

                var ldr = new Loader();
                var urlReq = new URLRequest(TargetContent);

                var ctx_app = ApplicationDomain.currentDomain;
                var ctx_sec = SecurityDomain.currentDomain;

                if (TargetContent.StartsWith("http://www.youtube.com/"))
                {
                    // http://www.youtube.com/crossdomain.xml
                    ctx_app = null;
                    ctx_sec = null;

                    // http://www.zedia.net/2010/using-the-actionscript-3-youtube-api/

                    DoplayVideo = delegate
                    {
                        ldr.content.playVideo();
                    };


                    DoloadVideoById = (id, s, q) =>
                    {
                        ldr.content.loadVideoById(id, s, q);
                    };

                    Action<Event> onReady = e =>
                    {
                        if (VideoPlayerReady != null)
                            VideoPlayerReady();

#if JSC_FEATURE_dynamic
                        dynamic player = ldr.content;

                        player.setSize(160, 120);
#endif
                        ldr.content.setSize(160, 120);
                    };

                    ldr.contentLoaderInfo.init +=
                        delegate
                        {
                            ldr.content.addEventListener("onReady", onReady.ToFunction(), false, 0, false);
                        };
                }



                ldr.contentLoaderInfo.complete +=
                    delegate
                    {
                        if (Ready != null)
                            Ready();
                    };

                //ldr.mouseChildren = false;

                var ctx = new LoaderContext(true, ctx_app, ctx_sec);
                ldr.load(urlReq, ctx);

                var sprite2 = new Sprite
                {
                    z = 0.001
                }.AttachTo(this);


                sprite2.graphics.drawRect(0, 0, 100, 100);

                var t = new Timer(1000 / 60);

                t.timer +=
                    delegate
                    {
                        var x = sprite2.x;
                        var y = sprite2.y;

                        sprite2.transform.matrix3D.appendTranslation(-x, -y, 0);
                        sprite2.transform.matrix3D.appendRotation(0.01, Vector3D.Y_AXIS);
                        sprite2.transform.matrix3D.appendRotation(0.02, Vector3D.X_AXIS);
                        sprite2.transform.matrix3D.appendTranslation(x, y, 0);

                    };

                t.start();

                DoClean =
                    delegate
                    {

                        ldr.content.GetChildren().Where(k => k.GetType().Name == "InfoPanel").ToArray().WithEach(
                            k => k.Orphanize()
                        );

                    };

                ldr.AttachTo(sprite2);

                var Inspect = default(Action<DisplayObject, XElement>);

                Inspect = (Target, Journal) =>
                {

                    var SourceType = Target.GetType();

                    var n = new XElement(SourceType.Name);

                    n.Add(new XAttribute("Namespace", SourceType.Namespace));

                    SourceType.BaseType.With(
                        BaseType =>
                            n.Add(new XAttribute("BaseType", BaseType.FullName))
                    );


                    Journal.Add(n);

                    (Target as DisplayObjectContainer).With(
                        Container =>
                        {
                            for (int i = 0; i < Container.numChildren; i++)
                            {
                                Inspect(Container.getChildAt(i), n);
                            }
                        }
                    );

                };

                DoInspect =
                    delegate
                    {
                        var doc = new XElement("Inspection");

                        // SecurityError: Error #2121: Security sandbox violation: Loader.content: http://localhost:26925/assets/LoadExternalFlashComponent.Application/LoadExternalFlashComponent.Components.MySprite1.swf cannot access http://sketch.odopod.com/flash/OdoSketch.swf?sketchURL=/sketches/231498.xml&userURL=/users/21416&bgURL=/images/bigbg.jpg&mode=embed. This may be worked around by calling Security.allowDomain.
                        //	at flash.display::Loader/get content()

                        try
                        {
                            Inspect(ldr.content, doc);
                        }
                        catch (Exception exc)
                        {
                            var n = new XElement("error", exc.Message);

                            doc.Add(n);
                        }

                        if (Inspecting != null)
                            Inspecting(doc);
                    };

            };

            LoadTargetContent();
        }
        public MySprite1()
        {
            // http://apiwiki.justin.tv/mediawiki/index.php/Live_Video_SWF_Documentation
            Security.allowDomain("*");
            Security.allowInsecureDomain("*");

            // http://apiwiki.justin.tv/mediawiki/index.php/Live_Video_SWF_Documentation

            //var TargetContent = "http://www.justin.tv/widgets/live_api_player.swf?video_height=480&video_width=640&consumer_key=YOUR_API_KEY";

            var TargetContent = "http://www.justin.tv/widgets/live_api_player.swf?video_height=480&video_width=640";

            var ldr = new Loader();
            var urlReq = new URLRequest(TargetContent);

            var ctx_app = ApplicationDomain.currentDomain;
            var ctx_sec = SecurityDomain.currentDomain;

            ctx_sec = null;
            ctx_app = null;

            __api_play_live = delegate
            {
            };

            ldr.contentLoaderInfo.complete +=
                delegate
                {
                    __api_play_live =
                        channel =>
                        {
                            ldr.content.api_play_live(channel);
                        };

                    __api_play_live("nitro301");
                    //(ldr.content as dynamic).api.play_live("apidemo");
                };

            var ctx = new LoaderContext(true, ctx_app, ctx_sec);
            sprite2 = new Sprite { z = 0.02 }.AttachTo(this);

            sprite2.mouseChildren = false;

            ldr.AttachTo(sprite2);

            var t = new Timer(1000 / 60);

            t.timer +=
                delegate
                {
                    var x = sprite2.x;
                    var y = sprite2.y;

                    sprite2.transform.matrix3D.appendTranslation(-x, -y, 0);
                    sprite2.transform.matrix3D.appendRotation(0.01, Vector3D.Y_AXIS);
                    sprite2.transform.matrix3D.appendRotation(0.02, Vector3D.X_AXIS);
                    sprite2.transform.matrix3D.appendTranslation(x, y, 0);

                };

            t.start();

            try
            {
                ldr.load(urlReq, ctx);

            }
            catch
            {
            }
        }