Skip to content

Instantly share code, notes, and snippets.

@rodrigogermanlopez
Created July 26, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigogermanlopez/dd1d883250e541c417738c927a245572 to your computer and use it in GitHub Desktop.
Save rodrigogermanlopez/dd1d883250e541c417738c927a245572 to your computer and use it in GitHub Desktop.
Adobe AIR Video Issue (reference file)
/**
*
* Created by Rodrigo Lopez [blnk™] on 7/24/17.
*
*/
package app.ui.media {
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.utils.setTimeout;
import starling.core.Starling;
import starling.display.Image;
import starling.display.Sprite;
import starling.textures.Texture;
public class TestVideo extends Sprite {
private var videoImage:Image;
private var ns1:NetStream;
private var ns2:NetStream;
private var nc:NetConnection;
private var texture1:Texture;
private var texture2:Texture;
private var videoUrl:String;
private var myClient:Object;
private var videoW:int;
private var videoH:int;
private var duration:Number = -1;
private var usingId:int;
private var cns:NetStream;
private var ctx:Texture;
private var _texturesCompleted:int = 0;
public var disposeOnClose:Boolean = true;
// counter of errors,
// An error is when resuming playback, after a delay, the netstream::time == 0
private var _numPlaybackErrors:int;
// special flag to force a failure on the code.
private var _forceFailure:Boolean;
// for windows, when the video swap happens more than this value, it autoDisposes
public var maxPlaybackErrors:int = 0; // 0 = no playback errors detection.
public function TestVideo( url:String ) {
videoUrl = url;
initialize();
}
protected function initialize():void {
videoImage = new Image( null );
addChild( videoImage );
}
private function onMetadata( info:Object ):void {
if ( duration > 0 ) return;
duration = info.duration;
videoW = info.width;
videoH = info.height;
}
private function netStatusHandler( event:NetStatusEvent ):void {
switch ( event.info.code ) {
case "NetStream.Play.Stop":
swap();
break;
}
}
private function swap():void {
if ( maxPlaybackErrors > 0 && _numPlaybackErrors > maxPlaybackErrors ) {
_forceFailure = false;
customDispose();
recreate();
return;
}
if ( cns ) {
cns.removeEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
cns.client = {};
cns.seek( 0 );
cns.pause();
}
usingId = usingId == 1 ? 2 : 1;
// update player.
cns = this["ns" + usingId];
cns.client = myClient;
cns.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
cns.resume();
ctx = this["texture" + usingId];
trace( "Using player id:", usingId );
applyTexture();
// validate if the stream is playing ...
setTimeout( validateCurrentNS, 500 );
}
private function validateCurrentNS():void {
if ( cns.time == 0 || _forceFailure ) {
++_numPlaybackErrors;
trace( "Current player stuck. num times stuck=", _numPlaybackErrors );
// try to unlock the video?
// I think it doesnt work anyways.
cns.seek( 2 );
swap();
} else {
_numPlaybackErrors = 0
}
}
private function applyTexture():void {
if ( !ctx ) return;
videoImage.texture = ctx;
videoImage.readjustSize();
videoImage.visible = true;
}
private function onVideoTextureComplete():void {
++_texturesCompleted;
if ( _texturesCompleted == 2 ) {
onTexturesInvalidated();
}
}
private function onTexturesInvalidated():void {
trace( "Video textures complete", ctx );
applyTexture();
}
public function openClose( isOpen:Boolean ):void {
Starling.current.skipUnchangedFrames = !isOpen;
if ( isOpen ) {
if ( _texturesCompleted == 0 ) {
recreate();
} else {
swap();
}
} else {
// STOP ALL SHIT.
if ( disposeOnClose ) {
customDispose();
} else {
cns.pause();
}
}
}
private function reloadVideo():void {
customDispose();
recreate();
}
private function recreate():void {
myClient = {};
myClient.onMetaData = onMetadata;
nc = new NetConnection();
nc.connect( null );
ns1 = new NetStream( nc );
ns2 = new NetStream( nc );
ns1.client = myClient;
ns1.useHardwareDecoder = ns2.useHardwareDecoder = false;
_texturesCompleted = 0;
texture1 = Texture.fromNetStream( ns1, 1, onVideoTextureComplete );
texture2 = Texture.fromNetStream( ns2, 1, onVideoTextureComplete );
usingId = 2;
ns1.play( videoUrl );
ns2.play( videoUrl );
ns1.pause();
ns2.pause();
swap();
}
private function customDispose():void {
_numPlaybackErrors = 0;
trace( "DISPOSING VIDEOS." );
videoImage.visible = false;
ns1.removeEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
ns1.dispose();
ns2.removeEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
ns2.dispose();
ns1 = null;
ns2 = null;
if ( nc ) {
nc.close();
nc = null;
}
ctx = null;
cns = null;
if ( texture1 ) texture1.dispose();
if ( texture2 ) texture2.dispose();
videoImage.texture = null;
_texturesCompleted = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment