| 1 | /** |
|---|
| 2 | * Plugin for playing closed captions and a closed audiodescription with a video. |
|---|
| 3 | **/ |
|---|
| 4 | package com.jeroenwijering.plugins { |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import com.jeroenwijering.events.*; |
|---|
| 8 | import com.jeroenwijering.parsers.SRTParser; |
|---|
| 9 | import com.jeroenwijering.parsers.TTParser; |
|---|
| 10 | import com.jeroenwijering.utils.Configger; |
|---|
| 11 | |
|---|
| 12 | import flash.display.MovieClip; |
|---|
| 13 | import flash.events.Event; |
|---|
| 14 | import flash.events.MouseEvent; |
|---|
| 15 | import flash.external.ExternalInterface; |
|---|
| 16 | import flash.media.*; |
|---|
| 17 | import flash.net.URLLoader; |
|---|
| 18 | import flash.net.URLRequest; |
|---|
| 19 | import flash.text.*; |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | public class Accessibility extends MovieClip implements PluginInterface { |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | /** List with configuration settings. **/ |
|---|
| 26 | public var config:Object = { |
|---|
| 27 | audio:undefined, |
|---|
| 28 | captions:undefined, |
|---|
| 29 | fontsize:15, |
|---|
| 30 | hide:false, |
|---|
| 31 | listener:undefined, |
|---|
| 32 | mute:false, |
|---|
| 33 | volume:90 |
|---|
| 34 | } |
|---|
| 35 | /** Reference to the MVC view. **/ |
|---|
| 36 | private var view:AbstractView; |
|---|
| 37 | /** XML connect and parse object. **/ |
|---|
| 38 | private var loader:URLLoader; |
|---|
| 39 | /** The array the captions are loaded into. **/ |
|---|
| 40 | private var captions:Array; |
|---|
| 41 | /** Textformat entry for the captions. **/ |
|---|
| 42 | private var format:TextFormat; |
|---|
| 43 | /** Displayelement to load the captions into. **/ |
|---|
| 44 | private var clip:MovieClip; |
|---|
| 45 | /** Currently active caption. **/ |
|---|
| 46 | private var current:Number; |
|---|
| 47 | /** sound object to be instantiated. **/ |
|---|
| 48 | private var sound:Sound; |
|---|
| 49 | /** Sound channel object. **/ |
|---|
| 50 | private var channel:SoundChannel; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | public function Accessibility(clp:MovieClip=null):void { |
|---|
| 54 | if(clp) { |
|---|
| 55 | clip = clp; |
|---|
| 56 | } else { |
|---|
| 57 | clip = this['accessibility']; |
|---|
| 58 | } |
|---|
| 59 | loader = new URLLoader(); |
|---|
| 60 | loader.addEventListener(Event.COMPLETE,loaderHandler); |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | /** Show/hide the captions **/ |
|---|
| 65 | public function hide(stt:Boolean):void { |
|---|
| 66 | config['hide'] = stt; |
|---|
| 67 | Configger.saveCookie('accessibility.hide',config['hide']); |
|---|
| 68 | clip.visible = config['hide']; |
|---|
| 69 | if(config['hide']) { |
|---|
| 70 | clip.captionsIcon.alpha = 1; |
|---|
| 71 | } else { |
|---|
| 72 | clip.captionsIcon.alpha = 0.3; |
|---|
| 73 | } |
|---|
| 74 | }; |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | /** Clicking the hide button. **/ |
|---|
| 78 | private function hideClick(evt:MouseEvent=null):void { |
|---|
| 79 | hide(!config['hide']); |
|---|
| 80 | }; |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | /** Initing the plugin. **/ |
|---|
| 84 | public function initializePlugin(vie:AbstractView):void { |
|---|
| 85 | view = vie; |
|---|
| 86 | if(view.config['audio']) { config['audio'] = view.config['audio']; } |
|---|
| 87 | if(view.config['captions']) { config['captions'] = view.config['captions']; } |
|---|
| 88 | if(!config['captions'] && !config['audio']) { |
|---|
| 89 | clip.visible = false; |
|---|
| 90 | return; |
|---|
| 91 | } |
|---|
| 92 | view.addControllerListener(ControllerEvent.ITEM,itemHandler); |
|---|
| 93 | view.addControllerListener(ControllerEvent.RESIZE,resizeHandler); |
|---|
| 94 | view.addModelListener(ModelEvent.TIME,timeHandler); |
|---|
| 95 | view.addModelListener(ModelEvent.STATE,stateHandler); |
|---|
| 96 | clip.tf.autoSize = TextFieldAutoSize.CENTER; |
|---|
| 97 | format = new TextFormat(null,config['fontsize']); |
|---|
| 98 | hide(config['hide']); |
|---|
| 99 | mute(config['mute']); |
|---|
| 100 | try { |
|---|
| 101 | setButtons(); |
|---|
| 102 | } catch (erro:Error) { |
|---|
| 103 | clip.audioIcon.visible = clip.captionsIcon.visible = false; |
|---|
| 104 | } |
|---|
| 105 | try { |
|---|
| 106 | ExternalInterface.addCallback("hideCaptions",hideClick); |
|---|
| 107 | ExternalInterface.addCallback("muteAudio",muteClick); |
|---|
| 108 | } catch (err:Error) {} |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /** Check for captions with a new item. **/ |
|---|
| 113 | private function itemHandler(evt:ControllerEvent=null):void { |
|---|
| 114 | setCaption(-1); |
|---|
| 115 | captions = new Array(); |
|---|
| 116 | var cap:String = view.playlist[view.config['item']]['captions']; |
|---|
| 117 | if(cap) { config['captions'] = cap; } |
|---|
| 118 | if(config['captions']) { |
|---|
| 119 | try { |
|---|
| 120 | loader.load(new URLRequest(config['captions'])); |
|---|
| 121 | } catch (err:Error) { |
|---|
| 122 | view.sendEvent('ERROR','Captions: '+err.message); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | var aud:String = view.playlist[view.config['item']]['audio']; |
|---|
| 126 | if(aud) { config['audio'] = aud; } |
|---|
| 127 | if(config['audio']) { setAudio(); } |
|---|
| 128 | }; |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | /** Captions are loaded; now display them. **/ |
|---|
| 132 | private function loaderHandler(evt:Event):void { |
|---|
| 133 | if(config['captions'].substr(-3) == 'srt') { |
|---|
| 134 | captions = SRTParser.parseCaptions(String(evt.target.data)); |
|---|
| 135 | } else { |
|---|
| 136 | captions = TTParser.parseCaptions(XML(evt.target.data)); |
|---|
| 137 | } |
|---|
| 138 | if(captions.length == 0) { |
|---|
| 139 | view.sendEvent('TRACE','Acessibility: Captions are not a valid TimedText or SRT file.'); |
|---|
| 140 | } |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | /** Mute/unmute the audiodesc. **/ |
|---|
| 145 | public function mute(stt:Boolean):void { |
|---|
| 146 | config['mute'] = stt; |
|---|
| 147 | Configger.saveCookie('accessibility.mute',config['mute']); |
|---|
| 148 | setVolume(); |
|---|
| 149 | if(config['mute']) { |
|---|
| 150 | clip.audioIcon.alpha = 0.3; |
|---|
| 151 | } else { |
|---|
| 152 | clip.audioIcon.alpha = 1; |
|---|
| 153 | } |
|---|
| 154 | }; |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | /** Clicking the hide button. **/ |
|---|
| 158 | private function muteClick(evt:MouseEvent=null):void { |
|---|
| 159 | mute(!config['mute']); |
|---|
| 160 | }; |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | /** Resize the captions if the display changes. **/ |
|---|
| 164 | private function resizeHandler(evt:ControllerEvent=undefined):void { |
|---|
| 165 | clip.width = view.config['width']; |
|---|
| 166 | clip.scaleY = clip.scaleX; |
|---|
| 167 | clip.y = view.config['height']-clip.height-config['fontsize']*clip.scaleX; |
|---|
| 168 | }; |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | /** Set the audidescription volume level. **/ |
|---|
| 172 | private function setAudio():void { |
|---|
| 173 | sound = new Sound(new URLRequest(config['audio'])); |
|---|
| 174 | channel = sound.play(); |
|---|
| 175 | setVolume(); |
|---|
| 176 | }; |
|---|
| 177 | |
|---|
| 178 | /** Set buttons in the controlbar **/ |
|---|
| 179 | private function setButtons():void { |
|---|
| 180 | if(config['captions']) { |
|---|
| 181 | view.getPlugin('controlbar').addButton(clip.captionsIcon,'captions',hideClick); |
|---|
| 182 | } else { |
|---|
| 183 | clip.captionsIcon.visible = false; |
|---|
| 184 | } |
|---|
| 185 | if(config['audio']) { |
|---|
| 186 | view.getPlugin('controlbar').addButton(clip.audioIcon,'audio',muteClick); |
|---|
| 187 | } else { |
|---|
| 188 | clip.audioIcon.visible = false; |
|---|
| 189 | } |
|---|
| 190 | }; |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | /** Set a caption on screen. **/ |
|---|
| 194 | private function setCaption(idx:Number):void { |
|---|
| 195 | var txt:String = ''; |
|---|
| 196 | if(idx > -1) { txt = captions[idx]['text']; } |
|---|
| 197 | current = idx; |
|---|
| 198 | clip.tf.htmlText = txt; |
|---|
| 199 | clip.tf.setTextFormat(format); |
|---|
| 200 | view.sendEvent('TRACE','caption: '+txt); |
|---|
| 201 | resizeHandler(); |
|---|
| 202 | if(config['listener']) { |
|---|
| 203 | try { |
|---|
| 204 | ExternalInterface.call(config['listener'],txt); |
|---|
| 205 | } catch(err:Error) {} |
|---|
| 206 | } |
|---|
| 207 | }; |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | /** Set the volume level. **/ |
|---|
| 211 | private function setVolume():void { |
|---|
| 212 | var trf:SoundTransform = new SoundTransform(config['volume']/100); |
|---|
| 213 | if(config['mute']) { trf.volume = 0; } |
|---|
| 214 | if(channel) { channel.soundTransform = trf; } |
|---|
| 215 | }; |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | /** The statehandler manages audio pauses. **/ |
|---|
| 219 | private function stateHandler(evt:ModelEvent) { |
|---|
| 220 | switch(evt.data.newstate) { |
|---|
| 221 | case ModelStates.PAUSED: |
|---|
| 222 | case ModelStates.COMPLETED: |
|---|
| 223 | case ModelStates.IDLE: |
|---|
| 224 | if(channel) { |
|---|
| 225 | channel.stop(); |
|---|
| 226 | } |
|---|
| 227 | break; |
|---|
| 228 | } |
|---|
| 229 | }; |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | /** Check timing of the player to sync captions. **/ |
|---|
| 233 | private function timeHandler(evt:ModelEvent):void { |
|---|
| 234 | var cur:Number = -1; |
|---|
| 235 | var pos:Number = evt.data.position; |
|---|
| 236 | // sync up the captions if needed. |
|---|
| 237 | for(var i:Number=0; i<captions.length; i++) { |
|---|
| 238 | if(captions[i]['begin'] < pos && captions[i]['end'] > pos) { |
|---|
| 239 | cur = i; |
|---|
| 240 | break; |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | if(cur > -1 && cur != current) { |
|---|
| 244 | setCaption(cur); |
|---|
| 245 | } |
|---|
| 246 | // sync up the audio if needed. |
|---|
| 247 | if(channel && view.config['state'] == ModelStates.PLAYING && Math.abs(pos-channel.position/1000) > 0.5) { |
|---|
| 248 | channel.stop(); |
|---|
| 249 | channel = sound.play(pos*1000); |
|---|
| 250 | setVolume(); |
|---|
| 251 | } |
|---|
| 252 | }; |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | }; |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | } |
|---|