source: plugins/captions/com/jeroenwijering/plugins/Captions.as @ 173

Revision 173, 6.1 KB checked in by jeroen, 4 years ago (diff)

tagged update of all plugins

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