hackedteam/rcs-console

View on GitHub
src/it/ht/rcs/console/operations/view/evidences/advanced/viewers/AudioPlayer.mxml

Summary

Maintainability
Test Coverage
<?xml version="1.0" encoding="utf-8"?>
<s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009"
                    xmlns:s="library://ns.adobe.com/flex/spark"
                    xmlns:mx="library://ns.adobe.com/flex/mx"
                    xmlns:utils="it.ht.rcs.console.utils.*"
                    xmlns:viewers="it.ht.rcs.console.operations.view.evidences.advanced.viewers.*"
                    height="100%"
                    width="100%"
                    creationComplete="init()">
    <fx:Script>
        <![CDATA[
            import flashx.textLayout.conversion.TextConverter;
            import it.ht.rcs.console.utils.TimeUtils;

            [Bindable]
            [Embed(source="/img/mediaplayers/play.png")]
            public var PlayIcon:Class;
            [Bindable]
            [Embed(source="/img/mediaplayers/pause.png")]
            public var PauseIcon:Class;
            [Bindable]
            [Embed(source="/img/mediaplayers/stop.png")]
            public var StopIcon:Class;
            [Bindable]
            [Embed(source="/img/mediaplayers/speaker.png")]
            public var SpeakerIcon:Class;
      
      [Bindable]
      [Embed(source="/img/mediaplayers/recording.swf")]
      public var RecordingIcon:Class;


            private var _sound:Sound;
            private var _channel:SoundChannel;
            private var _playingTime:int;
            private var _looper:Timer=new Timer(0, 0);
            private var _pausePoint:Number=0;
            private var _isPlaying:Boolean=false;
            private var _soundTransform:SoundTransform=new SoundTransform();

            [Bindable]
            private var _volume:Number=1;
            [Bindable]
            private var _pan:Number=0;
            [Bindable]
            private var _url:String;
      
      
    
      private var _date:int; //TimeUtils.timestampFormatter(evidence.da * 1000)

      [Bindable]
      public function get date():int
      {
        return _date;
      }

      public function set date(value:int):void
      {
        _date = value;
      }

            public function set url(value:String):void
            {
                _url=value;
            }

            public function get url():String
            {
                return _url;
            }

            private function init():void
            {
                _looper.addEventListener(TimerEvent.TIMER, onTime);

                //styles

                spectrumMonitor.addEventListener(SoundSpectrumMonitor.SELECTION, onSpectrumSelection);

            }

            [BINDABLE]
            public function set sound(value:String):void
            {

                stopBtn.enabled=false;
                playBtn.enabled=false;
                removeEventListener(Event.ENTER_FRAME, update);
                _sound=new Sound();
                _sound.addEventListener(ProgressEvent.PROGRESS, onSoundLoading);
                _sound.addEventListener(IOErrorEvent.IO_ERROR, onSoundError)
                _sound.addEventListener(Event.COMPLETE, onSoundLoaded);
                _sound.load(new URLRequest(value));
                spectrumMonitor.scaleX=1;
                spectrumMonitor.clear();

                _looper.stop();
                _volume=1;
                _pan=0;
                _soundTransform.volume=_volume;
                _soundTransform.pan=_pan;

                if (_channel)
                {
                    _channel.stop();
                    _channel.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete)
                    _channel.soundTransform=_soundTransform;
                }

                volumeControl.value=100;
                panControl.value=0;
                durationTxt.text="00:00:00";
        timeTracker.reset()
        dateTxt.text=TimeUtils.timestampFormatter(date * 1000)
       
            }

            private function onSoundError(e:IOErrorEvent):void
            {
                trace("sound error");
                _sound.removeEventListener(IOErrorEvent.IO_ERROR, onSoundError);
                timeTxt.text="Unable to load sound data";
        dateTxt.text=TimeUtils.timestampFormatter(date * 1000)
            }

            private function onSoundLoading(e:ProgressEvent):void
            {
                //trace(e.bytesLoaded)
                //trace(e.bytesTotal)
                var perc:int=(e.bytesLoaded / e.bytesTotal) * 100;
                timeTxt.text="Loading sound data..." + perc + "%";
        dateTxt.text=TimeUtils.timestampFormatter(date * 1000)
            }

            private function onSoundLoaded(e:Event):void
            {
                _sound.removeEventListener(ProgressEvent.PROGRESS, onSoundLoading)
                _sound.removeEventListener(IOErrorEvent.IO_ERROR, onSoundError);
                _sound.removeEventListener(Event.COMPLETE, onSoundLoaded);
                _playingTime=_sound.length;

                spectrumMonitor.addEventListener(SpectrumMonitor.READY, onSpectrumReady)
                spectrumMonitor.draw(_sound);
                timeTxt.text="00:00:00"
          dateTxt.text=TimeUtils.timestampFormatter(date * 1000)
          
        timeTracker.totalTime=_sound.length
        timeTracker.draw()
        trace(timeTracker.totalTime)

            }

            private function onSpectrumReady(e:Event):void
            {
                addEventListener(Event.ENTER_FRAME, update);
                playBtn.toolTip="pause";
                playBtn.source=PauseIcon;
                playBtn.enabled=true;
                stopBtn.enabled=true;
                _channel=_sound.play();
                _channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete)
                _channel.soundTransform=_soundTransform;
                _isPlaying=true;
            }

            private function onSoundComplete(e:Event):void
            {
                stop();
            }

            private function onTime(e:TimerEvent):void
            {
                _channel.stop();
                _channel=_sound.play(spectrumMonitor.selectionRect.x * spectrumMonitor.ratio);
            }

            private function update(e:Event):void
            {
                timeTxt.text=TimeUtils.formatTime(_channel.position)
                durationTxt.text=TimeUtils.formatTime(_playingTime)
                spectrumMonitor.update(_channel.position)
                _soundTransform.volume=_volume;
                _soundTransform.pan=_pan;
                _channel.soundTransform=_soundTransform;
        
        dateTxt.text=TimeUtils.timestampFormatter((date * 1000)+_channel.position)
            }

            private function onSpectrumSelection(e:Event):void
            {
                _channel.stop();
                var monitor:SpectrumMonitor=e.currentTarget as SpectrumMonitor;
                _channel=_sound.play(monitor.selectionRect.x * monitor.ratio);
                _channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete)
                _isPlaying=true;
                playBtn.toolTip="pause";
                playBtn.source=PauseIcon;
                addEventListener(Event.ENTER_FRAME, update);
                if (monitor.selectionRect.width)
                {
                    _looper.delay=monitor.ratio * monitor.selectionRect.width;
                    _looper.start();

                }
                else
                    _looper.stop();

            }

            private function onPlayClick(e:MouseEvent):void
            {
                if (_isPlaying)
                {
                    _pausePoint=_channel.position;
                    _channel.stop();
                    _channel.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete)
                    _isPlaying=false;
                    playBtn.toolTip="play";
                    playBtn.source=PlayIcon;
                    _looper.stop();
                    removeEventListener(Event.ENTER_FRAME, update);
                }
                else
                {
                    _channel=_sound.play(_pausePoint);
                    _channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete)
                    _isPlaying=true;
                    playBtn.toolTip="pause";
                    playBtn.source=PauseIcon;
                    if (spectrumMonitor.selectionRect.width)
                        _looper.start();
                    addEventListener(Event.ENTER_FRAME, update);
                }
            }

            private function onStopClick(e:MouseEvent):void
            {
                stop()
            }

            private function stop():void
            {
                _pausePoint=0;
                _channel.stop();
                _isPlaying=false;
                playBtn.toolTip="play";
                playBtn.source=PlayIcon;
                spectrumMonitor.update(0);
                removeEventListener(Event.ENTER_FRAME, update);
                _looper.stop();
                timeTxt.text="00:00:00";
                spectrumMonitor.clearSelection();
        
        dateTxt.text=TimeUtils.timestampFormatter(date * 1000)
            }

            private function onVolumeChange():void
            {
                _volume=volumeControl.value / 100;

            }

            private function onPanChange():void
            {
                _pan=panControl.value * -1;
                spectrumMonitor.pan(_pan)

            }

            public function reset():void
            {

                if (_channel)
                {
                    _channel.stop();

                }
                stopBtn.enabled=false;
                playBtn.enabled=false;
                removeEventListener(Event.ENTER_FRAME, update);
                spectrumMonitor.clear();
                _looper.stop();
                if (_sound)
                {
                    _sound.removeEventListener(ProgressEvent.PROGRESS, onSoundLoading);
                    _sound.removeEventListener(IOErrorEvent.IO_ERROR, onSoundError)
                    _sound.removeEventListener(Event.COMPLETE, onSoundLoaded);
                }
        timeTracker.reset()
            }

            private function volumeDataTipFunction(value:Number):String
            {
                return String(value / 100);
            }
        ]]>
    </fx:Script>

    <s:HGroup width="100%"
                        height="100%">

        <s:VGroup width="100%" height="100%" gap="0">
      
            <viewers:SpectrumMonitor id="spectrumMonitor"
                                                             visible="true"
                                                             width="100%"/>
        <viewers:TimeTracker id="timeTracker" width="100%"/>
        </s:VGroup>


        <s:VGroup height="100%">

            <s:Label text="L"/>
            <s:VSlider id="panControl"
                                 skinClass="it.ht.rcs.console.operations.view.evidences.advanced.viewers.skins.PanBarSkin"
                                 height="100%"
                                 enabled="true"
                                 minimum="-1"
                                 maximum="1"
                                 change="onPanChange()"
                                 stepSize="0.1"
                                 value="0"
                                 width="10"
                                 showDataTip="false"/>
            <s:Label text="R"/>
        </s:VGroup>

    </s:HGroup>

    <s:BorderContainer width="100%"
                                         height="50"
                                         borderColor="0xCCCCCC"
                                         backgroundColor="0xFFFFFF"
                                         backgroundAlpha="0"
                                         borderAlpha="0">
        <s:layout>
            <s:HorizontalLayout verticalAlign="middle"
                                                    horizontalAlign="right"/>
        </s:layout>
        <s:HGroup>
            <s:Line height="100%">
                <s:stroke>
                    <s:SolidColorStroke color="0xCCCCCC"/>
                </s:stroke>
            </s:Line>
            <s:Image source="{PlayIcon}"
                             buttonMode="true"
                             useHandCursor="true"
                             toolTip="play"
                             click="onPlayClick(event)"
                             id="playBtn"/>
            <s:Line height="100%">
                <s:stroke>
                    <s:SolidColorStroke color="0xCCCCCC"/>
                </s:stroke>
            </s:Line>
            <s:Image source="{StopIcon}"
                             buttonMode="true"
                             useHandCursor="true"
                             toolTip="stop"
                             click="onStopClick(event)"
                             id="stopBtn"/>
            <s:Line height="100%">
                <s:stroke>
                    <s:SolidColorStroke color="0xCCCCCC"/>
                </s:stroke>
            </s:Line>
        </s:HGroup>
        <s:HGroup verticalAlign="middle">
            <s:BitmapImage source="{SpeakerIcon}"/>
            <s:HSlider id="volumeControl"
                                 skinClass="it.ht.rcs.console.operations.view.evidences.advanced.viewers.skins.VolumeBarSkin"
                                 enabled="true"
                                 minimum="0"
                                 maximum="400"
                                 change="onVolumeChange()"
                                 stepSize="1"
                                 dataTipFormatFunction="volumeDataTipFunction"
                                 value="100"/>
        </s:HGroup>

        <s:Spacer width="100%"/>
        <s:RichText id="timeTxt"
                                text="00:00:00"
                                fontSize="24"/>
        <s:Spacer width="10"/>
        <s:Line height="50%">
            <s:stroke>
                <s:SolidColorStroke color="0xCCCCCC"/>
            </s:stroke>
        </s:Line>
        <s:Spacer width="10"/>
        <s:RichText id="durationTxt"
                                text="00:00:00"
                                fontSize="24" />
    <s:Spacer width="100%"/>
    <s:RichText id="dateTxt"
                
                fontSize="24" color="0xCCCCCC"/>
    </s:BorderContainer>
    <s:HGroup verticalAlign="middle"
                        width="100%"
                        horizontalAlign="center">
    </s:HGroup>
</s:VGroup>