logo
468x60-2-495


  • Home
  • Privacy Policy
  • About
search
top
Jun 10, 2011 Posted on Jun 10, 2011 in Hints and Tips | 0 comments

Quick Tip: Play External WAV Files in AS3

By default, Flash can play WAV files when they’re located in the Library, but not when loading external files. Read through this Quick Tip to learn how to play them.


Step 1: Brief Overview

play wav files in Flash with AS3

We’ll use common methods to request and load the desired WAV file, and use a fantastic third party class to play the loaded file.


Step 2: AS3WavSound

play wav files in Flash with AS3

AS3WavSound (AWS) is an excellent ActionScript class that can play externally loaded wave files, go to its Google Code page and get the source code.

Now this class wasn’t exactly created for external wav playback; here is a short description from the author:

The Flex SDK does not natively support playing (embedded) .wav files. Thus far developers worked around this using ugly hacks (generating swf bytedata to trick the Flash Player). Not anymore. AWS in the slimmest sense simply is a single as3 class. It extends the generic Sound class in Flash and adds support for playing back WAVE data. You don’t need this sound class if you are working with the Flash IDE or Flex Builder, as they convert .wav data directly to Sound objects. The open source SDK compiler however, does not support this feature. But it does now!

The Flash IDE does indeed convert the wav data to Sound objects, but only for embedded files (imported to the Library); if you want to play an external wav file, use the following method…


Step 3: Usage

Prepare a new ActionScript class and write the following code:

package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.utils.ByteArray;
	import flash.media.Sound;
	import org.as3wavsound.WavSound;
	import org.as3wavsound.WavSoundChannel;

	public final class Main extends Sprite
	{
		public final function Main():void
		{
			playB.addEventListener(MouseEvent.MOUSE_UP, loadWav);
		}

		private final function loadWav(e:MouseEvent):void
		{
			var urlRequest:URLRequest = new URLRequest('Phone.wav');
			var wav:URLLoader = new URLLoader();
			wav.dataFormat = 'binary';
			wav.load(urlRequest);
			wav.addEventListener(Event.COMPLETE, playWav);
		}

		private final function playWav(e:Event):void
		{
			var tts:WavSound = new WavSound(e.target.data as ByteArray);
			tts.play();
		}
	}
}

This is basic code for loading an external file in AS3, the URLLoader class uses the URLRequest to determine the location of the file to be loaded (which is loaded as binary data) and when finished, the WavSound class is used to play the sound.


Step 4: Example

Check out the demo for a working example using the code.


Step 5: About WAV Files

Keep in mind that this class won’t play every wave sound: the file must have a sample rate of 44100, 22050, or 11025 Hz, and bitrate sample data of 8 or 16.


Conclusion

Use this class to play your external WAV files.

I hope you liked this tutorial, thank you for reading!



View full post on Activetuts+

banner ad

Leave a Reply

Click here to cancel reply.

search search search search search
Find an Article
Categories
  • Flash Video Training
  • Hints and Tips
  • Recommended
Please Support Our Sponsors
Recent Posts
  • Workshop Coding Challenge: Fix This Breakout Game
  • Enable the Latest AIR SDK in Flash Professional CS5.5+
  • Quick Tip: Versioning Your Files With Dropbox (via Webdesigntuts+)
  • Workshop: Nuclear Outrun – Critique
  • Understanding Variables, Arrays, Loops, and Null: The Post-it Note Analogy
Tag Cloud
2011 ActionScript Active Activetuts+ Adobe animation Basic Basix Best Build Button Character Create Creating Critique Custom design Effect Effects Files Flash from Game Guide HTML5 Introduction Macromedia Motion Muzzle part Player Premium Professional Quick Silverlight Simple Text Tool Tutorial Tuts+ Tween Using Video website Workshop
About Our Site:

Hey there and welcome to "Flash Video Training Source", a resource for anybody interested in learning more about Adobe's great tool. We feature educational videos, which will help you master Adobe Flash and help you get to know all of its features. We at "Flash Video Training Source" believe that video training and video... more

Why don't you follow us on Twitter and get the latest video tutorials twitted to your account. Just click on the floating twitter bar to your right!

Go Back In Time
May 2012
M T W T F S S
« Apr    
 123456
78910111213
14151617181920
21222324252627
28293031  
Pretty Blank Box
top

Blogroll

  • Development Blog
  • Documentation
  • Plugins
  • Suggest Ideas
  • Support Forum
  • Themes
  • WordPress Planet

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Archives

  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
Powered by WordPress  |  Designed by Elegant Themes  |  Lightning Fast Hosting by Site 5 Hosting