10 Replies Latest reply on May 1, 2007 10:16 AM by fungfi1

    Checking if song is playing

    scottjan9119 Level 1
      I'm making a flash site with 4 frames. 1 Frame for each section of the web site. On the first frame I have attached a soundtrack. There are buttons on the page which bring you to each section of the site. My problem is that when you go back to the first frame, the music plays again, even when the other one is still playing. I'm not sure, but I'm guessing I have to write a function to check and see if the soundtrack is already playing...Can anyone direct me in the write direction.
        • 1. Re: Checking if song is playing
          Level 7
          scottjan9119,

          > My problem is that when you go back to the first frame,
          > the music plays again, even when the other one is still
          > playing.

          Did you use the Sound class to create a sound object, or series of sound
          objects? If not, it may be that you've put music into your keyframes as
          Event sounds, which load into memory and play completely.

          You can use the stopAllSounds() function to, well, to do what its name
          suggests. You might experiment with a Sync setting of Stream, rather than
          Event (see the Property inspector while the sound is selected in its
          keyframe) -- but that would require putting the sounds into their own movie
          clips, or having an incredibly long timeline in order to give enough room
          for the sounds to play out.

          Your best bet, though, is to use the Sound class to load MP3 files from
          outside the SWF. And you may already be doing that. If so, it's one of
          those facts of life that the Sound class doesn't have something like an
          isPlaying property. That would be super convenient, because checking the
          Sound.position property isn't enough (this one tells how far along the audio
          is, so if it's greater than zero, you know the audio is playing -- unless
          the music has been paused halfway through).

          So I would set a variable of your own choosing -- call it
          audioIsPlaying, say -- and set that to true when you've started your music,
          then set it to false when you stop the music.

          If any of this makes sense, have at it. If not, let me know where your
          comfort level is with the Sound class and I'll make a few concrete
          suggestions. :)


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


          • 2. Re: Checking if song is playing
            scottjan9119 Level 1
            Thanks David,
            Yeah I'm pretty new to actionscripting and sound, but on my first frame of the main movie, I have a loop that I imported and used linkage to name it.
            firstSound = new Sound();
            firstSound.attachSound("SoundTrack7");
            firstSound.start(0,999);

            I want it to play as soon as the movie loads(which it does)
            I have the movie stop(); on each of the four frames that I have made 1 being home page.
            Then when I go to frame 2 3 or 4 the music is still playing(which is fine) but when i go back to frame 1
            the sound loads again, ontop of the already playing sound.

            • 3. Re: Checking if song is playing
              Level 7
              scottjan9119,

              > I have a loop that I imported and used linkage to name it.
              > firstSound = new Sound();

              The Sound constructor accepts an optional parameter that you may or may
              not want to use, depending on your needs. You might be fine, just as you
              are, but without a parameter, which would be a movie clip ...

              var firstSound = new Sound(someMovieClip);

              ... the sound object is associated with the whole SWF movie, rather than
              harnessed into its own quasi "sound channel" by way of a movie clip. This
              is just an aside, really, but this article may shed some light on it.

              http://www.quip.net/blog/2006/flash/actionscript-20/understanding-the-sound-constructor

              > firstSound.attachSound("SoundTrack7");
              > firstSound.start(0,999);

              The rest is fine, so far.

              > I want it to play as soon as the movie loads(which it does)

              Right.

              > I have the movie stop(); on each of the four frames that I
              > have made 1 being home page. Then when I go to frame
              > 2 3 or 4 the music is still playing(which is fine) but when i
              > go back to frame 1the sound loads again, ontop of the
              > already playing sound.

              Yeah, that's because you've asked the audio to repeat almost a thousand
              times. Addressing the timeline with that stop() function is one thing, but
              there are a number of meanings to the word "stop" in ActionScript. On its
              own, stop() usually refers to the stop() function, which tells the timeline
              what to do. There's also the MovieClip.stop() method, which amounts to the
              same thing -- but it specifically targets movie clip timelines (including
              the main timeline). There's also the Sound.stop() method, which is what
              you're likely to want here.

              In frame two, your go-to-frame-1 button should use the second sound's
              instance name to reference that method ...

              secondSound.stop();
              gotoAndStop(1);
              // etc.

              Make sense?


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


              • 4. Re: Checking if song is playing
                scottjan9119 Level 1
                I don't want the sound to stop playing entirely just on top of each other...
                say I have the song Row Your Boat playing on frame 1
                I go to frame 2 and Row Your Boat is still playing.
                I go back to frame 1 and Row your Boat is still playing PLUS another Row Your Boat starts playing on top of the first.
                Go to frame 3 (still hearing the 2 Row Your Boats) go back to frame 1
                There are now 3 versions of Row Your Boat playing.

                How do I stop that from happening
                Thanks
                • 5. Re: Checking if song is playing
                  Level 7
                  scottjan9119,

                  >I don't want the sound to stop playing entirely just on top
                  > of each other...

                  Aha!

                  > say I have the song Row Your Boat playing on frame 1
                  > I go to frame 2 and Row Your Boat is still playing.

                  Gotcha.

                  > I go back to frame 1 and Row your Boat is still playing
                  > PLUS another Row Your Boat starts playing on top of
                  > the first.

                  Right. Well, in that case, yes ... checking an isPlaying variable will
                  do it. The name of the variable is arbitrary, but isPlaying makes sense.
                  In frame 1, when you start up Row Your Boat, set isPlaying to true. In
                  frame 2, check isPlaying before you start the other song.

                  if (isPlaying == false) {
                  // set isPlaying to false again and
                  // start up song two
                  }

                  Then all you have to do is get the songs themselves to set isPlaying
                  back to true. You can use the Sound.onComplete event to handle this.

                  Here's a related example on that event.

                  http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially


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


                  • 6. Re: Checking if song is playing
                    scottjan9119 Level 1
                    great thanks
                    • 7. Re: Checking if song is playing
                      mannerski Level 1
                      I had the same problem in my movie. I got rid of it in much simpler way: Copy all layers including sound from frame 1. Create new scene (scene 1) and paste frames. Now the original scene is scene 2. Don't put any actions in scene one.
                      The movie is going to start and go to scene 2 playing the sound. When you go back to frame one of scene 2 it won't overlap.
                      You can do the same using additional frame in the beginning that looks the same and swap all actions to frame 2.
                      • 8. Re: Checking if song is playing
                        fungfi1 Level 1
                        hi scottjan9119,

                        I saw your problem and I tried to follow on how to fix it but I don't understand it. Can you help me? I have the exact same problem you had I have tried fixing it for hours. I am new at this and don't know very much AS. Please help. thanks.
                        • 9. Re: Checking if song is playing
                          dazzie Level 1
                          I think you can get around this (in true hack style) by simply putting

                          firstSound.stop();

                          on frame one just before you create the new sound object. That's what I've done in the past for a quick solution (although the previous replies give more adaptive solutions). Using this method will simply restart the first track but will stop the existing one playing first.
                          • 10. Re: Checking if song is playing
                            fungfi1 Level 1
                            great! thanks dazzie...but i am still trying to make it so that the music just keeps playing without repeating. Thanks agin