3 Replies Latest reply on Jan 18, 2011 11:07 PM by Wrataxas

    basic sound loading question

    jonk88

      I have created a button that loads and plays a sound from a url. Now I would like to create a second button that plays the same sound without loading it again. What is the best way to do this?

        • 1. Re: basic sound loading question
          Subeesh Arakkan Level 4

          when you load the sound, assign it to a sound variable and use it. Check the following link for reference.

           

          http://livedocs.adobe.com/flex/3/html/help.html?content=Working_with_Sound_01.html

          • 2. Re: basic sound loading question
            jonk88 Level 1

            I guess my question wasnt clear. I need to be able to play a loaded sounds with multiple functions. Basically like a global variable. My question really is how can I pass my loaded sounds from loadsounds("url") to playsound1(), playsound2(), etc..

             

            I am really new to flex so maybe this seems too obvious, but I dont know the best way to do it.

             

            Something like this...

             

             

                 <fx:Script>
                      <![CDATA[
                           import flash.events.*;
                           import flash.events.Event;
                           import flash.events.TimerEvent;
                           import flash.media.Sound;
                           import flash.media.SoundChannel;
                           import flash.net.URLRequest;
                           import flash.utils.Timer;
                           
                           
                           public function loadsound(url:String){
                                var request:URLRequest = new URLRequest(url);
                                var soundFactory:Sound = new Sound();
                                soundFactory.addEventListener(Event.COMPLETE, completeHandler);
                                soundFactory.addEventListener(Event.ID3, id3Handler);
                                soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                                soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                                soundFactory.load(request);
                                
                                
                           }
                           
                           public function playsound(){
                                soundFactory.play();
                           }
            
            • 3. Re: basic sound loading question
              Wrataxas Level 2

              Here's a super simplified version of the way I dealt with sounds in a project.  Maybe part of this will be useful to you.

               

              package components
              {
                   import flash.events.Event;
                   import flash.media.Sound;
                   import flash.media.SoundChannel;
                   import flash.net.URLRequest;
                   import mx.collections.ArrayCollection;
              
              
                   public class Audio
              
                   {
                        public static const INDEX_MoneyChing:int = 0;
                        public static const INDEX_ShuffleNoise:int = 1;
                        public static const INDEX_DealCard:int = 2;
                        public static const INDEX_Cheering:int = 3;          
              
                        {
                             sounds = new ArrayCollection();
              
              
                             [Embed(source="sounds/Sounds/coin.mp3")]
                             private static var SoundMoneyChing:Class; sounds[ INDEX_MoneyChing ] = new SoundMoneyChing() as Sound;
                             [Embed(source="sounds/Sounds/shuffle.mp3")]
                             private static var SoundShuffleNoise:Class; sounds[ INDEX_ShuffleNoise ] = new SoundShuffleNoise() as Sound;
                             [Embed(source="sounds/Sounds/card2.mp3")]
                             private static var SoundDealCard:Class; sounds[ INDEX_DealCard ] = new SoundDealCard() as Sound;
                             [Embed(source="sounds/Sounds/cheering.mp3")]
                             private static var SoundCheering:Class; sounds[ INDEX_Cheering ] = new SoundCheering() as Sound;
                        }
                        
                        public static function Play( soundIndex:int ):void
                        {
                             sounds[ soundIndex ].play( 0, 0 );
                        }
              
                        protected static var sounds:ArrayCollection;
                   }
              }
              
              
              

               

              To play a sound you'd do: Audio.Play( Audio.INDEX_MoneyChing );