Detecting when a video cannot be loaded by the Spark VideoPlayer control in Flex 4

by Peter deHaan on December 15, 2009

in VideoPlayer (Spark), beta2

The following example shows how you can detect when an error was encountered while trying to play media using the Spark VideoPlayer control in Flex 4 by listening for the mediaPlayerStateChange event and checking for the playbackError state.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 beta, check out the Adobe Flash Builder 4 page on the Adobe Labs site. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4. For instructions on using the beta Flex 4 SDK in Flex Builder 3, see "Using the beta Flex 4 SDK in Flex Builder 3".

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/12/15/detecting-when-a-video-cannot-be-loaded-by-the-spark-videoplayer-control-in-flex-4/ -->
<s:Application name="Spark_VideoPlayer_mediaPlayerStateChange_playbackError_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark">
    <s:controlBarContent>
        <s:Button label="Good source" click="vidPlyr.source = goodStream;" />
        <s:Button label="Bad source" click="vidPlyr.source = badStream;" />
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import org.osmf.events.MediaPlayerStateChangeEvent;
            import org.osmf.media.MediaPlayerState;
 
            protected function vidPlyr_mediaPlayerStateChangeHandler(evt:MediaPlayerStateChangeEvent):void {
                switch (evt.state) {
                    case MediaPlayerState.LOADING:
                        vidPlyr.errorString = "";
                        break;
                    case MediaPlayerState.PLAYBACK_ERROR:
                        Alert.show("Unable to load video", evt.state);
                        vidPlyr.errorString = "Unable to load video";
                        break;
                }
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <s:DynamicStreamingVideoSource id="goodStream" host="rtmp://fmsexamples.adobe.com/vod/" streamType="recorded">
            <s:DynamicStreamingVideoItem streamName="mp4:_cs4promo_1000.f4v" />
        </s:DynamicStreamingVideoSource>
 
        <s:DynamicStreamingVideoSource id="badStream" host="rtmp://fmsexamples.adobe.com/vod/" streamType="recorded">
            <s:DynamicStreamingVideoItem streamName="mp4:_cs4promo_404_1000.f4v" />
        </s:DynamicStreamingVideoSource>
    </fx:Declarations>
 
    <s:VideoPlayer id="vidPlyr"
            loop="true"
            muted="true"
            left="100" right="100"
            top="100" bottom="100"
            horizontalCenter="0" verticalCenter="0"
            mediaPlayerStateChange="vidPlyr_mediaPlayerStateChangeHandler(event);" />
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

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: