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 /
  • Help Room /
avatar image
0
Question by KnightRiderGuy · Nov 02, 2016 at 12:37 PM · c#arrayscoroutinestimersaudio.playoneshot

Determine Audio Clip Length From An Array And Then Add A Delay

Ok I have kind of a problem that I'm sure there is a more elegant solution for than the one I'm using. I have a lot of voice clips that are accessed through numerous arrays like this. I'm currently using a coroutine for a few of the ones that just have one audio clip to add a little time onto the end of the Audio by setting a bool I call: KITT_SaidThis to true and then back to false about two seconds after the audio has completed, The reason for this is that I have a Voice recognition input that quite often thinks the Audio Clip responses are inputs, but I find if I add a little time after the response Audio then that seems to cure the problem. Like I say I'm accessing my response audio from numerous arrays like this.

 public void BillyTheKiddFactoid(){
         source.PlayOneShot (BillyTheKiddClips [Random.Range (0, BillyTheKiddClips.Length)]);
         StartCoroutine (BTKvoiceBlocker ());
     }
     IEnumerator BTKvoiceBlocker(){
         KITTmodesObject.KITT_SaidThis = true;
         yield return new WaitForSeconds (12.5f);    //Wait Time
         KITTmodesObject.KITT_SaidThis = false;
     }
     //Parapsychology
     public void ParapsychologyFactoid(){
         source.PlayOneShot (ParapsychologyClips [Random.Range (0, ParapsychologyClips.Length)]);
         StartCoroutine (PSvoiceBlocker ());
     }
     IEnumerator PSvoiceBlocker(){
         KITTmodesObject.KITT_SaidThis = true;
         yield return new WaitForSeconds (5.5f);    //Wait Time
         KITTmodesObject.KITT_SaidThis = false;
     }


It works OK for these two as they are just single audio responses so I just looked at the Audio clip length in the inspector and then added on a little extra time for my KITT_SaidThis bool to be true, Which does the trick but on the other response arrays I have more than one response drawn randomly so unless I used a generic time delay there are times there would be too long a delay, if you catch my meaning. I tried using an invoke method and using the audio length but strangely I found this did not work on the audio response clips that were 10 or so seconds long as the KITT_SaidThis bool would get activated to true but then at the end of the Audio response it would not go back to false. So the invoke method did not work in my case??

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Cynikal · Nov 03, 2016 at 01:06 PM

 public float AdditionalDelay = 0.5f;
 AudioClip myClip = ParapsychologyClips [Random.Range (0, ParapsychologyClips.Length)];
 private float MyAudioDelay = myClip.length + AdditionalDelay;

And then:

      IEnumerator PSvoiceBlocker(){
          KITTmodesObject.KITT_SaidThis = true;
          yield return new WaitForSeconds (MyAudioDelay);    //Wait Time
          KITTmodesObject.KITT_SaidThis = false;
      }
Comment
Add comment · Show 6 · Share
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 Cynikal ♦ · Nov 03, 2016 at 01:10 PM 0
Share

Or, you can do:

 public float AdditionalDelay = 0.5f;
 
  public void BillyThe$$anonymous$$iddFactoid(){
 AudioClip myClip = BillyThe$$anonymous$$iddClips [Random.Range (0, BillyThe$$anonymous$$iddClips.Length)];
          source.PlayOneShot (myClip);
          StartCoroutine (BT$$anonymous$$voiceBlocker (myClip.length + AdditionalDelay));
      }
      IEnumerator BT$$anonymous$$voiceBlocker(float TimeDelay){
          $$anonymous$$ITTmodesObject.$$anonymous$$ITT_SaidThis = true;
          yield return new WaitForSeconds (TimeDelay);    //Wait Time
          $$anonymous$$ITTmodesObject.$$anonymous$$ITT_SaidThis = false;
      }
      //Parapsychology
      public void ParapsychologyFactoid(){
 AudioClip myClip = ParapsychologyClips [Random.Range (0, ParapsychologyClips.Length)];
          source.PlayOneShot (myClip);
          StartCoroutine (PSvoiceBlocker (myClip.length + AdditionalDelay));
      }
      IEnumerator PSvoiceBlocker(float TimeDelay){
          $$anonymous$$ITTmodesObject.$$anonymous$$ITT_SaidThis = true;
          yield return new WaitForSeconds (TimeDelay);    //Wait Time
          $$anonymous$$ITTmodesObject.$$anonymous$$ITT_SaidThis = false;
      }

(I winged it via the reply feature here... so it might of came out crappy)

avatar image Landern · Nov 03, 2016 at 03:22 PM 0
Share

If you set fields in a class(since thats the only way public and private get used and wouldn't function in a method) in such a way:

 public float AdditionalDelay = 0.5f;
 AudioClip myClip = ParapsychologyClips [Random.Range (0, ParapsychologyClips.Length)];
 private float $$anonymous$$yAudioDelay = myClip.length + AdditionalDelay;

Would result in a error stating:

A field initializer cannot reference the non-static field, method, or property 'sometype.field'

Use the Awake or Start methods to set these variables, primitive types can have default values or be set(thing ints, floats, string etc), but referencing another field during initialization is going to end in pain. Static fields are a different story but thats covered in the error.

avatar image Cynikal ♦ Landern · Nov 03, 2016 at 03:27 PM 0
Share

You're right. I assumed with my later comment of me winging it would imply it was slightly out of order.

I'll make sure the next time I help people at 4am i'll be more thorough.

avatar image KnightRiderGuy Cynikal ♦ · Nov 03, 2016 at 04:04 PM 0
Share

Thanks @Cynikal, Wow 4Am?! that deserves a pat on the back man ;) So in your solution would I just need to call the IEnumerator just once or would I need to do like you have where I would need a special one for each of my audio clip array calls?

the only reason I ask is because I have a heck of a lot of individual arrays pretty much one for every audio input from our IVR_Settings file.

avatar image KnightRiderGuy · Nov 05, 2016 at 12:43 PM 0
Share

Thanks @Cynikal, This worked perfectly. I find I can just call the same IEnumerator from all of my Voice call arrays which is awesome. :D

avatar image Cynikal ♦ KnightRiderGuy · Nov 05, 2016 at 08:18 PM 0
Share

I'm glad you were able to figure it out.

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

231 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Keep track of last audio clip played from multiple arrays 1 Answer

Coroutine beginner 1 Answer

Coroutine not executing second time 0 Answers

Count Down Timer C# Conversion Script Not Working Help 3 Answers

How to interrupt wait coroutine in a loop? 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