So this is my 2nd ever class I have written. I started with a basic class that loads an external image to the stage of my flash, and shows a preloader while doing so. Im trying to take it a step further now by loading a sound then playing the image. I have a button on my stage with the instance name soundBtn. I applied this class in the linkage of that button, not sure if thats the correct way to do this or not.
Please let me know what Im doing wrong, I can get the sound to load up fine, but cant figure out the whole play the sound part of it.
package {
import flash.display.*;
import flash.text.*;
import flash.net.URLRequest;
import flash.events.*;
import flash.media.*;
public class SoundPlayer extends Sprite {
public var channel:SoundChannel;
public var sndSrc:String;
public var soundBtn:MovieClip;
public function SoundPlayer(sndSrc) {
//create sound clip and add to display list
var clip:Sound = new Sound();
//event listeners
clip.addEventListener(Event.OPEN, handleOpen);
clip.addEventListener(ProgressEvent.PROGRESS, handleProgress);
clip.addEventListener(Event.COMPLETE, handleComplete);
//Load the sound file
clip.load(new URLRequest(sndSrc))
}
private function handleOpen (event:Event):void {
trace("open");
}
private function handleProgress (event:ProgressEvent):void {
var percent:Number = Math.round (event.bytesLoaded/event.bytesTotal * 100);
trace(percent);
}
public function handleComplete (event:Event):void {
trace("complete");
}
soundBtn.addEventListener(MouseEvent.CLICK, playSound);
soundBtn.buttonMode = true;
public function playSound (event:MouseEvent):void {
channel = clip.play();
}
}
}