Section 8.2. Controlling External Sounds


8.2. Controlling External Sounds

One of the best ways to keep file size, and therefore download time, to a minimum is by keeping as many assets as possible external to your main file. You will look at this extensively in Chapter 12, but you will focus exclusively on sound in this chapter. Loading and playing external sounds makes the playback experience fast and efficient, and makes it easier to work on the project as a whole. Publishing your files will be faster because you won't have to compress internal sounds each time, and it's easier to update external files because editing the main .fla is usually not required.

There are a few ways to work with external sounds. Here, you will look at how to use components to load and control the audio files with a little scripting, and then you will see how do similar things by writing code of your own.

8.2.1. Encoding

You read earlier in this chapter that Flash can import a variety of sound formats. When it comes to loading external sounds, however, MP3 is the only encoding option. The same guidelines that applied to encoding imported sounds also apply to encoding external soundsparticularly making your sample rate a derivative of 44.1 kHz.

8.2.2. Media Components

In some projects, all you need or want is a simple controller that will load your file and play it. In other cases, a simple controller is needed to allow the user to play back the sounds as desired.

Your next mini-project is to whip up a controller in just a few seconds, to show you how easy it can be:

  1. Create a new Flash file, and save it as media_components_mp3.fla in your 08 folder. Give it dimensions of 320 x 90 pixels.

  2. Name the first layer components, and create a new layer called actions.

  3. Open the Components panel and drag the MediaDisplay component to the Stage in the components layer. It can be found in the Media Player 67 components category. (Despite the category name, these components also work in Flash Player 8.) This will specify the source file to playin this case, an MP3.


    Note: When working with video files, the MediaPlayback component contains everything you need. However, this is awkward when using MP3 files because they have no visual data. The separate MediaDisplay and MediaController components are an alternative to the integrated MediaPlayback. They allow you to target a source in the display component but tuck it away by positioning it off-Stage or making it small. The controller can then appear self-contained.

  4. Using the Properties panel, size the MediaDisplay component to 10 x 10 pixels, position it at (0, 0), and give it an instance name of my_display.

  5. From the same category in the Components panel, drag the MediaController component to the Stage in the components layer. This will control the MP3 file specified in the MediaDisplay component. Position it at (10, 10), and give it an instance name of my_ controller. Your Stage should now look like Figure 8-6.

    Figure 8-6. The MediaController component controls the MediaDisplay component, minimized but visible in the extreme upper left of the Stage

  6. As you have seen before, one way to set the component parameters is by using the Component Inspector. Select the controller and open the Component Inspector (Window Component Inspector). The default display behavior of the controller is to show a progress bar continuously, but to automatically show the controller elements only when rolling over the controller. In the Component Inspector, change the auto to on. This setting always shows the controller. For this mini-project, this is the only setting you need to configure in the controller.

    All that remains is to identify which MP3 to playin this case, the Nero music track you used earlierand to associate the controller with the display. This specifies which display, and therefore which MP3, to control. Using the Component Inspector to configure parameters is great for anyone who doesn't know ActionScript, but you are reading to learn, so you'll set the remaining minimal parameters using code. Even though you already set the controller display mode using the Component Inspector, the ActionScript to accomplish this has been included here for the sake of completeness:

  7. Add the following script to frame 1 in the actions layer:

     my_display.contentPath = "nero.mp3"; my_display.associateController(my_controller); my_controller.controllerPolicy = "on"; 

  8. Save your work and test the movie. Your file should now resemble media_components_mp3.fla. The sound file should begin playing immediately, which is the default behavior, and you should be able to control its playback with the controller.

Data Types

Data types, by themselves, are exactly what their name implies: types of data. For many reasons, it is helpful to be able to distinguish one kind of data from another when running a script. For example, to avoid errors, it would be helpful to know that you were performing a mathematical operation on a string if you thought the data you were manipulating was really a number.

Flash recognizes many data types. Some are quite general programming types, such as Number (a numeric value), String (text, or a string of characters), and Boolean (a true or false value, which you read about when learning about conditionals in Chapter 7). Still others are very Flash-specific. For example, when you refer to a movie clip in a script, it too has a data type: MovieClip. There are also data types such as Sound and Video. Many ActionScript classes, including MovieClipLoader and LoadVars (both of which you will use in this book), have associated data types, too.

If most of the focus on data types is behind the scenes, as it has been so far in this book, you don't have to worry about it. The original version of ActionScript, now referred to as ActionScript 1.0, automatically cast data into the correct type whenever possible. This is known as dynamic typing because the data types are dynamically reassigned.

The downside to this approach, however, is that you aren't warned when this occurs, so you have no way of knowing it's happened. Consequently, it's harder to know how to find a bug in your code.

ActionScript 2.0, first introduced in Flash MX 2004, brought true object-oriented programming to Flash.

ActionScript 2.0 is not an entirely new language, but it is much more mature and capable than its predecessor. In some ways, it is more similar to robust low-level languages such as Java. The object-oriented aspects of ActionScript 2.0 are beyond the scope of this text. However, you will look at another important concept ActionScript 2.0 introduced, which does not rely on object-oriented techniques: static typing.

Unlike the dynamic typing of ActionScript 1.0, static typing requires that you declare what kind of data will be stored in a variable, for example, and will not automatically cast to another type. This means an extra step or two when writing your scripts, but it has the huge advantage of warning you about data type mismatches when you compile your code. Right away, you know there is an error and where it is. If you can determine why a mathematical operation is being applied to a string, for example, you will be able to squash your bugs faster and more effectively.

The data type of a variable is specified using post-colon syntax. That is, the var keyword begins a variable declaration statement, and the data type immediately follows a colon after the variable name. This can be done without any further assignment, as a way of declaring your variables and data types before scripting, or along with an assignment for brevity. Examples of both techniques follow:

 var myNum:Number; var myString:String = "Flash"; 

When this syntax is used, the ActionScript 2.0 compiler can warn you if you make a mistake related to data types. For instance, it can display a type mismatch error warning when data isn't of the expected type. If you try the following, you will be warned of a type mismatch because you are trying to assign a string to a number:

 var myNum:Number = "Flash"; 

Similarly, the compiler can warn you when you try to use an unsupported method or property. For example, it should make sense that you can't specify an x-coordinate for a sound. Without visual data, you can't position a sound on the Stage. Therefore, if you try to set the _x movie clip property for the Sound class (which you'll learn about in the section " Scripting Your Own Sound Control"), you will get a warning that no such property exists:

 var mySound:Sound; mySound._x = 10; 

It's also a good idea to type the data passed into a function and returned from a function. Argument typing is similar to variable typing, but the var keyword is not required. Typing a value returned by a function is a bit different. This typing statement is placed just after the function constructor. The following example tells the compiler that a number should be passed into the argument, and that a number should also be returned by the function:

 function timesThree(whichNum:Number):Number {     return whichNum * 3; } 

When a value is not returned by the function, using a data type of void is recommended. This tells the compiler to alert you if anything unexpected is returned:

 function timesThree(whichNum:Number):Void {     trace(whichNum * 3); } 

Finally, for completeness's sake, it is usually recommended that anonymous functions, even without arguments, declare no return by using the void data type:

 my_btn.onRelease = function():Void {     my_mc.play(); }; 

You will find that this is frequently omitted, simply because the scripter knows no value is returned. However, it is still recommended. Type checking is meant to help you with compile-time error reports. Omitting type declarations in ActionScript 2.0 will not prevent your file from compiling, but you won't receive any notifications of possible errors, either.

You may be asking yourself why, when you didn't make type declarations in the prior chapter examples, you didn't receive errors. There are two reasons. First, when you're learning a new topic, it's helpful to learn it in digestible pieces. This is another example of chunking information. Second, and more importantly, current Flash Players must remain compatible with older projects. If a data type is not specified for a variable or object, the compiler won't perform type checking. This allows older code to go through the ActionScript 2.0 compiler without generating errors.

Although backward compatibility allows untyped objects to be compiled without error, it is considered a best practice to use static typing. All of the examples in the last half of this book will follow this guideline.

Information about additional issues relevant to static typing, such as using the Object data type when more than one kind of data may pass through an object and circumventing type checking altogether, can be found in Colin Moock's ActionScript: The Definitive Guide (O'Reilly).




Flash 8(c) Projects for Learning Animation and Interactivity
Flash 8: Projects for Learning Animation and Interactivity (OReilly Digital Studio)
ISBN: 0596102232
EAN: 2147483647
Year: 2006
Pages: 117

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net