In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Basic video player with controls

Get Adobe Flash player
by adamh 21 Jun 2012
    Embed
/**
 * Copyright adamh ( http://wonderfl.net/user/adamh )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/wsVP
 */

package {
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.net.navigateToURL;
    import flash.net.URLRequest;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.SimpleButton;
    import flash.system.LoaderContext;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.media.Video;
    import flash.media.SoundTransform;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.external.ExternalInterface;
    
    public class FlashTest extends Sprite {
   
        private var nc:NetConnection; 
        private var ns:NetStream; 
        private var vid:Video; 
        private var client:Object;
        
        private var pp_btn:CustomSimpleButton;
        private var restart_btn:CustomSimpleButton;
        private var volume_btn:CustomSimpleButton;
        private var close_btn:CustomSimpleButton;
        private var view_btn:CustomSimpleButton;
        
        private var icon_bmp_data:Bitmap;
        
        private var isPlaying:Boolean = false;
        private var isMuted:Boolean = true;
        private var isHidden:Boolean = true; 
        
        private var flashvars:Object;
        
        public function FlashTest() {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            flashvars = LoaderInfo(this.root.loaderInfo).parameters;
            
            //Sort out the icons
            var ldr:Loader = new Loader();
            var context:LoaderContext = new LoaderContext();
            context.checkPolicyFile = true;
            ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, function():void {
                trace(ldr.content);
                icon_bmp_data = ldr.content as Bitmap;
                addButtons();
            });
            ldr.load(new URLRequest( flashvars.icons || "http://mdschmd.com/dior_underlay/icons.png" ),context);
              
            //Sort out the video
            nc = new NetConnection(); 
            nc.connect (null); // Not using a media server. 
            ns = new NetStream(nc);
            
            vid = new Video(); 
            vid.x = 0; 
            vid.y = 0;
            vid.attachNetStream ( ns ); 
            
            client = new Object();
            client.onPlayStatus = function(info:Object):void {
                if(info.code == 'NetStream.Play.Complete') {
                    ns.seek(0);
                    pauseVid();                 
                }
            };
            
            ns.client = client;
            setMuted(true);
            ns.play ( flashvars.video || 'http://fileserver.glam.com/adamh/dior_underlay/Dior_Addict_v2.mp4' );
            isPlaying = true;
            track('start', 'videoplay');
            
            addChild (vid);
            
            //Manage clickout
            if(flashvars.clickout) {
                vid.addEventListener(MouseEvent.CLICK, function():void {
                    track('', 'exit', 'video');
                    navigateToURL(new URLRequest(flashvars.clickout), "_blank");
                });
            } 
            
            layout();
            stage.addEventListener(Event.RESIZE, layout);
        }
        
        private function track(type:String, name:String, code:String = ""):void {
            var method:String = type == 'start' ? 'startTimer' : type == 'stop' ? 'stopTimer' : 'track';
            try { ExternalInterface.call(flashvars.tracker+'.'+method, name, code); } catch(e:Object){}
        }
        
        private function pauseVid():void {
            isPlaying = false;
            ns.pause();
            pp_btn.switchIcon(makeIcon(icon_bmp_data,32,32, 0));
            track('stop', 'videoplay');
        }

        private function playVid():void {
            isPlaying = true;
            ns.resume();
            pp_btn.switchIcon(makeIcon(icon_bmp_data,32,32, 1));
            track('start', 'videoplay');
        }
        
        private function setMuted(shouldMute:Boolean):void {
            var st:SoundTransform = new SoundTransform();
            st.volume = shouldMute ? 0 : 1;
            ns.soundTransform = st;
            isMuted = shouldMute;
            if(volume_btn) volume_btn.switchIcon(makeIcon(icon_bmp_data,32,32, isMuted ? 4 : 3));
        }

         
        private function makeIcon(bmd:Bitmap,width:uint,height:uint,index:uint):Bitmap {
            var p:Point = new Point();
            var rect:Rectangle = new Rectangle(0, 0, width, height);
            var new_bmd:BitmapData = new BitmapData(width, height);
            rect.x = 0;
            rect.y = height * index;
            new_bmd.copyPixels(bmd.bitmapData, rect, p);
            return new Bitmap(new_bmd);
        }

        private function addButtons():void {
            //PlayPause button
            pp_btn = new CustomSimpleButton(makeIcon(icon_bmp_data,32,32,1));
            pp_btn.addEventListener(MouseEvent.CLICK, function():void {
                if(isPlaying) pauseVid();
                else playVid();
            });
            addChild(pp_btn);
            
            //Restart button
            restart_btn = new CustomSimpleButton(makeIcon(icon_bmp_data,32,32,2));
            restart_btn.addEventListener(MouseEvent.CLICK, function():void {
                ns.seek(0);
                track('', 'counter', 'replay');
            });
            addChild(restart_btn);
            
            //Volume button
            volume_btn = new CustomSimpleButton(makeIcon(icon_bmp_data,32,32, isMuted ? 4 : 3));
            volume_btn.addEventListener(MouseEvent.CLICK, function():void {
                setMuted(!isMuted);
                track('', 'counter', isMuted ? 'mute' : 'unmute');
            });
            addChild(volume_btn);
            
            //Close button
            close_btn = new CustomSimpleButton(makeIcon(icon_bmp_data,32,32, 5));
            close_btn.addEventListener(MouseEvent.CLICK, function():void {
                track('', 'counter', 'close');
                ExternalInterface.call(flashvars.fn_cntrl, "close");
            });
            addChild(close_btn);
            
            //View button
            view_btn = new CustomSimpleButton(null, "Show", 64);
            view_btn.addEventListener(MouseEvent.CLICK, function():void {
                ExternalInterface.call(flashvars.fn_cntrl, isHidden ? "show" : "hide");
                track('', 'counter', isHidden ? "show" : "hide");
                isHidden = !isHidden;
                view_btn.switchIcon(null, isHidden ? "Show" : "Hide");
            });
            addChild(view_btn);
            
            layout();
        }

        private function layout(e:Event = null):void {
            vid.width = stage.stageWidth;
            vid.height = stage.stageWidth * (9/16);
            
            if(pp_btn) pp_btn.x = stage.stageWidth - (32 * 3);
            if(restart_btn) restart_btn.x = stage.stageWidth - (32 * 2);
            if(volume_btn) volume_btn.x = stage.stageWidth - (32 * 1);
            if(close_btn) {
                close_btn.x = stage.stageWidth - (32 * 1);
                close_btn.y = 32 * 1;
            }
            if(view_btn) {
                view_btn.x = stage.stageWidth - (32 * 3);
                view_btn.y = 32 * 1;
            }
        }
    }
}
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.display.Bitmap;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.BitmapData;

class CustomSimpleButton extends SimpleButton {
    private var upColor:uint   = 0x666666;
    private var overColor:uint = 0x777777;
    private var downColor:uint = 0x888888;
    private var w:uint      = 32;
    private var h:uint      = 32;

    public function CustomSimpleButton(bmp:Bitmap = null, txt:String = null, w:uint = 32, h:uint = 32) {
        useHandCursor  = true;
        this.w = w;
        this.h = h;
        switchIcon(bmp, txt);
    }
    
    public function switchIcon(bmp:Bitmap = null, txt:String = null):void {
        downState      = new ButtonDisplayState(downColor, w, h, bmp, txt);
        overState      = new ButtonDisplayState(overColor, w, h, bmp, txt);
        upState        = new ButtonDisplayState(upColor, w, h, bmp, txt);
        hitTestState   = new ButtonDisplayState(upColor, w, h);
    }

}

class ButtonDisplayState extends Shape {
    private var bgColor:uint;
    private var w:uint      = 32;
    private var h:uint      = 32;
    private var txtFormat:TextFormat = new TextFormat();

    public function ButtonDisplayState(bgColor:uint, w:uint, h:uint, bmp:Bitmap = null, txt:String = null) {
        this.bgColor = bgColor;
        this.w = w;
        this.h = h;
        draw(bmp, txt);
    }

    private function draw(bmp:Bitmap, txt:String):void {
        graphics.beginFill(bgColor);
        graphics.drawRoundRect(0, 0, w, h, 20);
        graphics.endFill();
        if(bmp) {
            graphics.beginBitmapFill(bmp.bitmapData, null, false);
            graphics.drawRect(0, 0, w, h);
            graphics.endFill();
        }
        if(txt) {
            var textfield:TextField = new TextField;
            textfield.text = txt;
            txtFormat.color = 0xFFFFFF;
            txtFormat.size = 22;
            txtFormat.font = "_sans";
            textfield.setTextFormat(txtFormat);
            
            var bitmapdata:BitmapData = new BitmapData(w, h, true, 0x00000000);
            bitmapdata.draw(textfield);
            
            graphics.beginBitmapFill(bitmapdata);
            graphics.drawRect(0, 0, w, h);
            graphics.endFill();
        }
    }
}