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:

View MXML

<?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:

View MXML

<?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>
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

0 Responses to Detecting a connection error when loading an FLV with the VideoDisplay control

  1. Bram says:

    Is there a way to recover from this unresponsive state? How can we provide an alternative file when the error occurs?

    Thanks

  2. Durairaj says:

    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..

  3. CeeJ says:

    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?

  4. peterd says:

    CeeJ,

    Can you please file a bug/enhancement request at http://bugs.adobe.com/flex/ and post the bug number here?

    Peter

  5. CeeJ says:

    Done. Number is SDK-16455.

  6. Chas says:

    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…

  7. peterd says:

    FYI, I filed a new bug at http://bugs.adobe.com/jira/browse/SDK-17101 for the connectionError state 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

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree