YouTube support

(the stage stops emitting mouse events for some reason when we use youtube videos.)
This commit is contained in:
Jeremy Penner 2011-05-04 17:24:38 -07:00
parent eb1974a955
commit 77903fbd64
7 changed files with 121 additions and 41 deletions

View file

@ -1,5 +1,6 @@
package package
{ {
import flash.display.Shape;
import flash.display.Sprite; import flash.display.Sprite;
import flash.events.Event; import flash.events.Event;
import flash.events.MouseEvent; import flash.events.MouseEvent;
@ -24,13 +25,14 @@ package
{ {
removeEventListener(Event.ADDED_TO_STAGE, init); removeEventListener(Event.ADDED_TO_STAGE, init);
addEventListener(Event.REMOVED_FROM_STAGE, cleanup); addEventListener(Event.REMOVED_FROM_STAGE, cleanup);
stage.addEventListener(MouseEvent.CLICK, onClick);
addEventListener(MouseEvent.CLICK, onClick);
videotube.addEventListener(EventQte.QTE, onQte); videotube.addEventListener(EventQte.QTE, onQte);
} }
private function cleanup(e:Event):void private function cleanup(e:Event):void
{ {
removeEventListener(Event.REMOVED_FROM_STAGE, cleanup); removeEventListener(Event.REMOVED_FROM_STAGE, cleanup);
stage.removeEventListener(MouseEvent.CLICK, onClick); removeEventListener(MouseEvent.CLICK, onClick);
videotube.removeEventListener(EventQte.QTE, onQte); videotube.removeEventListener(EventQte.QTE, onQte);
} }
private function onQte(e:EventQte):void private function onQte(e:EventQte):void

View file

@ -7,11 +7,16 @@ package
*/ */
public class Gamedisc public class Gamedisc
{ {
public static const VIDEOTUBE_FLV:String = "flv";
public static const VIDEOTUBE_YOUTUBE:String = "yt";
public var urlVideo:String; public var urlVideo:String;
public var typeVideotube:String;
public var rgqte:Array; public var rgqte:Array;
public function Gamedisc(urlVideo:String) public function Gamedisc(urlVideo:String, typeVideotube:String)
{ {
this.urlVideo = urlVideo; this.urlVideo = urlVideo;
this.typeVideotube = typeVideotube;
this.rgqte = []; this.rgqte = [];
} }
public function AddQte(qte:Qte):void public function AddQte(qte:Qte):void
@ -20,14 +25,18 @@ package
} }
public function CreateVideotube():Videotube public function CreateVideotube():Videotube
{ {
return new VideotubeFlv(this); if (typeVideotube == VIDEOTUBE_FLV)
return new VideotubeFlv(this);
if (typeVideotube == VIDEOTUBE_YOUTUBE)
return new VideotubeYt(this);
throw "invalid videotube type";
} }
public function ToJson():Object public function ToJson():Object
{ {
var jsonRgqte:Array = []; var jsonRgqte:Array = [];
for each (var qte:Qte in rgqte) for each (var qte:Qte in rgqte)
jsonRgqte.push(qte.ToJson()); jsonRgqte.push(qte.ToJson());
return { urlVideo: urlVideo, rgqte: jsonRgqte }; return { urlVideo: urlVideo, typeVideotube: typeVideotube, rgqte: jsonRgqte };
} }
public function FromJson(json:Object):void public function FromJson(json:Object):void
{ {
@ -39,6 +48,7 @@ package
rgqte.push(qte); rgqte.push(qte);
} }
urlVideo = json.urlVideo; urlVideo = json.urlVideo;
typeVideotube = json.typeVideotube;
} }
} }

View file

@ -20,7 +20,8 @@ package
public function Main():void public function Main():void
{ {
gamedisc = new Gamedisc("The Last Eichhof - Longplay.flv"); //gamedisc = new Gamedisc("The Last Eichhof - Longplay.flv", Gamedisc.VIDEOTUBE_FLV);
gamedisc = new Gamedisc("EdzLFNELeCI", Gamedisc.VIDEOTUBE_YOUTUBE);
videotube = gamedisc.CreateVideotube(); videotube = gamedisc.CreateVideotube();
if (stage) init(); if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init); else addEventListener(Event.ADDED_TO_STAGE, init);
@ -31,9 +32,11 @@ package
removeEventListener(Event.ADDED_TO_STAGE, init); removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point // entry point
addChild(videotube); addChild(videotube);
toggleGame();
stage.addEventListener(KeyboardEvent.KEY_UP, onKey); stage.addEventListener(KeyboardEvent.KEY_UP, onKey);
videotube.play(); if (videotube.fready())
onVideotubeReady();
else
videotube.addEventListener(Videotube.READY, onVideotubeReady);
} }
private function toggleGame():void private function toggleGame():void
{ {
@ -58,6 +61,11 @@ package
} }
videotube.seek(0); videotube.seek(0);
} }
private function onVideotubeReady(event:Event = null):void
{
toggleGame();
videotube.play();
}
private function onKey(key:KeyboardEvent):void private function onKey(key:KeyboardEvent):void
{ {
if (key.keyCode == Keyboard.SPACE) if (key.keyCode == Keyboard.SPACE)

View file

@ -34,14 +34,20 @@ package
{ {
removeEventListener(Event.ADDED_TO_STAGE, init); removeEventListener(Event.ADDED_TO_STAGE, init);
addEventListener(Event.REMOVED_FROM_STAGE, cleanup); addEventListener(Event.REMOVED_FROM_STAGE, cleanup);
stage.addEventListener(MouseEvent.CLICK, onClick);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove); var shapeClick:Shape = new Shape();
shapeClick.graphics.beginFill(0x0000ff, 0);
shapeClick.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
addChildAt(shapeClick, 0);
addEventListener(MouseEvent.CLICK, onClick);
addEventListener(MouseEvent.MOUSE_MOVE, onMove);
} }
private function cleanup(e: Event):void private function cleanup(e: Event):void
{ {
removeEventListener(Event.REMOVED_FROM_STAGE, cleanup); removeEventListener(Event.REMOVED_FROM_STAGE, cleanup);
stage.removeEventListener(MouseEvent.CLICK, onClick); removeEventListener(MouseEvent.CLICK, onClick);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove); removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
} }
public function clear():void public function clear():void
{ {

View file

@ -1,6 +1,7 @@
package package
{ {
import flash.display.Sprite; import flash.display.Sprite;
import flash.events.Event;
import flash.events.EventDispatcher; import flash.events.EventDispatcher;
/** /**
@ -9,11 +10,44 @@ package
*/ */
public class Videotube extends Sprite public class Videotube extends Sprite
{ {
public static const READY:String = "videotube-ready";
public function fready():Boolean { throw "not implemented"; }
public function play():void { throw "not implemented"; } public function play():void { throw "not implemented"; }
public function pause():void { throw "not implemented"; } public function pause():void { throw "not implemented"; }
public function resume():void { throw "not implemented"; } public function resume():void { throw "not implemented"; }
public function time():Number { throw "not implemented"; } public function time():Number { throw "not implemented"; }
public function seek(sec:Number):void { throw "not implemented"; } public function seek(sec:Number):void { throw "not implemented"; }
}
private var secPrev:Number;
private var iqte:int;
protected var gamedisc:Gamedisc;
public function Videotube(gamedisc: Gamedisc)
{
this.gamedisc = gamedisc;
secPrev = 0;
iqte = 0;
}
protected function tick(e: Event):void
{
var secNow:Number = time();
if (secNow > secPrev && secPrev >= 0)
{
while(iqte < gamedisc.rgqte.length)
{
var qte:Qte = gamedisc.rgqte[iqte];
if (qte.secTrigger > secNow)
break;
if (qte.secTrigger > secPrev)
dispatchEvent(new EventQte(qte));
iqte ++;
}
}
else
{
iqte = 0;
}
secPrev = secNow;
}
}
} }

View file

@ -12,16 +12,10 @@ package
public class VideotubeFlv extends Videotube public class VideotubeFlv extends Videotube
{ {
private var flv:Video; private var flv:Video;
private var gamedisc:Gamedisc;
private var stream:NetStream; private var stream:NetStream;
private var iqte:int;
private var secPrev:Number;
public function VideotubeFlv(gamedisc:Gamedisc) public function VideotubeFlv(gamedisc:Gamedisc)
{ {
super(); super(gamedisc);
this.gamedisc = gamedisc;
iqte = 0;
secPrev = 0;
addEventListener(Event.ADDED_TO_STAGE, init); addEventListener(Event.ADDED_TO_STAGE, init);
} }
private function init(e: Event):void private function init(e: Event):void
@ -44,27 +38,7 @@ package
removeEventListener(Event.REMOVED_FROM_STAGE, cleanup); removeEventListener(Event.REMOVED_FROM_STAGE, cleanup);
stage.removeEventListener(Event.ENTER_FRAME, tick); stage.removeEventListener(Event.ENTER_FRAME, tick);
} }
private function tick(e: Event):void public override function fready():Boolean { return true; }
{
var secNow:Number = stream.time;
if (secNow > secPrev && secPrev >= 0)
{
while(iqte < gamedisc.rgqte.length)
{
var qte:Qte = gamedisc.rgqte[iqte];
if (qte.secTrigger > secNow)
break;
if (qte.secTrigger > secPrev)
dispatchEvent(new EventQte(qte));
iqte ++;
}
}
else
{
iqte = 0;
}
secPrev = secNow;
}
public override function play():void { stream.play(gamedisc.urlVideo); } public override function play():void { stream.play(gamedisc.urlVideo); }
public override function pause():void { stream.pause(); } public override function pause():void { stream.pause(); }
public override function resume():void { stream.resume(); } public override function resume():void { stream.resume(); }

46
src/VideotubeYt.as Normal file
View file

@ -0,0 +1,46 @@
package
{
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.Security;
/**
* ...
* @author jjp
*/
public class VideotubeYt extends Videotube
{
private var player:Object;
private var loader:Loader;
public function VideotubeYt(gamedisc:Gamedisc)
{
super(gamedisc);
Security.allowDomain("www.youtube.com");
player = null;
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
}
private function onLoaderInit(event:Event):void
{
addChild(loader);
loader.contentLoaderInfo.removeEventListener(Event.INIT, onLoaderInit);
loader.content.addEventListener("onReady", onPlayerReady);
}
private function onPlayerReady(event:Event):void
{
player = loader.content;
player.setSize(stage.stageWidth, stage.stageHeight);
player.cueVideoById(gamedisc.urlVideo);
stage.addEventListener(Event.ENTER_FRAME, tick);
dispatchEvent(new Event(Videotube.READY));
}
public override function fready():Boolean { return player !== null; }
public override function play():void { player.playVideo(); }
public override function pause():void { player.pauseVideo(); }
public override function resume():void { player.playVideo(); }
public override function time():Number { return player.getCurrentTime(); }
public override function seek(sec:Number):void { player.seekTo(sec, true); }
}
}