Array

Working with Sounds, Loading Sounds

Must Read

Short-term Approach for Signal Representation and Compression

In summary, stereoscopic 3D will be used in the short term. Broadcasters appear to be rallying around top/bottom spatial...

How to Spot a Real Black Friday Bargain

After searching your ads on Thanksgiving Day, you may be left with a few items that you think are...

Which Loan is Best For You-A Home Equity Loan or a Home Equity Line of Credit?

If you don't know the difference between a home equity loan and a home equity line of credit, here's...

Now that your sound files are ready, let’s see how we can use them. All sound-related classes belong to the flash.media package and will be introduced throughout this chapter.

Loading Sounds

The Sound class gets access to the audio information to load the sound file. It is a subclass of the EventDispatcher class.

As discussed before, your sound can be embedded, it can be loaded as an external file from your application assets directory, or it can be downloaded from a remote server. For the latter, advise your audience to use WiFi over 3G for a better experience.

If you try to play a sound that is not loaded, you will get a runtime error. Create a listener to be notified when the loading is complete, and then play your file:

import flash.media.Sound;
import flash.net.URLRequest;
import flash.events.Event;
var sound:Sound = new Sound();
sound.addEventListener(Event.COMPLETE, onLoaded);
var request:URLRequest = new URLRequest(“mySound.mp3”);
sound.load(request);
// sound fully loaded
function onLoaded(event:Event):void {
sound.removeEventListener(Event.COMPLETE, onLoaded);
sound.play();
}

You can inform the user that the file has started to load:

sound.addEventListener(Event.OPEN, onOpen);
function onOpen(event:Event):void {
trace(“sound loading”);
}

If it is a large file, create a listener to display the progress:

import flash.events.ProgressEvent;
sound.addEventListener(ProgressEvent.PROGRESS, onLoading);
function onLoading(event:ProgressEvent):void {
// display the percentage loaded
trace(event.bytesLoaded/event.bytesTotal)*100);
}

On Android devices, it is important to check for errors, particularly if the file is served from a remote server. Inform your user if there is a network issue:

import flash.events.IOErrorEvent;
sound.addEventListener(IOErrorEvent.IO_ERROR, onError);
function onError(event:IOErrorEvent):void {
trace(“sound cannot be loaded”, event.text);
}

Streaming

Streaming is the process of playing part of a sound file while other parts are loading in the background. The advantage is that you don’t need to wait for the whole file to download before playing. In addition, you can play very long tracks without memory constraints.

The audio files must be located on a streaming server. The quality of the server is an important factor to a good experience: 128 kbps is sufficient for audio, but a musician can detect artifacts in high frequency for MP3 encoding. Encoding your audio at 192 kbps is a good compromise.

Requesting the file is the same process as before.

You can start playing the audio as soon as there is enough data in the buffer. Buffering is the process of receiving and storing audio data before it is played. The default buffer time is 1,000 milliseconds. You can overwrite the default using the SoundLoaderCon text class.

In this example, the buffer time is changed to five seconds:

import flash.media.SoundLoaderContext;
var sound:Sound = new Sound();
var request:URLRequest = new URLRequest(“myStreamingSound.mp3”);
var context:SoundLoaderContext = new SoundLoaderContext(5000, true);
sound.load(request, context);
sound.play();

The SoundLoaderContext class is also used for security checks when loading sounds, but it may not be required in AIR.

Streaming MP3 files is buggy when it comes to midstream bit rate changes, a method often used by streaming services such as Internet radios. The audio sounds like it speeds up or is broken in chunks because it uses the bit rate declared at the start of the stream, even after a change.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest News

Underlight As Accent, For Power and The Main Light for Photography

Underlighting, in which fill or accent light comes from under the topic, is not widely used technique in the traditional Portraits,...

How To Fix Overexposure As A Creative Tool, The Complete Guide

As an creative tool, overexposure is sort of underrated. What I’m close to propose could be a deliberate and well thought out technique for...

7D Mark II is Canon’s best DSLR cameras without full-frame sensor.

The expected long-awaited Canon EOS 7D Mark II are shipping in November for $ 1,799 without a lens. With a higher price tag, you...

Low key photography and How to isolate your subject.

Low key photography doesn’t depend on underexposure to make its point; the key to low key is that the majority of tones, even correctly...

High Key Lighting Techniques for Professional Photographers

I’ve written many times about high key lighting techniques and how to achieve them. The term “high key” is a bit misleading. As I’ve...

More Articles Like This

blograby facebook like page