23
Jul
07

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

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>

7 Responses to “Detecting a connection error when loading an FLV with the VideoDisplay control”


  1. 1 Bram May 15th, 2008 at 3:57 am

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

    Thanks

  2. 2 Durairaj Aug 9th, 2008 at 3:54 pm

    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. 3 CeeJ Aug 13th, 2008 at 7:45 am

    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. 4 peterd Aug 13th, 2008 at 8:19 am

    CeeJ,

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

    Peter

  5. 5 CeeJ Aug 13th, 2008 at 8:41 am

    Done. Number is SDK-16455.

  6. 6 Chas Sep 29th, 2008 at 12:55 pm

    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. 7 peterd Sep 29th, 2008 at 2:10 pm

    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

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




July 2007
M T W T F S S
    Aug »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Badge Farm

  • Firefox 2
  • Powered by Redoable 1.2
  • Feeds burnt by Feedburner
  • Feed