-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
541 additions
and
0 deletions.
There are no files selected for viewing
Binary file added
BIN
+4.49 KB
9781484223758/Ch10/Ch10_SourceCode_TR/Complete/AS3Captions/AS3Captions_complete.fla
Binary file not shown.
120 changes: 120 additions & 0 deletions
120
...23758/Ch10/Ch10_SourceCode_TR/Complete/AS3Captions/edu/du/captions/data/CaptionsParser.as
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* Created by Joseph Labrecque on 12/9/2014. | ||
*/ | ||
package edu.du.captions.data { | ||
import flash.events.Event; | ||
import flash.events.EventDispatcher; | ||
import flash.events.IOErrorEvent; | ||
import flash.events.SecurityErrorEvent; | ||
import flash.net.URLLoader; | ||
import flash.net.URLRequest; | ||
import edu.du.captions.events.CaptionLoadEvent; | ||
import edu.du.captions.events.CaptionParseEvent; | ||
|
||
public class CaptionsParser extends EventDispatcher { | ||
private var rawArray:Array; | ||
private var captionsLoader:URLLoader; | ||
private var _captionsArray:Array; | ||
|
||
//do not mess with this directly - it is instantiated by CaptionsHandler | ||
public function CaptionsParser() { | ||
captionsLoader = new URLLoader(); | ||
_captionsArray = new Array(); | ||
rawArray = new Array(); | ||
} | ||
|
||
//adds some listeners to the URLLoader and then loads the vtt file | ||
public function loadCaptions(f:String):void { | ||
captionsLoader = new URLLoader(); | ||
_captionsArray = new Array(); | ||
rawArray = new Array(); | ||
|
||
captionsLoader.addEventListener(Event.COMPLETE, captionsLoaded); | ||
captionsLoader.addEventListener(IOErrorEvent.IO_ERROR, captionsError); | ||
captionsLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, captionsError); | ||
captionsLoader.load(new URLRequest(f)); | ||
} | ||
|
||
//this is the core of the parser - it attempts to parse the loaded vtt file into cute little objects. | ||
private function parseCaptions(r:Array):void { | ||
var l:int = r.length; | ||
try { | ||
var chapterSignifier:String = "\r\n"; | ||
if (rawArray[1].indexOf("\r\n") == -1) { | ||
chapterSignifier = "\n"; | ||
} | ||
//here we are looking to see whether we need to strip out caption indexes. | ||
//we check the position at index 1 since index 0 is just "WEBVTT" - always skip that junk! | ||
var rawTestIndex:int = rawArray[1].indexOf(chapterSignifier); | ||
if(rawTestIndex == 1) { | ||
//okay... we have indexes. let's kill them all. | ||
try { | ||
for (var j:int = 1; j < l; j++) { | ||
rawTestIndex = rawArray[j].indexOf(chapterSignifier); | ||
rawArray[j] = rawArray[j].substring(rawTestIndex + chapterSignifier.length); | ||
} | ||
} catch (e:Error){ | ||
dispatchEvent(new CaptionParseEvent(CaptionParseEvent.ERROR, true)); | ||
} | ||
} | ||
//now we are sure that the file is clean (no filthy indexes) and no errors yet. | ||
//so lets build neat little caption babies out of the base string data. | ||
for (var i:int = 1; i < l; i++) { | ||
var caption:Object = new Object(); | ||
//we get the start and end times from the standard vtt time format of "00:00.000". | ||
var startTime:String = r[i].substr(0, 12); | ||
var stopTime:String = r[i].substr(17, 12); | ||
//here we identify the first line break - this is where the caption text lives. | ||
var textIndex:int = r[i].indexOf(chapterSignifier); | ||
var captionText:String = ""; | ||
if (textIndex != -1){ | ||
captionText = r[i].substr(textIndex + chapterSignifier.length); | ||
} | ||
caption.startTime = startTime; | ||
caption.stopTime = stopTime; | ||
//replace the breaks with HTML junk for the TextField. | ||
caption.captionText = captionText.replace(chapterSignifier, "<br>"); | ||
//add the baby to our array. this one is cooked. | ||
_captionsArray.push(caption); | ||
} | ||
captionsLoader.removeEventListener(IOErrorEvent.IO_ERROR, captionsError); | ||
captionsLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, captionsError); | ||
//let the application know that we have parsed the file just fine! | ||
dispatchEvent(new CaptionParseEvent(CaptionParseEvent.PARSED, true)); | ||
} catch (e:Error){ | ||
captionsLoader.removeEventListener(IOErrorEvent.IO_ERROR, captionsError); | ||
captionsLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, captionsError); | ||
//let the application know something truly horrifying has occurred... | ||
dispatchEvent(new CaptionParseEvent(CaptionParseEvent.ERROR, true)); | ||
} | ||
} | ||
|
||
//captions loaded successfully - now this will all get parsed into little neat objects and placed in an array. | ||
public function captionsLoaded(e:Event):void { | ||
dispatchEvent(new CaptionLoadEvent(CaptionLoadEvent.LOADED, true)); | ||
var captionSignifier:String = "\r\n\r\n"; | ||
if (e.target.data.indexOf("\r\n\r\n") == -1) { | ||
captionSignifier = "\n\n"; | ||
} | ||
rawArray = e.target.data.split(captionSignifier); | ||
parseCaptions(rawArray); | ||
//let the application know that we have loaded the file just fine! | ||
captionsLoader.removeEventListener(Event.COMPLETE, captionsLoaded); | ||
} | ||
|
||
//tsk tsk tsk... something is wrong... does the file even exist? | ||
private function captionsError(e:*):void { | ||
//let the application know something truly horrifying has occurred... | ||
dispatchEvent(new CaptionLoadEvent(CaptionLoadEvent.ERROR, true)); | ||
captionsLoader.removeEventListener(IOErrorEvent.IO_ERROR, captionsError); | ||
captionsLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, captionsError); | ||
_captionsArray = new Array(); | ||
} | ||
|
||
//CaptionsHandler grabs the captions through this. | ||
public function get captionsArray():Array { | ||
return _captionsArray; | ||
} | ||
|
||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...8/Ch10/Ch10_SourceCode_TR/Complete/AS3Captions/edu/du/captions/events/CaptionLoadEvent.as
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Created by Joseph Labrecque on 12/10/2014. | ||
*/ | ||
package edu.du.captions.events { | ||
import flash.events.Event; | ||
|
||
public class CaptionLoadEvent extends Event { | ||
public static const LOADED:String = "onLoaded"; | ||
public static const ERROR:String = "onError"; | ||
public function CaptionLoadEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false) { | ||
super(type, bubbles, cancelable); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../Ch10/Ch10_SourceCode_TR/Complete/AS3Captions/edu/du/captions/events/CaptionParseEvent.as
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Created by Joseph Labrecque on 12/10/2014. | ||
*/ | ||
package edu.du.captions.events { | ||
import flash.events.Event; | ||
|
||
public class CaptionParseEvent extends Event { | ||
public static const PARSED:String = "onParsed"; | ||
public static const ERROR:String = "onError"; | ||
public function CaptionParseEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false) { | ||
super(type, bubbles, cancelable); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...758/Ch10/Ch10_SourceCode_TR/Complete/AS3Captions/edu/du/captions/utils/CaptionsHandler.as
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Created by Joseph Labrecque on 12/1/2014. | ||
* release version 1.1.1 | ||
* fixes for indexed captions | ||
* more robust comments ;) | ||
*/ | ||
package edu.du.captions.utils { | ||
import flash.text.TextField; | ||
import edu.du.captions.data.CaptionsParser; | ||
|
||
public class CaptionsHandler { | ||
public var captionsParser:CaptionsParser; | ||
private var timeFormatter:TimeFormatter; | ||
private var currentCaptionText:String; | ||
|
||
//instantiate this first and invoke everything through that. | ||
public function CaptionsHandler() { | ||
captionsParser = new CaptionsParser(); | ||
timeFormatter = new TimeFormatter(); | ||
} | ||
|
||
//pass in the URL to a .vtt file here - it'll employ the parser to load it up. | ||
public function gatherCaptions(s:String):void { | ||
var filePath:String = s; | ||
currentCaptionText = ""; | ||
captionsParser.loadCaptions(filePath); | ||
} | ||
|
||
//this can bve invoked whenever appropriate - pass in a time in seconds and bind it to a TextField instance. | ||
public function renderCaptions(t:Number, d:TextField):void { | ||
var captions:Array = captionsParser.captionsArray; | ||
for (var i:int = 0; i < captions.length; i++) { | ||
//for each caption object, get start and end times | ||
var s:Number = timeFormatter.text2Sec(captions[i].startTime); | ||
var e:Number = timeFormatter.text2Sec(captions[i].stopTime); | ||
//if the current time is previous to this caption start time - we don't care. | ||
if (t < s) { | ||
break; | ||
} else { | ||
//does the current time fall between the caption object start and end time? | ||
if (t > s && t < e) { | ||
//is it different from what is already displayed? great - print that stuff out. | ||
if (captions[i].captionText != currentCaptionText) { | ||
currentCaptionText = captions[i].captionText; | ||
d.htmlText = currentCaptionText; | ||
break; | ||
} | ||
//is the current time larger than the caption object end time? burn it all down. | ||
}else if(t > e){ | ||
currentCaptionText = ""; | ||
d.htmlText = currentCaptionText; | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...23758/Ch10/Ch10_SourceCode_TR/Complete/AS3Captions/edu/du/captions/utils/TimeFormatter.as
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Created by Joseph Labrecque on 12/2/2014. | ||
*/ | ||
package edu.du.captions.utils { | ||
public class TimeFormatter { | ||
public function TimeFormatter() {} | ||
|
||
//converts raw seconds into a "00:00:00" string. | ||
public function seconds2HMS(raw_seconds:int):String { | ||
var hours:int = Math.floor(raw_seconds/3600); | ||
var minutes:int = Math.floor((raw_seconds-(hours*3600))/60); | ||
var seconds:int = Math.floor(raw_seconds%60); | ||
var hS:String; | ||
var mS:String; | ||
var sS:String; | ||
hS = ""+hours; | ||
if (minutes<10) { | ||
mS = "0"+minutes; | ||
} else { | ||
mS = ""+minutes; | ||
} | ||
if (seconds<10) { | ||
sS = "0"+seconds; | ||
} else { | ||
sS = ""+seconds; | ||
} | ||
var time_string:String = hS+":"+mS+":"+sS; | ||
if (time_string.length != 7){ | ||
time_string = "00:00:00"; | ||
} | ||
return time_string; | ||
} | ||
|
||
//converts a string formatted like "00:00:00" to raw seconds. | ||
public function text2Sec(t:String):Number { | ||
var time:String = t; | ||
var seconds:Number = 0; | ||
seconds += Number(time.substr(0,2))*3600; | ||
seconds += Number(time.substr(3,2))*60; | ||
seconds += Number(time.substr(6,2)); | ||
seconds = Number(seconds + "." + time.substr(9,3)); | ||
return seconds; | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
9781484223758/Ch10/Ch10_SourceCode_TR/Complete/AS3Captions/video/Absinthe.vtt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
WEBVTT | ||
00:00:01.000 --> 00:00:04.000 A:middle | ||
Let's prepare some absinthe. I have some Jade PF 1901 here. | ||
|
||
00:00:06.000 --> 00:00:10.000 A:middle | ||
We'll pour 1 ounce of the absinthe into our absinthe glass. | ||
|
||
00:00:15.000 --> 00:00:17.000 A:middle | ||
There we are. Very nice color! | ||
|
||
00:00:19.000 --> 00:00:23.000 A:middle | ||
Next, we grab our iced water... | ||
|
||
00:00:26.000 --> 00:00:32.000 A:middle | ||
and place our dripper upon the glass. (you could also use a carafe or fountain) | ||
|
||
00:00:36.000 --> 00:00:39.000 A:middle | ||
Now, a measure of water is poured into the dripper. (4-5 ounces) | ||
|
||
00:00:40.000 --> 00:00:44.000 A:middle | ||
We can see the trails begin to form in the absinthe as the water is introduced. | ||
|
||
00:00:45.000 --> 00:00:50.000 A:middle | ||
The absinthe louche forms. Cloudy, milky... we can watch it rolling within the glass. | ||
|
||
00:01:05.000 --> 00:01:10.000 A:middle | ||
When the line of unlouched absinthe is gone it is a sign that the absinthe is ready to enjoy. | ||
|
||
00:02:05.000 --> 00:02:10.000 A:middle | ||
Delicious - enjoy! | ||
|
||
|
||
|
||
|
Binary file added
BIN
+10.4 KB
9781484223758/Ch10/Ch10_SourceCode_TR/Complete/CanvasCaptions/CanvasCaptions.fla
Binary file not shown.
Oops, something went wrong.