| 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.filters.DropShadowFilter; |
|---|
| 16 | import flash.net.URLLoader; |
|---|
| 17 | import flash.net.URLRequest; |
|---|
| 18 | import flash.text.*; |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | public class Captions extends MovieClip implements PluginInterface { |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | /** List with configuration settings. **/ |
|---|
| 25 | public var config:Object = { |
|---|
| 26 | back:false, |
|---|
| 27 | file:undefined, |
|---|
| 28 | size:14, |
|---|
| 29 | hide:false |
|---|
| 30 | }; |
|---|
| 31 | /** Displayelement to load the captions into. **/ |
|---|
| 32 | public var clip:MovieClip; |
|---|
| 33 | /** XML connect and parse object. **/ |
|---|
| 34 | private var loader:URLLoader; |
|---|
| 35 | /** Reference to the MVC view. **/ |
|---|
| 36 | private var view:AbstractView; |
|---|
| 37 | /** Icon for the controlbar. **/ |
|---|
| 38 | private var icon:MovieClip; |
|---|
| 39 | /** Reference to the textfield. **/ |
|---|
| 40 | public var field:TextField; |
|---|
| 41 | /** The array the captions are loaded into. **/ |
|---|
| 42 | private var captions:Array; |
|---|
| 43 | /** Textformat entry for the captions. **/ |
|---|
| 44 | private var format:TextFormat; |
|---|
| 45 | /** Currently active caption. **/ |
|---|
| 46 | private var current:Number; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | public function Captions():void { |
|---|
| 50 | loader = new URLLoader(); |
|---|
| 51 | loader.addEventListener(Event.COMPLETE,loaderHandler); |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | /** Clicking the hide button. **/ |
|---|
| 56 | private function clickHandler(evt:MouseEvent):void { |
|---|
| 57 | hide(!config['hide']); |
|---|
| 58 | }; |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | /** Set buttons in the controlbar **/ |
|---|
| 62 | private function drawButton():void { |
|---|
| 63 | try { |
|---|
| 64 | icon = new MovieClip(); |
|---|
| 65 | icon.graphics.beginFill(0x000000); |
|---|
| 66 | icon.graphics.moveTo(1,0); |
|---|
| 67 | icon.graphics.lineTo(1,7); |
|---|
| 68 | icon.graphics.lineTo(6,7); |
|---|
| 69 | icon.graphics.lineTo(6,5); |
|---|
| 70 | icon.graphics.lineTo(4,5); |
|---|
| 71 | icon.graphics.lineTo(4,6); |
|---|
| 72 | icon.graphics.lineTo(3,6); |
|---|
| 73 | icon.graphics.lineTo(3,1); |
|---|
| 74 | icon.graphics.lineTo(4,1); |
|---|
| 75 | icon.graphics.lineTo(4,2); |
|---|
| 76 | icon.graphics.lineTo(6,2); |
|---|
| 77 | icon.graphics.lineTo(6,0); |
|---|
| 78 | icon.graphics.lineTo(1,0); |
|---|
| 79 | icon.graphics.moveTo(7,0); |
|---|
| 80 | icon.graphics.lineTo(7,7); |
|---|
| 81 | icon.graphics.lineTo(12,7); |
|---|
| 82 | icon.graphics.lineTo(12,5); |
|---|
| 83 | icon.graphics.lineTo(10,5); |
|---|
| 84 | icon.graphics.lineTo(10,6); |
|---|
| 85 | icon.graphics.lineTo(9,6); |
|---|
| 86 | icon.graphics.lineTo(9,1); |
|---|
| 87 | icon.graphics.lineTo(10,1); |
|---|
| 88 | icon.graphics.lineTo(10,2); |
|---|
| 89 | icon.graphics.lineTo(12,2); |
|---|
| 90 | icon.graphics.lineTo(12,0); |
|---|
| 91 | icon.graphics.lineTo(7,0); |
|---|
| 92 | icon.graphics.endFill(); |
|---|
| 93 | view.getPlugin('controlbar').addButton(icon,'captions',clickHandler); |
|---|
| 94 | } catch (err:Error) {} |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | private function drawClip() { |
|---|
| 99 | var rct:MovieClip = new MovieClip(); |
|---|
| 100 | rct.graphics.beginFill(0x000000,0.5); |
|---|
| 101 | rct.graphics.drawRect(0,0,400,60); |
|---|
| 102 | format = new TextFormat(); |
|---|
| 103 | format.color = 0xFFFFFF; |
|---|
| 104 | format.size = config['size']; |
|---|
| 105 | format.align = "center"; |
|---|
| 106 | format.font = "_sans"; |
|---|
| 107 | format.leading = 4; |
|---|
| 108 | field = new TextField(); |
|---|
| 109 | field.width = 400; |
|---|
| 110 | field.height = 10; |
|---|
| 111 | field.y = 10; |
|---|
| 112 | field.autoSize = "center"; |
|---|
| 113 | field.selectable = false; |
|---|
| 114 | field.multiline = true; |
|---|
| 115 | field.defaultTextFormat = format; |
|---|
| 116 | clip.addChild(rct); |
|---|
| 117 | clip.addChild(field); |
|---|
| 118 | if(!config['back']) { |
|---|
| 119 | rct.alpha = 0; |
|---|
| 120 | var ftr:DropShadowFilter = new DropShadowFilter(0,45,0,1,2,2,10,3); |
|---|
| 121 | field.filters = new Array(ftr); |
|---|
| 122 | } |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | /** Show/hide the captions **/ |
|---|
| 127 | public function hide(stt:Boolean):void { |
|---|
| 128 | config['hide'] = stt; |
|---|
| 129 | view.saveCookie('captions.hide',config['hide']); |
|---|
| 130 | clip.visible = config['hide']; |
|---|
| 131 | if(config['hide']) { |
|---|
| 132 | icon.alpha = 1; |
|---|
| 133 | } else { |
|---|
| 134 | icon.alpha = 0.3; |
|---|
| 135 | } |
|---|
| 136 | }; |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | /** Initing the plugin. **/ |
|---|
| 140 | public function initializePlugin(vie:AbstractView):void { |
|---|
| 141 | view = vie; |
|---|
| 142 | view.addControllerListener(ControllerEvent.ITEM,itemHandler); |
|---|
| 143 | view.addControllerListener(ControllerEvent.RESIZE,resizeHandler); |
|---|
| 144 | view.addModelListener(ModelEvent.TIME,timeHandler); |
|---|
| 145 | drawButton(); |
|---|
| 146 | drawClip(); |
|---|
| 147 | hide(config['hide']); |
|---|
| 148 | }; |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | /** Check for captions with a new item. **/ |
|---|
| 152 | private function itemHandler(evt:ControllerEvent=null):void { |
|---|
| 153 | current = 0; |
|---|
| 154 | captions = new Array(); |
|---|
| 155 | var fil:String = view.playlist[view.config['item']]['captions.file']; |
|---|
| 156 | if(fil) { |
|---|
| 157 | config['file'] = fil; |
|---|
| 158 | } else if (view.config['captions']) { |
|---|
| 159 | config['file'] = view.config['captions']; |
|---|
| 160 | } |
|---|
| 161 | try { |
|---|
| 162 | loader.load(new URLRequest(config['file'])); |
|---|
| 163 | } catch (err:Error) { |
|---|
| 164 | view.sendEvent('TRACE','Captions: '+err.message); |
|---|
| 165 | } |
|---|
| 166 | }; |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | /** Captions are loaded; now display them. **/ |
|---|
| 170 | private function loaderHandler(evt:Event):void { |
|---|
| 171 | if(config['file'].substr(-3) == 'srt') { |
|---|
| 172 | captions = SRTParser.parseCaptions(String(evt.target.data)); |
|---|
| 173 | } else { |
|---|
| 174 | captions = TTParser.parseCaptions(XML(evt.target.data)); |
|---|
| 175 | } |
|---|
| 176 | if(captions.length == 0) { |
|---|
| 177 | view.sendEvent('TRACE','Captions: not a valid TimedText or SRT file.'); |
|---|
| 178 | } |
|---|
| 179 | }; |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | /** Resize the captions if the display changes. **/ |
|---|
| 183 | private function resizeHandler(evt:ControllerEvent=undefined):void { |
|---|
| 184 | clip.width = view.config['width']; |
|---|
| 185 | clip.scaleY = clip.scaleX; |
|---|
| 186 | if(!config['back']) { |
|---|
| 187 | field.y = 50 - field.height; |
|---|
| 188 | } |
|---|
| 189 | clip.y = view.config['height']-clip.height; |
|---|
| 190 | }; |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | /** Set a caption on screen. **/ |
|---|
| 194 | private function setCaption(pos:Number):void { |
|---|
| 195 | for(var i:Number=0; i<captions.length-1; i++) { |
|---|
| 196 | if(captions[i]['begin'] < pos && captions[i+1]['begin'] > pos) { |
|---|
| 197 | current = i; |
|---|
| 198 | field.htmlText = captions[i]['text']; |
|---|
| 199 | resizeHandler(); |
|---|
| 200 | return; |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | }; |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | /** Check timing of the player to sync captions. **/ |
|---|
| 208 | private function timeHandler(evt:ModelEvent):void { |
|---|
| 209 | var pos:Number = evt.data.position; |
|---|
| 210 | if(captions.length > 0 && (captions[current]['begin'] > pos || captions[current+1]['begin'] < pos)) { |
|---|
| 211 | setCaption(pos); |
|---|
| 212 | } |
|---|
| 213 | }; |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | }; |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | } |
|---|