Developing Android Applications with Adobe AIR [62]
var file:File = directory.resolvePath("mySound.wav");
// create an instance of the WAVWriter class and set properties
var wav:WAVWriter = new WAVWriter();
wav.numOfChannels = 1; // mono
wav.sampleBitRate = 16; // or 8
wav.samplingRate = 44100; // or 22000
// rewind to the beginning of the ByteArray
bytes.position = 0;
// create stream as conduit to copy data and write file
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
// convert byteArray to WAV format and close stream
wav.processSamples(stream, bytes, 44100, 1);
stream.close();
}
Open source libraries
The current native libraries cannot load a WAV file dynamically or encode a ByteArray as an MP3 file. As an alternative, you can try some of the available open source libraries.
For instance, Shine, written by Gabriel Bouvigné, is an Alchemy/Flash MP3 encoder (see https://github.com/kikko/Shine-MP3-Encoder-on-AS3-Alchemy and http://code.google.com/p/flash-kikko/):
import fr.kikko.lab.ShineMP3Encoder;
encoder = new ShineMP3Encoder(bytes);
encoder.addEventListener(Event.COMPLETE, onEncoding);
encoder.addEventListener(ProgressEvent.PROGRESS, onProgress);encoder.addEventListener(ErrorEvent.ERRROR, onError);
encoder.start();
file.save(mp3Encoder.mp3Data, "recording.mp3");
In addition, the following WAV decoders are also available:
AS3WavSound (http://www.ohloh.net/p/as3wavsound)
standingwave3 (http://maxl0rd.github.com/standingwave3/)
Ogg/Vorbis (http://vorbis.com/software/
Tonfall (http://code.google.com/p/tonfall/; this is also an encoder)
Saving to a remote server
If you have access to a streaming media server such as Flash Media Server, you can save and stream audio to the device. The microphone can be attached to a NetStream for uploading. Audio data can also be streamed from the server and played back using a Video object.
Two compression codecs are available:
import flash.media.soundCodec;
mic.codec = SoundCodec.NELLYMOSER; // default
mic.coder = SoundCodec.SPEEX;
If you are using this technology, urge your audience to use a WiFi connection over 3G unless they have a flat-fee data plan.
Audio Assets
As with visual assets, there are different methods for using audio assets in your application. We will go over the available options next.
Embedding Files
You can embed sounds in your application by adding them to your Flash library or your Flash Builder project. Embedded files should be small, like the ones used for sound effects or user interface audio feedback.
Your application will not display until all of its assets are loaded. Test it. If it sits on a black screen for too long, you may want to group the sounds in an external .swf file that you load as a separate process.
Using Flash Professional
Unless you place audio on the timeline, you need to give it a linkage name. Go to Library→Properties→Linkage and click on Export for ActionScript. Change the name so that it doesn’t include an extension, and add a capital letter to conform to class naming conventions. For instance, “mySound.mp3” should be “MySound”. Note that the Base class becomes flash.media.Sound:
var mySound:MySound = new MySound();
mySound.play();
Using Flash Builder
Place your audio file in your project folder. Embed it and assign it to a class so that you can create an instance of it:
import flash.media.Sound;
[Embed(source="mySound.mp3")]
public var Simple:Class;
var mySound:Sound = new Simple as Sound;
mySound.play();
Using External Files
Using external files is best for long sounds or if you want the flexibility to replace the files without recompiling your application. Please review Chapter 4 on how to package external files with your application:
import flash.media.Sound;
import flash.net.URLRequest;
var urlRequest:URLRequest = new URLRequest("mySound.mp3");
var sound:Sound = new Sound();
sound.load(urlRequest);
sound.play();
This example works for a small file, which loads quickly. We will cover how to handle larger files