6 Replies Latest reply on Jun 16, 2011 1:45 PM by kglad

    Loop sound issue

    skalamichal Level 1

      Hello everyone,

       

      I found an issue when I was creating a sound loop in Flash. I'm using similar code (here is a short version):

       

      var soundtrack:Sound = null; // it's initialized later, not shown here

      var pausePos:Number = 0;     // the time at which was the sound paused and should be restarted again

       

      // run paused sound

      function playSound()

      {

           if (soundtrack != null)

           {

                soundtrack.start(pausePos, 99999); // loop the sound

           }

      }

       

      // pause the sound and store the time

      function pauseSound()

      {

                if (soundtrack != null)

           {

                pausePos = soundtrack.position;

                soundtrack.stop();

           }

      }

       

      On the stage is an button to play/pause the sound. The issue is that when the sound is paused and then unpaused again, it loops from the stored "pausePos" time. Not just it starts for the first time at the "pausePos" time, but the loop is then from the "pausePos" to the sound end time.

       

      How can I solve this issue? The onSoundComplete handler ins't the solution, because there is then a short delay. Any ideas?

       

      Thank you,

      Michal

        • 1. Re: Loop sound issue
          kglad Adobe Community Professional & MVP

          position is in ms and start() offset parameter is in seconds.  use:

           

           

          var soundtrack:Sound = null; // it's initialized later, not shown here

          var pausePos:Number = 0;     // the time at which was the sound paused and should be restarted again

           

          // run paused sound

          function playSound()

          {

               if (soundtrack != null)

               {

                    soundtrack.start(pausePos/1000, 99999); // loop the sound

               }

          }

           

          // pause the sound and store the time

          function pauseSound()

          {

                    if (soundtrack != null)

               {

                    pausePos = soundtrack.position;

                    soundtrack.stop();

               }

          }

           



          1 person found this helpful
          • 2. Re: Loop sound issue
            skalamichal Level 1

            I'm sorry, my mistake. I've already divided the position by 1000. It's not the issue.

             

            Any other ideas?

             

            Michal

            • 3. Re: Loop sound issue
              skalamichal Level 1

              The issue is that when the sound has for example 10 seconds and you pause the sound in the 7th second, after the sound is run via:

              sound.start(7/1000, 99999)

              the sound plays from second 7 to 10 and then again from 7 to 10. But I need to start it in the 7th second and then loop it again from the beginning.

               

              Michal

              • 4. Re: Loop sound issue
                kglad Adobe Community Professional & MVP

                you'll need to use the sound complete event to loop your sound, then.  you can remove trailing white space in a sound editor and bypass leading white space by using the offset parameter.

                • 5. Re: Loop sound issue
                  skalamichal Level 1

                  Hmm, it's something I've already tried. There are some small issues, check this code, which looked as the solution for me:

                   

                  // updated playSound function
                  function playSound()
                  {
                       if (soundtrack != null)
                       {
                            soundtrack.start(pausePos/1000); // don't loop, to fire the onSoundComplete event
                            soundtrack.onSoundComplete = Delegate.create(this, loopSound);
                       }
                  }
                  
                  // and here is the loopSound method, which will start the loop
                  function loopSound()
                  {
                       soundtrack.onSoundComplete = null; // remove the event
                       soundtrack.start(0, 99999);
                  }
                  

                   

                  This seems to be the best solution for me, because the sound is already updated to loop without any gap and plays nice when its run via the start(offset, loops) function. But doesn't play well when it's run via the onSoundCompleted event. In this case there is then only one gap.

                   

                  Anyway, I don't like this solution, because there is still one gap The best solution would be to start the sound as usual with the sound(offset, loops) function and reset the offset position mark. So it seems there is not the best solution. It seems like an bug in the Flash Player for me, but maybe it's done on purpose.

                   

                  Thank you for your responses!

                   

                  Michal

                  • 6. Re: Loop sound issue
                    kglad Adobe Community Professional & MVP

                    you're welcome.