6 Replies Latest reply on Apr 18, 2008 2:35 AM by eXtremeData

    Delaying the start of a sound

    german01 Level 1
      I' am new to Flash ActionScript 2 and 3. I'm working on a project that involves several audio clips.
      I would like to delay the start of audio clips at least 2 seconds.
      Each sound is at the begining of a new page.
      I used the following script unsucessfully. Please help!
      The name of the sound is p32a.mp3

      Here is the script
      - - - - - - - - - - - - - -
      var playIt:Number;

      function playS(){
      mySound = new Sound();
      mySound.attachSound("p32a.mp3");
      mySound.onSoundComplete = function(){
      stopAllSounds();
      }
      mySound.start();
      clearInterval (stopS);
      stopS = setInterval(playIt, 2000);
      }
      function (playIt) {
      clearInterval(stopS);
      }
        • 1. Re: Delaying the start of a sound
          kglad Adobe Community Professional & MVP
          then put your start() method in playIt (which should not be in parantheses):


          clearInterval (stopS);
          stopS = setInterval(playIt, 2000);
          }
          function playIt() {
          mySound.start();
          clearInterval(stopS);
          }
          • 2. Re: Delaying the start of a sound
            Level 7
            german01,

            Go ahead and declare all of your variables outside of any functions.
            This allows them to be scoped to the main timeline, as opposed to playS().
            This is important, because if you declare mySound inside playS(), that
            variable (i.e., your Sound instance) is only available to that function, and
            dies as soon as the function stops executing.

            var playIt:Number;
            var mySound:Sound;

            There are plenty of times when it *is* right to declare variables inside
            functions, but this isn't one of them. That said, I'm looking to see where
            your playS() fuction even gets called. It looks like it doesn't! In your
            setInterval() call, you're using your playIt variable is a function
            reference, but that first parameter is supposed to be the function called by
            setInterval() after two seconds.

            Here's how I would write the code:

            var mySound:Sound = new Sound();
            mySound.attachSound("p32a.mp3");
            mySound.onSoundComplete = function():Void {
            stopAllSounds();
            }

            This declares a variable, mySound, and sets it to an instance of the
            Sound class. What this means is that your mySound variable now has
            available to it all the functionality defined by that class: mySound *is* a
            Sound object, which means you can invoke the attachSound() method on it, as
            you've done. After that, you've associated a function with the
            Sound.onSoundComplete event of this Sound instance. (I'm not sure why
            you're calling the stopAllSounds() function when p32a.mp3 completes -- the
            MP3 will stop on its own -- but it doesn't hurt anything.)

            Now that you're set up, you want to invoke Sound.start() no that myStart
            variable two seconds from now. Here's how to do that:

            var interval:Number = setInterval(
            function():Void {
            mySound.start();
            clearInterval(interval);
            },
            2000
            );

            setInterval() accepts at least two parameters: a) the function to trigger
            (here, an anonymous function literal) and b) an interval of time.

            setInterval(functionToTrigger, 2000);

            In this case, the function to trigger does two things: a) invokes
            Sound.start() on the Sound instance (mySound) and b) invokes clearInterval()
            in order to keep the function from repeating. The value "interval" is
            nothing more than a numeric value. The number comes from the return value
            of setInterval().

            Does that make sense? Write back if it doesn't. :)


            David Stiller
            Co-author, Foundation Flash CS3 for Designers
            http://tinyurl.com/2k29mj
            "Luck is the residue of good design."


            • 3. Re: Delaying the start of a sound
              german01 Level 1
              Kglad and David:

              Thanks a lot for taking the time to help me out. Here is the final code that worked!

              var mySound:Sound = new Sound();
              mySound.attachSound("p32a.mp3");
              var interval:Number = setInterval (
              function():Void{
              mySound.start();
              clearInterval(interval);
              },
              1000
              );
              setInterval(toTriger, 1000);


              I decided to delay the time just 1 second, so I reduced the time from 2 seconds (2,000) to 1 second (1,000). It works the way I wanted it. Thanks to both of you again.
              • 4. Re: Delaying the start of a sound
                Level 7
                german01,

                > Here is the final code that worked!

                var mySound:Sound = new Sound();
                mySound.attachSound("p32a.mp3");
                var interval:Number = setInterval(
                function():Void{
                mySound.start();
                clearInterval(interval);
                },
                1000
                );
                setInterval(toTriger, 1000);

                Drop that last line, actually. You've already set up an anonymous
                function (not named anything) that starts your sound, then clears the
                interval. Unless you have another, different function named toTriger()
                somethere, your last line doesn't do anything useful.

                If you prefer not to use an anonymous function, you could do this:

                var mySound:Sound = new Sound();
                mySound.attachSound("p32a.mp3");
                var interval:Number = setInterval(delaySound, 1000);

                function delaySound():Void {
                mySound.start();
                clearInterval(interval);
                }


                David Stiller
                Adobe Community Expert
                Dev blog, http://www.quip.net/blog/
                "Luck is the residue of good design."


                • 5. Re: Delaying the start of a sound
                  german01 Level 1
                  Thanks David:

                  I used the script you provided and made the necessary changes to the one I had tried. It worked!
                  It is shorter and cleaner.

                  Thanks again.

                  HERE IS THE CODE TO DELAY THE START OF ONE SOUND

                  var mySound:Sound = new Sound();
                  mySound.attachSound("p32a.mp3");
                  var interval:Number = setInterval(delaySound, 1000);

                  function delaySound():Void {
                  mySound.start();
                  clearInterval(interval);
                  }

                  HERE IS THE CODE TO DELAY THE FIRST SOUND, BUT NOT THE SECOND (the second plays in sequence, right after the first)

                  var mySound2:Sound = new Sound();
                  mySound2.attachSound("p32b.mp3");
                  mySound2.onSoundComplete = function(){
                  mySound3 = new Sound();
                  mySound3.attachSound("p32c.mp3");
                  mySound3.start();
                  }
                  var interval:Number = setInterval (delaySound, 1000);
                  function delaySound():Void{
                  mySound2.start();
                  clearInterval(interval);
                  }
                  • 6. Re: Delaying the start of a sound
                    eXtremeData
                    Thanks.
                    I has same problem and this code work .