Chapter 13. Programming Sound

Introduction

You can add sound to your movie at authoring time by importing a sound into a layer of a timeline. Furthermore, you can set the volume and pan for that sound at authoring time. However, to control the sound during runtime, you must use a Sound object. With a Sound object, you can programmatically control sounds in ways that you cannot at authoring time, including:

  • Attaching sounds from the library

  • Loading external MP3s

  • Starting and stopping playback based on user input, server responses, or other runtime events

  • Adjusting the sound panning and volume based on user input, server responses, or other runtime events

  • Controlling the playback position of a sound (such as user-controlled seek, fast-forward, and rewind)

  • Accessing the sound’s total duration

  • Reading song data (artist name, title, etc.) from ID3 tags (for loaded MP3s only)

Sound objects control the sounds that exist within a movie clip—whether those sounds were placed within the movie clip at authoring time or attached or loaded into the movie clip at runtime using ActionScript. It is possible to place multiple sounds within a single movie clip; however, doing so is poor practice because it prevents you from controlling the sounds individually. Rather, in most cases, you should create separate movie clips for each sound and create a separate Sound object to target each movie clip.

The hierarchy of movie clip timelines also comes into play with sounds. The volume and panning for sounds in ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.