The other day I was playing with the VideoDisplay control in Flex and was curious why my simple test cases were not working. Well, as it turns out, my remote server wasn’t available so the Flash Video (FLV) couldn’t be loaded. So I quickly typed up the following code to listen for the stateChange event to see if my VideoDisplay control was getting into the dreaded connectionError state (represented by the VideoDisplay.CONNECTION_ERROR constant).
Full code after the jump.
The following example listens for the stateChange event to determine if the VideoDisplay control entered the VideoEvent.CONNECTION_ERROR state:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/07/23/detecting-a-connection-error-when-loading-an-flv-with-the-videodisplay-control/ -->
<mx:Application name="VideoDisplay_stateChange_connectionError_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white" >
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.VideoEvent;
private function videoDisplay_stateChange(evt:VideoEvent):void {
switch (evt.state) {
case VideoEvent.CONNECTION_ERROR:
evt.currentTarget.visible = false;
Alert.show(evt.currentTarget.source, "Unable to connect to video");
break;
}
}
]]>
</mx:Script>
<mx:VideoDisplay id="videoDisplay"
source="http://www.helpexamples.com/flash/video/404.flv"
stateChange="videoDisplay_stateChange(event);" />
</mx:Application>
View source is enabled in the following example.
Due to popular demand, here is the “same” example in a more ActionScript friendly format:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/07/23/detecting-a-connection-error-when-loading-an-flv-with-the-videodisplay-control/ -->
<mx:Application name="VideoDisplay_stateChange_connectionError_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
initialize="init();">
<mx:Script>
<![CDATA[
import mx.controls.VideoDisplay;
import mx.controls.Alert;
import mx.events.VideoEvent;
private var videoDisplay:VideoDisplay;
private function init():void {
videoDisplay = new VideoDisplay();
videoDisplay.addEventListener(VideoEvent.STATE_CHANGE,
videoDisplay_stateChange);
videoDisplay.source = "http://www.helpexamples.com/flash/video/404.flv";
addChild(videoDisplay);
}
private function videoDisplay_stateChange(evt:VideoEvent):void {
switch (evt.state) {
case VideoEvent.CONNECTION_ERROR:
evt.currentTarget.visible = false;
Alert.show(evt.currentTarget.source, "Unable to connect to video");
break;
}
}
]]>
</mx:Script>
</mx:Application>




Is there a way to recover from this unresponsive state? How can we provide an alternative file when the error occurs?
Thanks
Bram
Inside the error event you can reassign the source to some other alternative file.give an id for VideoDisplay and use VideoDisplay id .source=alternative file.
hope this helps..
This just what I was looking for… thanks!
Here’s another question, though. This code will throw the error when the file is missing or unavailable. However, it doesn’t seem to throw an error if the file is not a valid FLV file.
To test, I made a simple 0 byte text file, renamed it test.flv, and tried to load it. I traced the VideoDisplay’s state changes, but all I see is “loading” and “buffering,” and the application is stuck there. No connectionError. Any idea how one might test for validity of a FLV before attempting to play it?
CeeJ,
Can you please file a bug/enhancement request at http://bugs.adobe.com/flex/ and post the bug number here?
Peter
Done. Number is SDK-16455.
Yeah, the problem here gents is once the videoDisplay has gotten a bad source, it is completely broken and will not play a good one (without recreating the videoDisplay). UGH. Terrible coding from the boys at adobe, hope they fix it in the next Flex version…
FYI, I filed a new bug at http://bugs.adobe.com/jira/browse/SDK-17101 for the
connectionErrorstate locking the VideoDisplay control. Feel free to vote/comment (preferrably constructive). Also, if anybody has a better workaround, please add it to the bug notes.Peter