<?xml version="1.0"?>
<!--                                                               -->
<!-- Adobe Flex 4 source file of VPlayer.swf,                      -->
<!-- a FlashPlayer-10 compatible component for playing             -->
<!-- FLV and MP4/H.264 video files and streams.                    -->
<!--                                                               -->
<!-- version 20120425                                              -->
<!--                                                               -->
<!--                                                               -->
<!-- The free Adobe Flex 4 SDK is required to compile              -->
<!-- this file. Get it from                                        -->
<!--                                                               -->
<!--        http://www.adobe.com/products/flex/                    -->
<!--                                                               -->
<!-- and run                                                       -->
<!--                                                               -->
<!-- mxmlc -static-link-runtime-shared-libraries VPlayer.mxml      -->
<!--                                                               -->
<!-- on the command line.                                          -->
<!--                                                               -->
<!--                                                               -->
<!-- Copyright (C) 2012 Alexander Grahn                            -->
<!--                                                               -->
<!-- This work may be distributed and/or modified under the        -->
<!-- conditions of the LaTeX Project Public License, either        -->
<!-- version 1.3 of this license or (at your option) any later     -->
<!-- version.                                                      -->
<!--                                                               -->
<!-- The latest version of this license is in                      -->
<!--   http://www.latex-project.org/lppl.txt                       -->
<!-- and version 1.3 or later is part of all distributions of      -->
<!-- LaTeX version 2005/12/01 or later.                            -->
<!--                                                               -->
<!-- This work has the LPPL maintenance status `maintained'.       -->
<!--                                                               -->
<!-- The current maintainer of this work is A. Grahn.              -->
<!--                                                               -->

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx"
    preinitialize="initialise(FlexGlobals.topLevelApplication.parameters);"
    creationComplete="initCallBacks();"
>
    <fx:Script>
        <![CDATA[
            [Bindable] private var scaleMode:String='stretch';
            [Bindable] private var source:String;
            [Bindable] private var autoPlay:Boolean=false;
            [Bindable] private var loop:Boolean=false;
            [Bindable] private var vol:Number=0.75;
            private var vidComplete:Boolean=false;

            import mx.core.FlexGlobals;
            private function initialise(flashVars:Object):void {
              source=flashVars.source;
              if(flashVars.autoPlay){autoPlay=(flashVars.autoPlay=='true')}
              if(flashVars.loop){loop=(flashVars.loop=='true')}
              if(flashVars.volume){vol=Number(flashVars.volume)}
              if(flashVars.scaleMode){scaleMode=flashVars.scaleMode}
            }

            import flash.events.ProgressEvent;
            private function progressHandler(event:ProgressEvent):void {
              if (event.bytesTotal) event.target.visible=true;
              else event.target.visible=false;
            }

            import org.osmf.events.MediaPlayerStateChangeEvent;
            import mx.controls.Alert;
            private function stateChangeHandler(event:MediaPlayerStateChangeEvent):void {
              vidComplete=false;
              if(event.state=='playbackError')
                Alert.show('Unable to play \''+event.target.source+'\'','Error');
            }

            private function play():void {
              if(vidComplete){vidDisp.seek(0);}vidDisp.play();
            }

            private function pause():void {
              vidDisp.pause();
            }

            private function playPause():void {
              if(vidDisp.playing) vidDisp.pause(); else vidDisp.play();
            }

            private function seek(p:Number):void {
              vidDisp.seek(p);
            }

            private function rewind():void {
              vidDisp.seek(0);
            }

            private function volume(v:Number):void {
              vidDisp.volume = v;
            }

            private function mute():void {
              if(vidDisp.muted) {
                vidDisp.muted=false;
                if (vidDisp.volume==0) volume(0.75);
              }
              else {
                vidDisp.muted=true;
              }
            }

            private function setSource(s:Object):void {
              vidDisp.source = s;
            }

            private function initCallBacks():void {
              ExternalInterface.addCallback("play", play);
              ExternalInterface.addCallback("pause", pause);
              ExternalInterface.addCallback("playPause", playPause);
              ExternalInterface.addCallback("seek", seek);
              ExternalInterface.addCallback("rewind", rewind);
              ExternalInterface.addCallback("volume", volume);
              ExternalInterface.addCallback("mute", mute);
              ExternalInterface.addCallback("setSource", setSource);
            }
        ]]>
    </fx:Script>

    <s:VideoDisplay
        id="vidDisp"
        width="100%" height="100%" scaleMode="{scaleMode}"
        source="{source}" volume="{vol}"
        autoPlay="{autoPlay}" autoRewind="false" loop="{loop}"
        mouseDown="vidDisp.pause();"
        mouseUp="if(vidComplete){vidDisp.seek(0);}vidDisp.play();"
        mediaPlayerStateChange="stateChangeHandler(event);"
        complete="vidComplete=true;"
        durationChange="if(vidDisp.autoPlay) vidDisp.play(); else vidDisp.seek(0);"
    />

    <mx:ProgressBar width="80%" mode="polled" source="vidDisp"
        horizontalCenter="0" verticalCenter="0" labelPlacement="center"
        id="loadingProgress" alpha="0.5"
        complete="loadingProgress.visible=false;"
        progress="progressHandler(event)"
    />
</s:Application>
