Hmmm... You got me.

This is what I came up with...

package assets.sounds
{
     import flash.media.Sound;
     import flash.media.SoundChannel;
     import flash.net.URLRequest;

     public class SoundManager
     {
         private var sound:Sound;
         private var channel:SoundChannel;

         public function playSound(sSoundName:String):void
         {
             if(channel != null)
             {
                 channel.stop();
             }
             sound = new Sound();
             var req:URLRequest = new URLRequest("assets/sounds/" +
sSoundName + ".mp3");
             sound.load(req);
             channel = sound.play();
         }//playSound
     }//class SoundManager
}//package assets.sounds

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="horizontal"
     creationComplete="onCreationComplete()">
     <mx:Script>
         <![CDATA[
             import assets.sounds.SoundManager;

             private var soundManager:SoundManager

             private function onCreationComplete():void
             {
                 soundManager = new SoundManager();
             }
         ]]>
     </mx:Script>
     <mx:Button label="Play Beep-1"
click="soundManager.playSound('beep-1')"/>
     <mx:Button label="Play Beep-2"
click="soundManager.playSound('beep-2')"/>
     <mx:Button label="Play Beep-3"
click="soundManager.playSound('beep-3')"/>
</mx:Application>


--- In [email protected], "Tracy Spratt" <tr...@...> wrote:
>
> I am trying to make a sound manager class that will simplify playing a
sound by using an id string.  I have embedded the sound files, but am
havign trouble getting the Sound object initialized. I get an error,
"Variable beep1 is not defined" though cllearly it is.
>
> I suspect I am misusing getDefinitionByName.  If there is an
easier/better way, I am open to that as well.  The full class code is
below.
>
> Tracy
>
> package assets.sounds
> {
>   import flash.utils.getDefinitionByName;
>   import flash.media.Sound;
>
>   public class SoundManager
>   {
>     [Embed(source="beep-1.mp3")]private var beep1:Class;
>     [Embed(source="beep-2.mp3")]private var beep2:Class;
>     [Embed(source="beep-3.mp3")]private var beep3:Class;
>
>
>     public function playSound(sSoundName:String):void
>     {
>       var classRef:Class = getDefinitionByName(sSoundName) as Class; 
//Variable beep1 is not defined.
>       var s:Sound = new classRef as Sound;
>       s.play();
>     }//playSound
>   }//class SoundManager
> }//package assets.sounds
>

Reply via email to