0

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>
Rajeev
  • 44,985
  • 76
  • 186
  • 285

1 Answers1

1

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.

Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
  • Thanks it worked,so if the song is played again how to reset the initialize the progress bar again.. – Rajeev Feb 02 '11 at 17:57
  • @Rajeev: Simply nulling the `sndChannel` should do it. Also, as soon as you press play again, the progress goes back to zero. If you want it to go back before you press play, you can always switch into "manual" mode (`audioprogress.mode = "manual"`), set the progress (`audioprogress.setProgress(0, snd.length)`) and then back to "polled". But, if you do that, make sure the sndChannel is nulled or something like that, because the `ProgressBar` will start asking the question again. If it is the same `sndChannel` then the progress will be set right back to where it was. Hope that helps :) – Brian Genisio Feb 02 '11 at 19:11
  • could please edit and the answer and give me the code.Thanks for all the help.Really appreciate it – Rajeev Feb 03 '11 at 11:41
  • @Rajeev: I think it really is out of the scope of this question, but I did it anyways. Depending on what behavior you want, you might have to do it a different way. I put up the most quick/dirty way to clear the progress back to zero. Of course, it assumes you are done with that sound channel. You can also do the same thing in `stopSound()`. – Brian Genisio Feb 03 '11 at 11:53