- Home /
Audio is always playing
Hi Unityanswers, I am encountering problems with the audio.
public void travel()
{
walkingSound.Play ();
if (walkingSound.isPlaying)
{
Debug.Log("yes");
}
if (!walkingSound.isPlaying)
{
Debug.Log("no");
}
}
This function is called when I click a UI button. When I click the button, the audio plays and the console prints "yes". However even after the audio has stopped playing, the console does not print "no". I have tried the same code without the line that plays the audio (walkingSound.Play ();) and when I click the button, the console prints "no". So it appears that the audio hasn't stopped playing even though it seems to have stopped playing. How do I fix this? Thank you very much for your help! :)
your code is working as it should.
you're playing the sound then checking whether it's playing - always yes.
if you remove the play command, then it's not, so it'll always display no!
are you expecting the sound the play, then receive a notification after it stops? if so, you'll need to put that check elsewhere.
perhaps the question to be asked is why do you need to know when it's finished?
one way to do this is to trigger a coroutine when the sound is played, with an appropriate delay (for the audio clip length) - this coroutine can then inform you that the sound has finished. however, this is messy. obviously, it depends on what you're really trying to do...
Your problem is the placement of the .isPlaying()
check. Right now, if you click the button, the audio starts to play AND at the same time it checks if it is not playing. So logically, it will never print "no" because it always starts the audio at the same time.
Your function void travel() is only called when you press the button. So the part..
if (!walkingSound.isPlaying)
{
Debug.Log("no");
}
..is also only called when the button is pressed.
If you want to constantly check if something happens, use a built-in function like void Update()
and put the check inside of it. This function is called every frame.
So every frame the audio is not playing, the console will print "no".
It will also print "no" before you even clicked the button, because logically the code always checks.
If you don't want to check it all the time, you would have to write additional code that handles under which conditions the check should happen.
Additionally I ask you to read the FAQ before posting questions or comments:
answers.unity3d.com/page/faq.html
On top of that, your question text is confusing and does not describe the problem you are having.
Audio is always playing..
..can mean, you can't stop an AudioSource, Audio loops altough you didn't set it to loop, etc, but not your particular problem.
Because your audio is not always playing.
Be clear and precise in describing the problem in the question text.
An appropriate question text would have been:
orif(!AudioSource.isPlaying) is not working inside button script
How to check if AudioSource stopped playing with a button?
$$anonymous$$ake sure your whole question is formatted so it is easy to read.
For example the code part of your question is formatted correctly, but the text after the code is just one long strip whitout line breaks or highlighted code snippets
.
Feel free to ask if you have problems understanding my answer. Have a nice day.
Hi, just checking if you found my answer to be useful. ;) If yes, please accept it and close the question. If no, or only partially yes. feel free to ask about the part where you have trouble.
Your answer
Follow this Question
Related Questions
Delayed Button Sounds 0 Answers
Audio won't fade out. 1 Answer
PlayClipAtPoint Qualify with Type Name 2 Answers
Audio/c#/unity Can i control the duration by time ? 1 Answer
audio doesn't play on movement 0 Answers