Chapter 16 - XMLClass.swf
This example loads a track list from a file called, soundtrack.xml. When the Play button is clicked, a random track is selected and played. The Stop button stops the playback.
SoundtrackController class:
import SoundClass.AudioController;
import XMLClass.SoundtrackXML;
class XMLClass.SoundtrackController extends AudioController {
public var xmlDoc:SoundtrackXML;
public var xmlFile:String;
public var phases:Object;
public var activePhase:String;
public function SoundtrackController(container:Object, xml_file:String) {
super(container);
xmlFile = xml_file;
phases = new Object();
xmlDoc = new SoundtrackXML(this);
xmlDoc.ignoreWhite = true;
loadXML();
}
public function loadXML() {
xmlDoc.onLoad = function(success:Boolean) {
this.owner.parseXML();
}
xmlDoc.load(xmlFile);
}
public function parseXML() {
for (var prop:String in xmlDoc.firstChild.childNodes) {
trace(prop + ' ' + xmlDoc.firstChild.childNodes[prop].nodeName);
if (xmlDoc.firstChild.childNodes[prop].nodeName == "phase") {
parsePhase(xmlDoc.firstChild.childNodes[prop]);
}
}
}
public function parsePhase(node:XMLNode) {
for (var prop:String in node.childNodes) {
trace(" " + prop + ' ' + node.childNodes[prop].nodeName + ", " + node.attributes["id"]);
if (node.childNodes[prop].nodeName == "track") {
parseTrack(node.childNodes[prop], node.attributes["id"]);
}
}
}
public function parseTrack(node:XMLNode, phase:String) {
trace(" " + node.firstChild.nodeValue);
if (phases[phase] == undefined) {
phases[phase] = new Array();
}
phases[phase].push(node.firstChild.nodeValue);
}
public function playTrack(phase:String, loop:Boolean) {
var randomIndex = Math.floor(Math.random() * phases[phase].length);
stopSoundNow(phase);
activePhase = phases[phase][randomIndex];
loadSound(phase, activePhase, false, true);
playSound(phase, loop);
}
}
AudioController class:
import SoundClass.GameSound;
import ch7.GameDepthManager;
class SoundClass.AudioController {
var soundClip:MovieClip;
var soundList:Object;
var soundClipContainer:Object;
public function AudioController(container:Object) {
soundClipContainer = container;
var depth:Number = GameDepthManager.getNextAudioClipDepth();
var name:String = "sound_clip_" + depth;
soundClip = soundClipContainer.createEmptyMovieClip(name, depth);
soundList = new Object();
}
public function registerSound(sound_id:String):Void {
soundList[sound_id] = new GameSound(soundClip, this, sound_id);
soundList[sound_id].attachSound(sound_id);
}
public function loadSound( sound_id:String, sound_url:String,
is_streaming:Boolean,
play_when_loaded:Boolean):GameSound {
var thisSound:GameSound;
thisSound = new GameSound( soundClip, this, sound_id, sound_url,
is_streaming, play_when_loaded);
soundList[sound_id] = thisSound;
thisSound.loadSound(sound_url, is_streaming);
thisSound.onLoad = function() {
var tempSound:GameSound = GameSound(this);
tempSound.stop();
tempSound.AC.onSoundLoaded(tempSound.soundID);
}
thisSound.onID3 = function() {
var tempSound:GameSound = GameSound(this);
tempSound.AC.onID3Detected(this.soundID);
}
return thisSound
}
public function onSoundLoaded(sound_id:String):Void {
var thisSound:GameSound = soundList[sound_id];
if (thisSound.playWhenLoaded) thisSound.start();
}
public function onID3Detected(sound_id:String):Void {
var thisSound:GameSound = soundList[sound_id];
trace("onID3Detected: soundID: " + sound_id);
for (var prop in thisSound.id3) {
trace(prop + ": " + thisSound.id3[prop]);
}
}
public function playSound(sound_id:String, loop:Boolean):Void {
var thisSound:GameSound = soundList[sound_id];
//first stop all sounds managed by this controller
for (var prop in soundList) {
stopSoundNow(prop);
}
if (loop) {
thisSound.onSoundComplete = function() {
var tempSound:GameSound = GameSound(this);
trace("SoundInstance: looping: " + this);
tempSound.start();
}
} else {
thisSound.onSoundComplete = null;
}
thisSound.start();
}
public function stopSoundNow(sound_id:String):Void {
var thisSound:GameSound = GameSound(soundList[sound_id]);
thisSound.onSoundComplete = null;
thisSound.stop();
}
public function stopSoundAtEndOfLoop(sound_id:String):Void {
var thisSound:GameSound = GameSound(soundList[sound_id]);
if (thisSound.onSoundComplete != undefined) {
thisSound.onSoundComplete = undefined;
} else {
thisSound.stop();
}
}
public function setSoundVolume(sound_id:String, volume:Number):Void {
var thisSound:GameSound = GameSound(soundList[sound_id]);
thisSound.setVolume(volume);
}
}
SoundtrackXML class:
class XMLClass.SoundtrackXML extends XML {
public var owner:Object;
public function SoundtrackXML(owner:Object) {
super();
this.owner = owner;
}
}
GameSound class:
import SoundClass.AudioController;
class SoundClass.GameSound extends Sound {
var soundClip:MovieClip;
var AC:AudioController;
var soundID:String;
var soundURL:String;
var isStreaming:Boolean;
var playWhenLoaded:Boolean;
public function GameSound(clip:MovieClip,
audio_controller:AudioController,
id:String, url:String,
is_streaming:Boolean,
play_when_loaded:Boolean) {
super(clip);
soundClip = clip;
AC = audio_controller;
soundID = id;
soundURL = url;
isStreaming = is_streaming;
playWhenLoaded = play_when_loaded;
}
}

