source: trunk/as3/com/jeroenwijering/plugins/Captions.as @ 165

Revision 165, 5.6 KB checked in by jeroen, 4 years ago (diff)

Fixed the captions to also load .txt Subrip files

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;
10import com.jeroenwijering.utils.Configger;
11
12import flash.display.MovieClip;
13import flash.events.Event;
14import flash.events.MouseEvent;
15import flash.filters.DropShadowFilter;
16import flash.net.URLLoader;
17import flash.net.URLRequest;
18import flash.text.*;
19
20
21public 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                var fil:String = view.playlist[view.config['item']]['captions.file'];
155                if(fil) {
156                        config['file'] = fil;
157                } else if (view.config['captions']) {
158                        config['file'] = view.config['captions'];
159                }
160                try {
161                        loader.load(new URLRequest(config['file']));
162                } catch (err:Error) {
163                        view.sendEvent('TRACE','Captions: '+err.message);
164                }
165        };
166
167
168        /** Captions are loaded; now display them. **/
169        private function loaderHandler(evt:Event):void {
170                var ext:String = config['file'].substr(-3);
171                captions = new Array();
172                if(ext == 'srt' || ext == 'txt') {
173                        captions = SRTParser.parseCaptions(String(evt.target.data));
174                } else {
175                        captions = TTParser.parseCaptions(XML(evt.target.data));
176                }
177                if(captions.length == 0) {
178                        view.sendEvent('TRACE','Captions: not a valid TimedText or SRT file.');
179                }
180        };
181
182
183        /** Resize the captions if the display changes. **/
184        private function resizeHandler(evt:ControllerEvent=undefined):void {
185                clip.width = view.config['width'];
186                clip.scaleY = clip.scaleX;
187                if(!config['back']) {
188                        field.y = 50 - field.height;
189                }
190                clip.y = view.config['height']-clip.height;
191        };
192
193
194        /** Set a caption on screen. **/
195        private function setCaption(pos:Number):void {
196                for(var i:Number=0; i<captions.length-1; i++) {
197                        if(captions[i]['begin'] < pos && captions[i+1]['begin'] > pos) {
198                                current = i;
199                                field.htmlText = captions[i]['text'];
200                                resizeHandler();
201                                return;
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 && (captions[current]['begin'] > pos || captions[current+1]['begin'] < pos)) {
211                        setCaption(pos);
212                }
213        };
214
215
216};
217
218
219}
Note: See TracBrowser for help on using the repository browser.