11 Replies Latest reply on Feb 26, 2007 11:33 AM by Rory_Carlton

    Help with sound and SetInterval

    Rory_Carlton
      I'm loading several sounds into an array and then calling them to play at various points along the timeline. There are gaps (pauses) between the places where the sounds play. So far all is working fine but the problem I'm having is what if one sound is called to play before the previous sound has completed? I'm trying to use SetInterval to make the second sound wait until the first has finished, but it's not working.

      This code is in frame 1 to set everything up. The timeline is very short and in frame 7 i call one sound and in frame 8 the next. It works almost right. The first sound plays, and the second does not start, and the trace ("sound overlap") appears.. The first sound completes and the trace("sound complete") appears, but nothing more. The second sound does not start. Also, the trace("waiting") never appears. It's like the waitForSound function never gets called.

      Attached is the code:

      Here is the output:

      loading sound 1: sounds/01_01.mp3
      loading sound 2: sounds/01_02.mp3
      loading sound 3: sounds/01_03.mp3
      ...etc...
      loading sound 24: sounds/01_24.mp3
      playing sound
      sound overlap
      sound complete

      Also, the main timeline never gets to the end of the movie, so I believe the _root.play() is not getting called.

      What am I doing wrong? Thanks in advance for any help.
        • 1. Re: Help with sound and SetInterval
          kglad Adobe Community Professional & MVP
          use the attach code option to display your actionscript. otherwise, your code is apt to be mangled by this forum. your code is difficult to read.

          the fatal errors are a failure to clear that interval before it's set which can lead to more than one setInterval being called before previous ones are cleared and the failure to pass "this" to waitForSound().

          anyway, this problem is not that complex. just check to see if a sound is playing when a new sound is called for (by check soundPlaying) and if it is, define nextIndex (the index of the next sound that should play):

          • 2. Re: Help with sound and SetInterval
            Rory_Carlton Level 1
            Thanks very much for the help, but I still have a couple more questions.

            Can you explain more about "failure to pass "this" to waitForSound()."? How should I do that?

            Also, if the sounds are loaded with the code you gave, will the movie wait at that point until all the sounds have loaded, or will it continue playing while the sounds continue loading? I want the movie to continue while the sounds load in the background, so I can then call them as needed.

            Thanks
            • 3. Re: Help with sound and SetInterval
              Rory_Carlton Level 1
              P.S. Thanks for the tip on attach code. I went back and fixed that.
              • 4. Re: Help with sound and SetInterval
                kglad Adobe Community Professional & MVP
                no, you'll need to use something (like your onLoad method) to ensure all your sounds are loaded before proceding.

                when setInterval() calls a function "this" in the function is undefined unless you specifically pass that reference to the function. to do that you should use:

                • 5. Help with sound and SetInterval
                  Rory_Carlton Level 1
                  Well, thank you again for your help, but it's still not working. I now have the code as below.

                  The output is:
                  loading sound 1: sounds/01_01.mp3
                  loading sound 2: sounds/01_02.mp3
                  loading sound 3: sounds/01_03.mp3
                  ...etc...loading sound 24: sounds/01_24.mp3
                  playing sound
                  sound overlap
                  sound complete

                  The trace("waiitng") never appears. And it seems the main timeline remains stopped, because the movie never runs to its end.
                  • 6. Re: Help with sound and SetInterval
                    kglad Adobe Community Professional & MVP
                    there are, at least, two issues with your code.

                    you have one playMe() method for each sound object (and that's unneeded) and you're using this.start() where "this" refers to the timeline that contains that code, not to the sound object.
                    • 7. Re: Help with sound and SetInterval
                      Rory_Carlton Level 1
                      That was really helpful! Of course you were right about all those playMe() functions. I pulled that out as a separate function. Also, the "this" was incorrect. I pulled the waitForSound() function out also. The new code WORKS!

                      In the frame where I want the sound to start, i now have "playMe(1);" or "playMe(2);" etc.

                      I do have one more question. The business of passing the soundNum parameter to playMe() and then having to store that in _root. nextSound so that waitForSound() knows what sound to play when the interval clears... that seems clumsy to me. I mean, it works, but it seems inelegant. Is there some way for playMe to pass the soundNum to waitForSound? Something like:

                      soundInterval = setInterval(tl,"waitForSound(soundNum)", 10);

                      ... but that doesn't work.

                      Any suggestions?
                      • 8. Re: Help with sound and SetInterval
                        kglad Adobe Community Professional & MVP
                        yes, you can pass an unlimited number of parameter to the function called by setInterval(). those parameters should be listed after the frequency parameter:

                        • 9. Re: Help with sound and SetInterval
                          Rory_Carlton Level 1
                          Excellent! Thank you, I have updated it and it works perfectly.

                          One other thing and then I will stop asking questions, I promise - you must have better things to do than this! Because some of the sound files may be quite large, and they can be played in an unpredictable order, I need to check that a sound has been loaded before trying to play it. Especially over a slow Internet connection.

                          I updated the script to cover this case, but it's hard to test because the sounds load almost instantly from my hard drive. Can you take a look at the script and see if it looks correct, or do you have any ideas for testing it?

                          Thanks again.

                          • 10. Re: Help with sound and SetInterval
                            kglad Adobe Community Professional & MVP
                            click on control/test movie. in the test environment click view/download settings and click the download speed you want to test. then click view/simulate download to test.
                            • 11. Re: Help with sound and SetInterval
                              Rory_Carlton Level 1
                              Thank you. Now another problem: the movie plays fine and loads the sounds when I try it in the Flash authoring environment, or when I play the swf in the Flash Player.

                              But when I place the SWF in an HTML document (using DreamWeaver). it plays with no sound. The mp3 files are all in the "sounds" directory, which is inside the "flash" directory, where the swf is located.

                              This happens when I try it on my hard drive as well as when it's over the internet. What can the problem be?