Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* OpenFL Video help file for flash/html5
* openfl=3.5.3
* lime=2.8.3
*/
//vars
var duration:Int;
var ns:NetStream;
var vid:Video;
//create video
vid = new Video(800,600);
var c:NetConnection = new NetConnection();
c.connect(null);
ns = new NetStream(c);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns.addEventListener(NetStatusEvent.NET_STATUS, onStatus);//seems to only works in flash
vid.attachNetStream(ns);
var meta:Dynamic = { };
//flash uses onMetaData and NetStatusEvent.NET_STATUS, html5 uses onPlayStatus.
#if flash
meta.onMetaData = function(meta:Object):Void
{
duration = meta.duration;
}
#elseif html5
meta.onPlayStatus = function(meta:Object):Void
{
handleVideoStatus(meta.code, meta);//NetStatusEvent.NET_STATUS doesn't work on html5, so we have to call this here
}
#end
ns.client = meta;
function asyncErrorHandler(e:AsyncErrorEvent):Void
{
trace('error '+e);
}
function onStatus(e:NetStatusEvent):Void
{
handleVideoStatus(e.info.code);
}
function handleVideoStatus(status:String, ?meta:Object):Void
{
switch(status)
{
case "NetStream.Play.canplay"://html5
duration = meta.duration;
case "NetStream.Play.timeupdate"://html5
handleProgression(meta.position);
case "NetStream.Play.Complete", "NetStream.Play.Stop"://html5 then flash
trace('video stopped')
#if flash
if(hasEventListener(Event.ENTER_FRAME))
removeEventListener(Event.ENTER_FRAME, onUpdate);
#end
}
}
function play():Void
{
ns.play(videoName);
#if flash
if(!hasEventListener(Event.ENTER_FRAME))
addEventListener(Event.ENTER_FRAME, onUpdate);
#end
}
#if flash
function onUpdate(e:Event):Void
{
handleProgression(Std.int(ns.time));
}
#end
function handleProgression(time:Int):Void
{
var progress:Float = time / duration;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment