Accessibility

ActionScript Article

 

Handling cue points for audio files in ActionScript 2.0 and ActionScript 3.0


David Stiller

David Stiller

quip.net

Table of Contents

Created:
9 October 2006
Modified:
3 December 2007
User Level:
Intermediate
Products:
Flash

Both Flash CS3 Professional and Flex Builder 2 support cue points for Flash video (FLV) files out of the box. In Flash CS3, all you need is an instance of the FLVPlayback component and you're set. You may define cue points at any number of steps along the way: during the Import Video wizard, by using the Component Inspector panel or Property inspector, or by calling the FLVPlayback.addAsCuePoint() method as needed with ActionScript. In Flex Builder 2, all you need is an instance of the CuePointManager class associated with a VideoDisplay instance. In either case, set up a listener for cuePoint events and respond as desired.

Cue points for audio files are a bit harder to implement. Although it's possible in Flash CS3 to load MP3 files with one of the media components (MediaController, MediaDisplay, or MediaPlayback), keep in mind that these components are geared toward Flash Player 6 and 7 and require ActionScript 2.0. Built on an older architecture than FLVPlayback, any one of the media components adds a minimum 55K file size to the published SWF file. FLVPlayback adds only 49K in ActionScript 3.0 documents (35K in ActionScript 2.0 documents), but the newer component does not load MP3 files. Likewise in Flex Builder 2, the VideoDisplay class loads only video. To work around this, you could produce audio-only FLV files (that is, files with no video content), but this practice isn't particularly widespread. In team settings, especially, you may have no control over external assets. Chances are significantly high that your audio will be comprised of MP3 files. Fortunately, a bit of ActionScript quickly fits the bill.

In this article, you'll explore two versions of one particular solution: a custom SoundSync class that supports cue points for internal sound assets or external MP3 files. This code will be presented in ActionScript 3.0 for Flash CS3 or Flex Builder 2 and in ActionScript 2.0 for Flash CS3 documents intended for publication to players earlier than Flash Player 9.

In either language, your approach is the same. Both versions of ActionScript provide a Sound class capable of presenting embedded or loaded audio files. Both languages allow audio to be played, either from the beginning or from a point after that, paused, looped, and more. You certainly don't want to reinvent that wheel! All the existing functionality is useful. The only thing missing is support for cue points, so it makes sense to extend the existing Sound class in order to retain the original feature set.

The custom SoundSync adds the following new public methods to its base class:

  • addCuePoint(): Adds a cue point object
  • getCuePoint(): Returns a cue point object based on its cue point name or time
  • removeCuePoint(): Removes a cue point object based on its cue point name or time
  • removeAllCuePoints(): Removes all cue point objects

In addition, a number of private methods are added. Some are overridden, too, depending on the language used. In a nutshell, this SoundSync class uses a timer to repeatedly check the current cue point object's time property against the current position of the sound asset in play. Even with the advent of Flash CS3 support for ActionScript 3.0, which provides numerous improvements over its predecessor, a great number of Flash developers (as of this writing) are still on the migration path toward the new language. Let's begin in ActionScript 2.0, to cover the basic concepts of this class and then translate the solution to ActionScript 3.0.

Requirements

In order to make the most of this article, you need the following software and files:

Flash CS3 Professional

Flex Builder 2 (optional)

Sample files:

Prerequisite knowledge

Intermediate knowledge of ActionScript 2.0 and ActionScript 3.0.  Introductory experience with Flash CS3 Professional or Flex Builder 2.

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial 3.0 Unported License.

About the author

David is a career multimedia programmer/designer whose portfolio includes NASA, Adobe, and major US automotive and boat manufacturers. He likes anaglyph 3D photography, finely crafted wooden game boards, Library of Congress field recordings, and Turkish coffee. David is self-taught and gets a kick out of sharing "aha!" moments with others through consultation, mentoring, and regular contributions to the Adobe Flash and ActionScript forums. He is a resident author at Community MX, a web development training site geared toward Adobe products. David lives in Virginia with his amazing wife, Dawn, and his beguiling daughter, Meridian.