Created
July 26, 2017 19:55
-
-
Save tuarua/b4af96b7f30a5b71483cae3f12976e33 to your computer and use it in GitHub Desktop.
Looping Starling Video for Desktop
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
package { | |
import starling.core.Starling; | |
import starling.display.BlendMode; | |
import starling.display.Sprite; | |
import starling.display.Image; | |
import starling.textures.Texture; | |
import flash.events.AsyncErrorEvent; | |
import flash.events.NetStatusEvent; | |
import flash.events.SecurityErrorEvent; | |
import flash.net.NetConnection; | |
import flash.net.NetStream; | |
import flash.net.NetStreamAppendBytesAction; | |
import flash.utils.ByteArray; | |
import starling.textures.TextureSmoothing; | |
public class StarlingRoot extends Sprite { | |
[Embed(source="issue_video_1920x1080.flv", mimeType="application/octet-stream")] | |
public var videoBytes:Class; | |
private var videoTexture:Texture; | |
private var videoImage:Image; | |
private var _connection:NetConnection; | |
private var _stream:NetStream; | |
private var _byteArray:ByteArray; | |
public function StarlingRoot() { | |
} | |
public function start():void { | |
_byteArray = new videoBytes(); | |
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 onMetaData(metaData:Object):void { | |
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); | |
videoTexture = Texture.fromNetStream(_stream, Starling.current.contentScaleFactor, onTextureComplete); | |
_stream.play(null); | |
_stream.appendBytes(_byteArray); | |
} | |
private function onTextureComplete():void { | |
videoImage = new Image(videoTexture); | |
videoImage.blendMode = BlendMode.NONE; | |
videoImage.touchable = false; | |
setSize(); | |
if (!this.contains(videoImage)) { | |
this.addChildAt(videoImage, 0); | |
} | |
} | |
public function setSize():void { | |
var scaleFactor:Number = 1280 / videoTexture.nativeWidth; | |
videoImage.scaleY = videoImage.scaleX = scaleFactor; | |
if (videoTexture.nativeWidth == 1280) { | |
videoImage.textureSmoothing = TextureSmoothing.NONE; | |
} else { | |
videoImage.textureSmoothing = TextureSmoothing.BILINEAR; | |
} | |
} | |
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 { | |
} | |
private function asyncErrorHandler(event:AsyncErrorEvent):void { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credits: http://blog.redannick.com/seamlessly-looping-video-files-in-as3-with-netstream-appendbytes/