Groups | Blog | Home
all groups > flash actionscript > august 2006 >

flash actionscript : Playing a sound


CogDev
8/4/2006 6:46:03 PM
I have a class file used for managing sounds, and it's method
playSound(Boolean) gets called from another .as file.

I have included trace statements for the name of the file from the library,
and the duration of the sound that gets selected.

At runtime, the appropriate string for the name of the sound gets played, but
the duration and the name of the sound return "undefined".

All of the sounds are in the library of the .fla file, and I have checked the
box in the linkage properties for "export for actionscript" I have tried
deleting the ".wav" in that box, as well as including "Sound" for the class,
and neither worked.

Here is my code:

class Narrator {
private var promptCount:Number;
private var choose1:Sound;
private var chooseVanilla:Sound;
private var choose2:Sound;
private var chooseChocolate:Sound;
private var choose3:Sound;
private var chooseButterscotch:Sound;
private var testNumber:Boolean;
private var chooseSounds;
private var useSounds;

public function Narrator(testNumber){
promptCount = 4;


trace("test number is "+testNumber);

var testSounds = ["RL TC Prompt 1 take 2",
"RL TC Prompt 2",
"RL TC Prompt 3",
"RL TC Prompt4 take 1"]

var controlSounds = ["RL CC Prompt 1 take 1",
"RL CC Prompt 2 take 1",
"RL CC Prompt 3 take 1",
"RL CC Prompt 4 take 1"]

var testChoices = ["RL TC Choose 1 take 3",
"RL TC Choose 2 take 3",
"RL TC Choose 3 take 1"]

var controlChoices = ["RL CC Choose Chocolate take 1",
"RL CC Choose Vanilla take 1",
"RL CC Choose Butterscotch take 1"]

if(testNumber == true){ //decide which set of sounds to use
useSounds = testSounds;
chooseSounds = testChoices;
}else{
useSounds = controlSounds;
chooseSounds = controlChoices;
}


}

public function playSound(input:String){ // this chooses the appropriate
sound from the set
var currentSound:Sound;
switch(input){

case "oneChip":
case "chocolate":
trace("attaching "+chooseSounds[0]);
currentSound.attachSound(chooseSounds[0]);
trace("duration "+currentSound.duration);
break;

case "twoChips":
case "vanilla":
trace("attaching "+chooseSounds[1]);
currentSound.attachSound(chooseSounds[1]);
trace("duration "+currentSound.duration);
break;

case "threeChips":
case "butterscotch":
trace("attaching "+chooseSounds[2]);
currentSound.attachSound(chooseSounds[2]);
trace("duration "+currentSound.duration);
break;

case "prompt":
var r = random(promptCount);
trace(r);
trace("attaching "+useSounds[r]);
currentSound.attachSound(useSounds[r]);
trace("duration "+currentSound.duration);
break;

default:
trace("Invalid sound chosen");
break;
}

//_global.ready = false
//_root.attachAudio(currentSound);
currentSound.start(0,0);
//trace(currentSound.duration)
trace("playing "+currentSound);
currentSound.onSoundComplete = function(){
//_global.ready = true;
currentSound.stop();
delete this.onSoundComplete;
}
}
}
CogDev
8/5/2006 12:23:01 AM
CogDev
8/7/2006 4:29:43 PM
AddThis Social Bookmark Button