Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use the NetConnection statuses rather: NetConnection.Connect.Closed</p> <p>I ran into a similar issue &amp; noticed that the NetStream.bytesTotal value is truncated when a video connection fails. Thus, capture the video length in bytes according to the NetStream to begin with &amp; use it to detect a failure when NetConnection.Connect.Closed is called.</p> <p>Because video code is so impossible to explain out of context I put this together, building on Flash's example @ <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetConnection.html#includeExamplesSummary" rel="nofollow">Flash NetConnection Example</a></p> <p>Here's the code, comments inline:</p> <pre><code>public class NonTruncatedNetConnectionExample extends Sprite { private var videoURL:String = "http://www.helpexamples.com/flash/video/cuepoints.flv"; private var connection:NetConnection; private var stream:NetStream; private var video:Video = new Video(); // ADDITION: special length variable to check for truncation private var videoBytes:uint; public function NonTruncatedNetConnectionExample() { connection = new NetConnection(); connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); connection.connect(null); } private function netStatusHandler(event:NetStatusEvent):void { switch (event.info.code) { case "NetConnection.Connect.Success" : connectStream(); break; case "NetStream.Play.StreamNotFound" : trace("Stream not found: " + videoURL); break; // ADDITION: this will be triggered when the connection is closed // on completion, or on failure case "NetConnection.Connect.Closed" : if (this.videoBytes != this.stream.bytesTotal) { // failure // you can throw the error here // or in the loadingProgress function below } else { // success // the video loaded completely } break; } } private function securityErrorHandler(event:SecurityErrorEvent):void { trace("securityErrorHandler: " + event); } private function connectStream():void { var stream:NetStream = new NetStream(connection); stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); stream.client = new CustomClient(); video.attachNetStream(stream); stream.play(videoURL); addChild(video); // ADDITION: the loadingProgress function this.addEventListener(Event.ENTER_FRAME, loadingProgress); } /*** ** ADDITION : loadingProgress captures the length of the video in bytes for use in comparison when ** NetConnection.Connect.Closed is called ** It also tracks current loading progress so you can use it as a buffer indicator */ private function loadingProgress(E:Event):void { // check that this.stream.client has initialised &amp; you have correct access to this.stream variables // bytesTotal is also a divisor so must be greater than 0 before continuing, or it will hard-fail if (this.stream.client &amp;&amp; this.stream.bytesTotal &gt; 0) { // sanity checker ;) trace("bytesLoaded = " + this.stream.bytesLoaded + " :: bytesTotal = " + this.stream.bytesTotal); // capture the video's total bytes only if the variable does not yet exist // before this point this.stream.bytesTotal returns a bogus (really big) number // watch out for capturing this.stream.totalBytes before this point, or you create a double negative, // because it won't match the actual bytesTotal &amp; will therefore error [ it got me :( ; but then I got it ;) ] if (!this.videoBytes) this.videoBytes = this.stream.bytesTotal; // compare this to stream.totalBytes to detect truncation if (this.videoBytes != this.stream.bytesTotal) { // error // you can throw the error here if you want, or wait for the NetConnection.Connect.Closed switch above } else { // or you can detach this event listener here while just holding on to the videoBytes variable // &amp; wait for the NetConnection.Connect.Closed switch above // e.g. this.removeEventListener(Event.ENTER_FRAME, loadingProgress); } // use this to drive a buffer bar if you want var radian:Number = (this.stream.bytesLoaded / this.totalBytes); var percent:Number = radian * 100; } } } </code></pre> <p>And also:</p> <pre><code>class CustomClient { public function onMetaData(info:Object):void { trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate); } public function onCuePoint(info:Object):void { trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload