The following example shows how you can determine when the volume has changed on a Spark VideoPlayer control in Flex 4 by listening for the internal volumeChanged event.

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 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/06/11/detecting-when-the-volume-changes-on-a-spark-videoplayer-control-in-flex-4/ -->
<s:Application name="Spark_VideoPlayer_volumeChanged_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">

    <fx:Script>
        <![CDATA[
            private function init():void {
                videoPlayer.addEventListener("volumeChanged", videoPlayer_volumeChanged);
            }

            private function videoPlayer_volumeChanged(evt:Event):void {
                var obj:Object = {};
                obj.type = evt.type;
                obj.volume = evt.currentTarget.volume.toFixed(2);
                obj.time = new Date().toTimeString().split(" ")[0];
                arrList.addItemAt(obj, 0);
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <s:ArrayList id="arrList" />
    </fx:Declarations>

    <s:HGroup left="20" right="20"
            verticalCenter="0"
            gap="20">
        <s:VideoPlayer id="videoPlayer"
                source="http://helpexamples.com/flash/video/cuepoints.flv"
                muted="true"
                initialize="init();" />
        <mx:DataGrid id="dataGrid"
                dataProvider="{arrList}"
                verticalScrollPolicy="on"
                width="100%"
                height="100%">
            <mx:columns>
                <mx:DataGridColumn dataField="type" />
                <mx:DataGridColumn dataField="volume" />
                <mx:DataGridColumn dataField="time" />
            </mx:columns>
        </mx:DataGrid>
    </s:HGroup>

</s: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/2009/06/11/detecting-when-the-volume-changes-on-a-spark-videoplayer-control-in-flex-4/ -->
<s:Application name="Spark_VideoPlayer_volumeChanged_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        initialize="init();">

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import spark.components.HGroup;
            import mx.core.ScrollPolicy;
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.collections.ArrayList;
            import mx.controls.DataGrid;
            import spark.components.VideoPlayer;

            [Bindable]
            private var arrList:ArrayList;
            private var videoPlayer:VideoPlayer;
            private var dataGrid:DataGrid;

            private function init():void {
                arrList = new ArrayList();

                videoPlayer = new VideoPlayer();
                videoPlayer.source = "http://helpexamples.com/flash/video/cuepoints.flv";
                videoPlayer.muted = true;
                videoPlayer.addEventListener("volumeChanged", videoPlayer_volumeChanged);

                var cols:Array = [];
                cols.push(new DataGridColumn("type"));
                cols.push(new DataGridColumn("volume"));
                cols.push(new DataGridColumn("time"));

                dataGrid = new DataGrid();
                dataGrid.dataProvider = arrList;
                dataGrid.columns = cols;
                dataGrid.verticalScrollPolicy = ScrollPolicy.ON;
                dataGrid.percentWidth = 100;
                dataGrid.percentHeight = 100;

                var hGr:HGroup = new HGroup();
                hGr.gap = 20;
                hGr.left = 20;
                hGr.right = 20;
                hGr.verticalCenter = 0;
                hGr.addElement(videoPlayer);
                hGr.addElement(dataGrid);
                addElement(hGr);
            }

            private function videoPlayer_volumeChanged(evt:Event):void {
                var obj:Object = {};
                obj.type = evt.type;
                obj.volume = evt.currentTarget.volume.toFixed(2);
                obj.time = new Date().toTimeString().split(" ")[0];
                arrList.addItemAt(obj, 0);
            }
        ]]>
    </fx:Script>

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

 
Tagged with:
 
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.

One Response to Detecting when the volume changes on a Spark VideoPlayer control in Flex 4

  1. Tshibangu says:

    Hi,

    I found your example very nice.
    I need to do the same thing (control bar which disappear after a delay) and a custom
    a little bit similar.
    Very can I find the source code of the control bar please?
    I am desperate. :(

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