|  | Check if sound is playing |  | |
| | | Gaz.T. |  |
| Posted: Fri Jun 13, 2008 9:06 am Post subject: Check if sound is playing |  |
I want to discover if a sound (e.g. 'myMusic') is playing. I've tried this:- (code may not be where I want it to be in the forum, I'm new here and don't fully understand the attach code thing).
but it seems that using '.position' doesn't give 2 separate values if interrogated twice very quickly. I basically want a return value that tells me if a sound is playing or not.
Can anyone help please :smile;
if (soundPlaying(myMusic)){ do something... }
function soundPlaying(soundName){ sample=soundName.position; if (soundName.position != sample){ val=1; } else { val=undefined; } return val; } |
| |
| | | Noelbaland |  |
| Posted: Fri Jun 13, 2008 11:00 am Post subject: Re: Check if sound is playing |  |
Hello there,
Try using an onEnterFrame event to listen for a change in the sounds position. Like this |
| |
| | | clbeech |  |
| Posted: Fri Jun 13, 2008 12:39 pm Post subject: Re: Check if sound is playing |  |
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.
if (soundPlaying(myMusic)){ //do something... }
function soundPlaying(s:Sound):Boolean { if(s.position < s.durration) { return true; }else{ return false; } } |
| |
| | | Gaz.T. |  |
| Posted: Fri Jun 13, 2008 1:08 pm Post subject: Re: Check if sound is playing |  |
| |  | |
Thanks for your help,
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 |
| |
| | | Gaz.T. |  |
| Posted: Fri Jun 13, 2008 1:16 pm Post subject: Re: Check if sound is playing |  |
Thanks clbeech
Unfortunately that only tells me that the play position is somewhere in the middle and not whether it's moving or stationary, but thanks for your help. |
| |
| | | clbeech |  |
| Posted: Fri Jun 13, 2008 1:27 pm Post subject: Re: Check if sound is playing |  |
| 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. |
| |
| | | Gaz.T. |  |
| Posted: Fri Jun 13, 2008 1:38 pm Post subject: Re: Check if sound is playing |  |
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. |
| |
|
|