Developing Android Applications with Adobe AIR [65]
In this example, the sound starts at the three-second position and loops five times:
sound.play(3000, 5);
When it loops again, it starts at the same position, here the third second.
The Sound class does not dispatch an event when it is done playing. The SoundChannel class is used for that purpose, as well as for controlling sound properties such as volume and to stop playback. Create it when a Sound object starts playing. Each sound has its own channel:
import flash.media.SoundChannel;
var sound = new Sound();
sound.addEventListener(Event.COMPLETE, onLoaded);
sound.load(new URLRequest("mySound.mp3"));
function onLoaded(event:Event):void {
sound.removeEventListener(Event.COMPLETE, onLoaded);
var channel:SoundChannel = sound.play();
channel.addEventListener(Event.SOUND_COMPLETE, playComplete);
}
function playComplete(event:Event):void {
event.target.removeEventListener(Event.SOUND_COMPLETE, playComplete);
trace("sound done playing");
}
Displaying Progress
There is no direct way to see playback progress, but you can build a timer to regularly display the channel position in relation to the length of the sound. The sound needs to be fully loaded to acquire its length:
import flash.utils.Timer;
import flash.events.TimerEvent;
var channel:SoundChannel;
var sound:Sound;
// load sound
// on sound loaded
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, showProgress);
channel = sound.play();
channel.addEventListener(Event.SOUND_COMPLETE, playComplete);
timer.start();
function showProgress(event:TimerEvent):void {
// show progress as a percentage
var progress:int = Math.round(channel.position/sound.length*100);
}
Do not forget to stop the timer when the sound has finished playing:
function playComplete(event:Event):void {
channel.removeEventListener(Event.SOUND_COMPLETE, playComplete);
timer.removeEventListener(TimerEvent.TIMER, showProgress);
}
You do not know the length of a streaming audio file until it is fully loaded. You can, however, estimate the length and adjust it as it progresses:
function showProgress(event:TimerEvent):void {
var percentage:int = sound.bytesLoaded/sound.bytesTotal;
var estimate:int = Math.ceil(sound.length/percentage);
// show progress as a percentage
var progress:int = (channel.position/estimate)*100;
trace(progress);
}
Stopping Sounds
To stop a sound from playing, simply call the stop function on its channel:
channel.stop();
To stop streaming audio, calling the stop function works once, but the sound starts playing again from the beginning. This is because the stream is still downloading. You also need to stop the downloading process:
channel.stop();
sound.close();
If you have several sounds playing at the same time, you can stop the sounds of all the channels one at a time.
The best approach is to use the static SoundMixer class, which controls embedded and dynamic sounds and the mixed output from multiple sound channels. You can use this class to stop as well as control the volume of all the sounds currently playing:
SoundMixer.stopAll();
Resuming Sounds
There is no direct way to pause a sound and then resume playing, but you can easily create the same effect by storing the position of the channel where you stop, and start from that same position when playing again:
var lastPosition:int = channel.position;
channel.stop();
// to resume from the same position
sound.play(lastPosition);
Accessing Metadata
When playing AAC files, you have access to metadata information that can be useful for your application. In this example, we are tracing the duration and the codec:
import flash.net.NetConnection;
import flash.net.NetStream;
var connection:NetConnection;
connection = new NetConnection();
connection.connect(null);
var stream:NetStream = new NetStream(connection);