Displaying playback progress

Creating a Timer object to drive playback monitoring is complex operation that you should only have to code once. Encapsulating this Timer logic in a reusable class like the SoundFacade class lets applications listen to the same kinds of progress events when a sound is loading and when it is playing.

The Timer object that is created by the SoundFacade.play() method dispatches a TimerEvent instance every second. The following onPlayTimer() method executes whenever a new TimerEvent arrives:

public function onPlayTimer(event:TimerEvent):void 
{
    var estimatedLength:int = 
        Math.ceil(this.s.length / (this.s.bytesLoaded / this.s.bytesTotal));
    var progEvent:ProgressEvent = 
        new ProgressEvent(PLAY_PROGRESS, false, false, this.sc.position, estimatedLength);
    this.dispatchEvent(progEvent);
}

The onPlayTimer() method implements the size estimation technique described in the section Monitoring playback. Then it creates a new ProgressEvent instance with an event type of SoundFacade.PLAY_PROGRESS, with the bytesLoaded property set to the current position of the SoundChannel object and the bytesTotal property set to the estimated length of the sound data.


Flash CS3