Last active
September 18, 2019 10:12
-
-
Save redannick/7772246 to your computer and use it in GitHub Desktop.
Seamless flv looping
This file contains hidden or 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
package | |
{ | |
import flash.events.AsyncErrorEvent; | |
import flash.events.NetStatusEvent; | |
import flash.events.SecurityErrorEvent; | |
import flash.media.Video; | |
import flash.net.NetConnection; | |
import flash.net.NetStream; | |
import flash.net.NetStreamAppendBytesAction; | |
import flash.utils.ByteArray; | |
public class AppendByteVideoLoop | |
{ | |
private var _video:Video; | |
private var _connection:NetConnection; | |
private var _stream:NetStream; | |
private var _byteArray:ByteArray; | |
public function AppendByteVideoLoop(video:Video, data:ByteArray):void | |
{ | |
_video = video; | |
_byteArray = data; | |
connect(); | |
} | |
private function connect():void | |
{ | |
_connection = new NetConnection(); | |
_connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); | |
_connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); | |
_connection.connect(null); | |
} | |
private function addToStream():void | |
{ | |
_stream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN); | |
_stream.appendBytes(_byteArray); | |
} | |
public function destroy():void | |
{ | |
_stream.close(); | |
_stream = null; | |
_connection.close() | |
_connection = null; | |
} | |
public function togglePause():void | |
{ | |
_stream.togglePause() | |
} | |
public function onMetaData(metaData:Object):void | |
{ | |
_video.width = metaData.width; | |
_video.height = metaData.height; | |
addToStream(); | |
} | |
public function onXMPData(xmp:Object):void { } | |
public function onPlayStatus(status:Object):void { } | |
private function connectStream():void | |
{ | |
_stream = new NetStream(_connection); | |
_stream.client = this; | |
_stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); | |
_stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); | |
_video.attachNetStream(_stream); | |
_stream.play(null); | |
_stream.appendBytes(_byteArray); | |
} | |
private function netStatusHandler(event:NetStatusEvent):void | |
{ | |
switch (event.info.code) | |
{ | |
case "NetConnection.Connect.Success": | |
connectStream(); | |
break; | |
case "NetStream.Play.StreamNotFound": | |
trace(event.info.code, "Unable to locate video data "); | |
break; | |
} | |
} | |
private function securityErrorHandler(event:SecurityErrorEvent):void | |
{ | |
trace("securityErrorHandler: " + event); | |
} | |
private function asyncErrorHandler(event:AsyncErrorEvent):void | |
{ | |
trace("asyncErrorHandler: " + event); | |
} | |
} | |
} |
This file contains hidden or 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
package | |
{ | |
import flash.events.AsyncErrorEvent; | |
import flash.events.NetStatusEvent; | |
import flash.events.SecurityErrorEvent; | |
import flash.media.Video; | |
import flash.net.NetConnection; | |
import flash.net.NetStream; | |
import flash.net.NetStreamAppendBytesAction; | |
import flash.utils.ByteArray; | |
public class AppendByteVideoLoopWithIntro | |
{ | |
private var _video:Video; | |
private var _connection:NetConnection; | |
private var _stream:NetStream; | |
private var _byteArrayIntro:ByteArray; | |
private var _byteArrayLoop:ByteArray; | |
public function AppendByteVideoLoopWithIntro(video:Video, dataIntro:ByteArray, dataLoop:ByteArray):void | |
{ | |
_video = video; | |
_byteArrayIntro = dataIntro; | |
_byteArrayLoop = dataLoop; | |
connect(); | |
} | |
private function connect():void | |
{ | |
_connection = new NetConnection(); | |
_connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); | |
_connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); | |
_connection.connect(null); | |
} | |
private function addToStream():void | |
{ | |
_stream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN); | |
_stream.appendBytes(_byteArrayLoop); | |
} | |
public function togglePause():void | |
{ | |
_stream.togglePause() | |
} | |
public function destroy():void | |
{ | |
_stream.close(); | |
_stream = null; | |
_connection.close() | |
_connection = null; | |
} | |
public function onMetaData(metaData:Object):void | |
{ | |
_video.width = metaData.width; | |
_video.height = metaData.height; | |
addToStream(); | |
} | |
public function onXMPData(xmp:Object):void { } | |
public function onPlayStatus(status:Object):void { } | |
private function connectStream():void | |
{ | |
_stream = new NetStream(_connection); | |
_stream.client = this; | |
_stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); | |
_stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); | |
_video.attachNetStream(_stream); | |
_stream.play(null); | |
_stream.appendBytes(_byteArrayIntro); | |
} | |
private function netStatusHandler(event:NetStatusEvent):void | |
{ | |
switch (event.info.code) | |
{ | |
case "NetConnection.Connect.Success": | |
connectStream(); | |
break; | |
case "NetStream.Play.StreamNotFound": | |
trace(event.info.code, "Unable to locate video data "); | |
break; | |
} | |
} | |
private function securityErrorHandler(event:SecurityErrorEvent):void | |
{ | |
trace("securityErrorHandler: " + event); | |
} | |
private function asyncErrorHandler(event:AsyncErrorEvent):void | |
{ | |
trace("asyncErrorHandler: " + event); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Brilliant! Thanks for this!