Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aaabbbsss · Nov 17, 2015 at 01:41 PM · c#buttonaudioaudiosourceisplaying

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! :)

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Nov 17, 2015 at 01:41 PM 0
Share

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...

avatar image Le-Pampelmuse · Nov 17, 2015 at 08:44 PM 2
Share

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:

if(!AudioSource.isPlaying) is not working inside button script

or

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.

avatar image Le-Pampelmuse · Dec 14, 2015 at 09:38 PM 1
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges