Search code examples
flashapache-flexactionscript-3mxmlmxmlc

attach progressbar


In the following code how can the progress bar be attached to the sound and indicate it progress

    <mx:Script>
        <![CDATA[

            import flash.media.*;
            import flash.net.NetStream;

            [Embed(source="new1.mp3")]
            [Bindable]
            public var sndCls:Class;

            public var snd:Sound = new sndCls() as Sound;
            public var sndChannel:SoundChannel;
            private var recordingState:String = "idle";

            public function playSound():void {
                sndChannel=snd.play();
            }

            public function stopSound():void {
               sndChannel.stop();
            }

        ]]>
    </mx:Script>


     <mx:Button label="Play" click="playSound()" />
     <mx:ProgressBar x="30" y="36" mode="manual" id="audioprogress" label=""
                     labelPlacement="bottom" width="220" fontSize="10" 
                     fontWeight="normal"/>
 </mx:Application>

Solution

  • Here is the easiest way I know how:

    First, create two functions in your script section:

    public function get bytesLoaded():Number {
        if(sndChannel == null)
            return 0;
    
        return sndChannel.position;
    }
    
    public function get bytesTotal():Number {
        return snd.length;
    }
    
    public function clearProgress():void {
        sndChannel = null;
    }
    

    Then, change your ProgressBar to the "polled" mode and set the source to this

    <mx:ProgressBar ... mode="polled" source="{this}" />
    

    Works perfectly for me :)

    NOTE The functions need to be named bytesLoaded and bytesTotal. It is part of the "polling" mode of the ProgressBar. If you really want "manual" mode, you will need to create a timer, which is more complicated than this mechanism.