6 Replies Latest reply on Jun 13, 2008 6:38 AM by Gaz.T.

    Check if sound is playing

    Gaz.T. Level 1
      I want to discover if a sound object (e.g. 'myMusic') is playing. I've tried the code at the bottom of this post:-

      My idea was to call a function that checked the sound's position twice and compared the two values. If they were different, then the sound is playing. However it seems that interrogating with '.position' doesn't give 2 separate values if done in quick succession.

      I basically want a return value that tells me if a sound is playing or not.

      Can anyone help please
        • 1. Check if sound is playing
          Noelbaland Level 1
          Hello there,

          Try using an onEnterFrame event to listen for a change in the sounds position. Like this

          • 2. Re: Check if sound is playing
            clbeech Level 3
            actually Gaz - you already have the right idea, if as you indicate, you only need to check the sound at the time the condition is called - there's just a couple of things you need to do differently. you should check the position against the 'durration' of the sound - and you need a return type on the method.
            • 3. Check if sound is playing
              Gaz.T. Level 1
              Thanks for your help Noel,

              The problem I'm having is I need my code to decide, almost instantly, if a sound needs to be played or not. (within milliseconds, not 2 seconds unfortunately)

              It seems that a sound that's 10,000 milliseconds long will have a position value of 0 before it's played and a value of 10,000 when it has finished. However if the sound was stopped somewhere in the middle, the position value freezes at the point it was when the sound stopped.

              Later in the code, I want to start that sound again if the sound isn't playing, but if it already is, then I don't want to start it.

              I can write an if statement to see if the sound hasn't been played (mySound.position==0) or if it has reached the end (mySound.position==mySound.duration) but if it was stopped mid flow (so to speak) and the position value is 3045, for example, I can't find a way of telling if the sound was stopped at 3045 milliseconds, or if it is currently playing and just happens to be at 3045 ms.

              I thought by checking the position at two separate intervals (of a playing sound), then a few milliseconds will have lapsed between statements and the values would be different. Unfortunately they were exactly the same. :( So a playing sound would give the same result as a stopped sound. I could put a delay in between the two readings, but that would slow my animation down too much.

              I've got over the problem by using boolean variables for each sound that get set to 'True' when a sound is started, get set to 'False' using an .onSoundComplete command, or get set to 'False' if the sound is stopped mid flow.

              It's a bit of a clumsy way of doing it, but it seems to work and it makes my if Statement a bit easier.

              If someone else has a better way, then I'd be over the moon to learn

              Many thanks
              • 4. Check if sound is playing
                Gaz.T. Level 1
                Thanks clbeech

                Unfortunately that only tells me that the play position is somewhere in the middle and not whether it's moving or stationary, I should have clarified the sound could have been stopped part way through it's playing cycle, and that's what I needed to determine (if it's playing or stopped).

                Thanks for your help.
                • 5. Re: Check if sound is playing
                  clbeech Level 3
                  if that is the case you should already 'know' that the sound was stopped - use a master variable and set it when stop() is called.
                  • 6. Re: Check if sound is playing
                    Gaz.T. Level 1
                    Thanks for that,

                    That's what I've done to resolve it (in my reply to Noelballand), it looks like there's no other way, I thought there may have been a built in method to check if a sound was playing that I'd overlooked.

                    Many thanks for your help.