The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area. This book explains the process of working with PHP in Flash, while creating real world examples that you can actually learn something from.
The Flash and PHP Bible has a dedicated forum for support and comments.
Learn how to write a custom class to play portions of a single media file rather than loading an maintaining multiple files.
View an Example of this article before you get started.
As you start to develop applications more often you learn ways to save time. One common way to work with sound is to load in many clips and use whatever one you need at the time, or import them into the library during the authoring process. Both of these approaches work in most case, but do have limitations. For instance, loading the sound into the library means you can't update the sounds without republishing the entire movie. The second option, loading in the sounds at runtime works better but means some sort of loading manager is needed and takes longer.
Of course I wouldn't have written an article if there was only two ways to work with sound in ActionScript, because you probably already knew about those.
The third option for working with sound it to load in one sound file (mp3) and only play a portion of it, depending on which sound you want to play. As you noticed in the attached example, I have loaded a family guy sample where each button plays a certain portion of the sample. The idea of this approach is to minimize the loading process and make it easier to update.
For determing the specific point to play the file from I recommend using Soundbooth or a similar sound editor. In sounbooth it clearly displays the current time which makes it very easy to work with

Now that the background of this article is established, let's start by looking at how the custom class will be used and then move on to building the class. The first part of the code is your typical class import, which in this case is loading in the custom class. Then a variable is set to hold the music file that will be loaded. An event listener is used to give the button the click ability which plays the sound.
The special method that is found within the CLICK event handler is passing along the start and end positions which determines which sound to play.
import SoundSnippet;
var url:String = "stewies_new_laws.mp3";
var soundSnippet1:SoundSnippet = new SoundSnippet(url);
sample1MC.addEventListener(MouseEvent.CLICK, playSound1);
function playSound1(e:MouseEvent):void
{
soundSnippet1.playSound(0, 700);
}
That is the complete block of code for using the custom Sound class, the next step is to write that class.
The SoundSnippet class is responsible for loading the sound at startup, playing a certain portion and keeping track of the timers to make all this happen. Here is a class skeleton which will give you a better idea of how the class is structured.
package
{
import flash.display.Sprite;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.utils.Timer;
public class SoundSnippet
{
...
function SoundSnippet(url:String) {}
public function playSound(start:uint=0, end:uint=0):void {}
private function timerHandler(event:TimerEvent):void {}
private function soundComplete():void {}
private function ioErrorHandler(event:Event):void {}
private function progressHandler(event:ProgressEvent):void {}
private function enableTimer():void {}
}
}
The first part is responsible for importing the required classes in order to load and play a sound. Then the other methods are used to handle the loading process and keep track of the timers to ensure the sound stops at the correct point.
The constructor function is used to load the sound and assign the proper event handlers. The url to load is passed in as an argument which is used to assign a brand new URLRequest instance.
function SoundSnippet(url:String)
{
soundFactory = new Sound();
soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler);
var request:URLRequest = new URLRequest(url);
soundFactory.load(request);
}
The next method to focus on is the playSound, this method is called when you want to start the sound. The start and ending points are passed in as separate arguments. If the end point is not passed in the sound clip is played to the end by pulling in the length of the sound file. The last step of this method is to make a call to enableTimer which initializes a timer.
public function playSound(start:uint=0, end:uint=0):void
{
song = soundFactory.play(start);
if(end == 0)
{
endPoint = soundFactory.length;
}
else
{
endPoint = end;
}
enableTimer();
}
The next method, enableTimer is called within the playSound method to initialize the Timer events.
private function enableTimer():void
{
timer = new Timer(20);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
The last of the special methods would be timerHandler which looks at the current duration and checks to make sure it is less than the ending point.
function timerHandler(event:TimerEvent):void
{
if(uint(song.position.toFixed(2)) >= endPoint)
{
timer.stop();
song.stop();
soundComplete();
return;
}
}
The other three methods are used to handle the loading and playing events.
private function soundComplete():void
{
trace("Snippet finished playing");
}
private function ioErrorHandler(event:Event):void
{
trace("ioErrorHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void
{
trace("progressHandler: " + event);
}
You should now have all of the class completed and can test out the example. Make sure you have saved the MP3, class and FLA all in the same directory or edited the paths if you moved things around.
Here is the completed class for easy copy and pasting. Simply save the file as SoundSnippet.as in the same directory as the sound and Flash file (FLA).
package
{
import flash.display.Sprite;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.utils.Timer;
public class SoundSnippet
{
private var song:SoundChannel;
private var timer:Timer;
private var soundFactory:Sound;
private var endPoint:uint;
function SoundSnippet(url:String)
{
soundFactory = new Sound();
soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler);
var request:URLRequest = new URLRequest(url);
soundFactory.load(request);
}
public function playSound(start:uint=0, end:uint=0):void
{
song = soundFactory.play(start);
if(end == 0)
{
endPoint = soundFactory.length;
}
else
{
endPoint = end;
}
enableTimer();
}
private function enableTimer():void
{
timer = new Timer(20);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
function timerHandler(event:TimerEvent):void
{
if(uint(song.position.toFixed(2)) >= endPoint)
{
timer.stop();
song.stop();
soundComplete();
return;
}
}
private function soundComplete():void
{
trace("Snippet finished playing");
}
private function ioErrorHandler(event:Event):void
{
trace("ioErrorHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void
{
trace("progressHandler: " + event);
}
}
}
If you have questions or comments please feel free to post them in the comment form below.
|
megat afif Wed Mar 19, 2008 3:24 am
i'm a newbie. could you write this code in full php script as example..thank
|
|
mkeefe Wed Mar 19, 2008 7:23 am
The code is for ActionScript 3 (Flash 9) and is provided in full within the last block of code.
This really isn't a task for PHP as it runs on your server and not in the users browser. |
|
tom Sun May 25, 2008 12:01 am
Thanks for the excellent class. I modified it slightly by making the sound loop...
|
|
Wolfram Mon Oct 6, 2008 10:45 am
Hey,
thanks for this code, I've used it in the prototype app for my diploma thesis. Can you somehow post me your name to give reference to you? |
|
mkeefe Mon Oct 6, 2008 11:52 am
@ Wolfram - Glad to hear it was useful for you, and on something so important!.
Sure, my "title" is: "Matthew Keefe, Founder, mkeefeDESIGN" |
©2004 - 2008 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN