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

by Peter deHaan on July 23, 2007

in VideoDisplay, VideoEvent

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 comments… read them below or add one }

1 Bram May 15, 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

Reply

2 Durairaj August 9, 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..

Reply

3 CeeJ August 13, 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?

Reply

4 peterd August 13, 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

Reply

5 CeeJ August 13, 2008 at 8:41 am

Done. Number is SDK-16455.

Reply

6 Chas September 29, 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…

Reply

7 peterd September 29, 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

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: