Can anyone help me with this one, as I have been banging my head against a brick wall for sometime now, and can seem to find no solution.
As you can see from the attached file, [which is a simplification of my project, containing only the relevant portions], I have a MovieClip on stage, with instance name tuneSelector_mc. Below is the code controlling its actions:
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Loader;
import flash.media.Sound;
import flash.media.SoundChannel;
//instance variables
var tuneSelector_mc:MovieClip;
var dialRotation:Number = 0;
var snd:Sound;
var channel:SoundChannel;
var currSong:String;
//button mode statement
tuneSelector_mc.buttonMode = true;
//array containing song details
var songList:Array = new Array("birds.mp3","summer.mp3", "moon.mp3", "flyswatter.mp3", "scene.mp3", "lemsip.mp3");
//Event Listeners for onstage button
tuneSelector_mc.addEventListener(MouseEvent.CLICK, chooseSong);
tuneSelector_mc.addEventListener(MouseEvent.CLICK,turnKnob);
// Turn the selector switch;
function turnKnob(event:MouseEvent):void{
while(tuneSelector_mc.rotation >=287){
tuneSelector_mc.rotation = 0;
}
tuneSelector_mc.rotation += 36;
trace("turnKnob function rotates tuneSelector_mc " + tuneSelector_mc.rotation + " degrees");
}
//choose the tune
function chooseSong(event:MouseEvent):void{
dialRotation = tuneSelector_mc.rotation;
switch(event.currentTarget.name){
case "dialRotation == 36":
currSong = "../audio/" + songList[0] as String;
trace("switch statement 1 " + tuneSelector_mc.rotation + " degrees");
break;
case "dialRotation == 72":
currSong = "../audio/" + songList[1] as String;
trace("switch statement 2 " + tuneSelector_mc.rotation + " degrees");
break;
case "dialRotation == 108":
currSong = "../audio/" + songList[2] as String;
trace("switch statement 3 " + tuneSelector_mc.rotation + " degrees");
break;
case "dialRotation == 144":
currSong = "../audio/" + songList[3] as String;
trace("switch statement 4 " + tuneSelector_mc.rotation + " degrees");
break;
case "dialRotation == 180":
currSong = "../audio/" + songList[4] as String;
trace("switch statement 5 " + tuneSelector_mc.rotation + " degrees");
break;
case "dialRotation == 216":
currSong = "../audio/" + songList[5] as String;
trace("switch statement 6 " + tuneSelector_mc.rotation + " degrees");
break;
}
if(snd !=null){
channel.stop();
}
snd = new Sound();
channel = new SoundChannel();
snd.load(URLRequest(currSong));
channel = snd.play();
}
At runtime it throws the following Error.
ArgumentError: Error #2068: Invalid sound.
at flash.media::Sound/play()
at test_AS3_fla::MainTimeline/chooseSong()[test_AS3_fla.MainTimeline::frame1:73]
turnKnob function rotates tuneSelector_mc 36 degrees
The code above is a rewrite attempt at getting this to work. In a previous attempt I approached the problem as per below, using the same Event Listeners, and function turnKnob, only the listener called playSongs() as below, and each song used a separate variable for the Sound class.
This worked, in as much as music played, but obviously without any soundChannel, with each click the next song would load and play, while the previous one was still playing. When I tried adding a SoundChannel, and using the if(snd!=null){channel.stop} statement, it threw errors, playing the first song fine, then saying that “functions had been called out of sequence, or the previous call was unsuccessful”. If I tweaked the code to use one var snd:Sound for all songs, I would get a Null Object reference. The code below is the original bare-bones structure, without any soundChannel, which plays sounds, albeit overlapping ones!
var snd1:Sound = new Sound();
var snd2:Sound = new Sound();
var snd3:Sound = new Sound();
var snd4:Sound = new Sound();
function playSongs():void{
while(tuneSelector_mc.rotation >=287){
tuneSelector_mc.rotation = 0;
}
if(tuneSelector_mc.rotation ==36){
snd1.load(new URLRequest("Moon.mp3"));
snd1.addEventListener(Event.COMPLETE, playSong1);
function playSong1(event:Event):void{
snd1.play();
}
}else if(tuneSelector_mc.rotation ==72){
snd2.load(new URLRequest("Birds.mp3"));
snd2.addEventListener(Event.COMPLETE, playSong2);
function playSong2(event:Event):void{
snd2.play();
}else if(tuneSelector_mc.rotation ==144){
snd3.load(new URLRequest("Taxman.mp3"));
snd3.addEventListener(Event.COMPLETE, playSong3);
function playSong3(event:Event):void{
snd3.play();
}else if(tuneSelector_mc.rotation ==180){
snd4.load(new URLRequest("Susan's House.mp3"));
snd4.addEventListener(Event.COMPLETE, playSong4);
function playSong4(event:Event):void{
snd4.play();
}else{
return;
}
}
If anyone can please explain where I am going wrong, or how I should pass the rotation of tuneSelector_mc into the switch statement, or a completely different approach to solving this, then please help!