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??
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;
}
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)
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.
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.
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.
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
I'm glad you were able to figure it out.
Your answer
Follow this Question
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