<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="712" height="616" addedToStage="init()" xmlns:video="be.boulevart.video.*" resizeEffect="Resize" viewSourceURL="srcview/index.html">

<mx:Style source="appStyle.css"/>

    <mx:states>
        <mx:State name="flvBuild">
            <mx:SetProperty name="width" value="1048"/>
            <mx:AddChild position="lastChild">
            <mx:Panel title="Preview" x="698" y="10" height="312" layout="absolute">
                <mx:VideoDisplay x="0" y="0" width="320" height="240" id="playerFLV" complete="{onVideoDone()}"/>
                <mx:Button right="2" y="244" label="Stop" click="replay()" id='btnPlayStopPreview' visible="true" textAlign="right"/>
            </mx:Panel>
                
            </mx:AddChild>
        </mx:State>
    </mx:states>
    <mx:Panel id="content" x="15" y="10"   title="AIRCam - Offline recording FLVs" layout="absolute" height="582" width="680">
    
        <video:SmoothVideoPlayer id="vidDisplay"  maintainAspectRatio="true"  width="640" height="480" x="10" y="10" shadowDirection="left" shadowDistance="5" smoothing="true"/>    

        
        <mx:Button label="Record" id="btnRecord" click="recordMe()" textAlign="right" right="10" bottom="10" buttonMode="{btnRecord.enabled}" labelPlacement="right" icon="{bIcon}"/>
        <mx:ProgressBar x="10" y="508" label="Saving" id="pbSaving" labelPlacement="left" visible="false" mode="manual"  fontSize="12"/>
        
    </mx:Panel>
    
    
    <mx:Script>
        <![CDATA[
            import mx.events.CloseEvent;
            import mx.controls.Alert;
            import mx.messaging.channels.StreamingHTTPChannel;
            import be.boulevart.events.FLVRecorderEvent;
            import mx.collections.ArrayCollection;
            import mx.events.VideoEvent;
            import mx.binding.utils.BindingUtils;
            import be.boulevart.video.FLVRecorder;
            
            [Bindable]
            private var bIcon:Class
            
            [Embed(source="assets/recrd.png")]
            private var recordIcon:Class
            
            [Embed(source="assets/stop.png")]
            private var stopIcon:Class
            
            private var cam:Camera;
            private var recorder:FLVRecorder
            private var file:File;
            private var isRecording:Boolean=false
            
            private var bmpds:ArrayCollection;
            private var times:int=1
            
            private var fps:int
            
            private function init():void{
                if(Camera.names.length>0){
                    cam =Camera.getCamera()
                    fps=cam.fps
                    cam.setQuality(0,100)
                    
                    vidDisplay.live=true
                    vidDisplay.attachCamera(cam)
                    
                    bIcon=recordIcon
                }else{
                    Alert.okLabel="Close"
                    var a:Alert=Alert.show("Sorry, you don't have a webcam on your system, this application is useless to you.","Sorry",Alert.OK,this,handlesNoCams)
                }
            }
            
            private function handlesNoCams(e:CloseEvent):void{
                System.exit(0)
            }
            
            
            
            private function onVideoDone():void{
                //if(sr){
                    btnPlayStopPreview.label="Play"
                    btnPlayStopPreview.visible=true
                    playerFLV.stop()
                //}
            }
            
            private var sr:Boolean=false
            private function replay(){
                sr=false
                
                if(playerFLV.playing){
                    btnPlayStopPreview.label="Play"
                        
                    playerFLV.stop()
                    
                }else{
                
                    btnPlayStopPreview.label="Stop"
                        playerFLV.play()
                }
                
                sr=true
            }
            
            private var tmr:Timer
            private function startRecording():void{
                
                isRecording=true
                bmpds=new ArrayCollection()
                
                this.currentState=""
                this.width=712
                
                if(recorder==null){
                    recorder=FLVRecorder.getInstance()
                }
                
                file=File.desktopDirectory.resolvePath("recording"+times+".flv");
                
                recorder.setTarget(file,640,480,fps,systemManager)
                
                if(tmr==null){
                    tmr=new Timer(1000/fps)
                    
                }
                tmr.addEventListener(TimerEvent.TIMER,function():void{record()})
                tmr.start()
            }
            
            private function record():void{
                if(isRecording){
                    recorder.captureComponent(vidDisplay)
                }else{
                    trace("oeps, not recording")
                }
            }
            
            private function stopRecording():void{
                isRecording=false
                tmr.stop()
                recorder.stopRecording()
                recorder.addEventListener(FLVRecorderEvent.FLV_CREATED,fileMade)
                recorder.addEventListener(FLVRecorderEvent.FLV_START_CREATION,startCreatingFLV)
                times++    
            }
            
            private function fileMade(e:FLVRecorderEvent):void{
                this.currentState="flvBuild"
                
                recorder.removeEventListener(FLVRecorderEvent.PROGRESS,onFLVCreationProgress)
                recorder=null
                
                pbSaving.visible=false
                pbSaving.setProgress(0,1)
                btnRecord.enabled=true
                playerFLV.source=e.url
                
            }
            private function startCreatingFLV(e:FLVRecorderEvent):void{
                btnRecord.enabled=false
                //e.progress: percent complete (0-1)
                recorder.addEventListener(FLVRecorderEvent.PROGRESS,onFLVCreationProgress)
                
            }
            
            private function onFLVCreationProgress(e:FLVRecorderEvent):void{
                pbSaving.visible=true
                pbSaving.setProgress(e.progress,1)
            }
            
            
            private function recordMe():void{
                if(isRecording){
                    btnRecord.label="Record"
                      bIcon=recordIcon
                    stopRecording()
                }else{
                      btnRecord.label="Stop"
                      bIcon=stopIcon
                    startRecording()
                }
            }
            
        ]]>
    </mx:Script>
    
</mx:WindowedApplication>