- Home /
Audio Clip Random Start time, end random amount of timelater
Here's what I'm trying to do. I have 6 audio clips. I want to randomly load one and start it at a random time. I want it to stop a random amount of time later. This stop time would preferably be a float as I only want each clip to play between .4-.8 seconds.
Currently, I'm just trying to add 1 second to the stop time because I think that audio.timeSamples is an int and I don't really understand how to add a float amount. There's probably a better way to do it, but after a lot of Googling, I can't quite get it.
So far, it's not throwing any errors, but it's not doing what I want it to do. I just loads a random clip every frame. I think I'm supposed to be using the sample rate here somewhere, but I'm not understanding how to use it correctly. All of the clips are at 11025.
Thanks!
using UnityEngine; using System.Collections;
public class VoiceScanner : MonoBehaviour {
public AudioClip[] myClip;
int RandomStartTime = Random.Range(1,50);
public AudioSource soundFile;
// Use this for initialization
void Start () {
audio.clip = myClip[Random.Range(0,myClip.Length)];
audio.Play();
}
void Update () {
int startTimesample = RandomStartTime ;
int endTimesample = RandomStartTime +(1);
if (audio.timeSamples < startTimesample) {
audio.timeSamples = startTimesample;
} else if(audio.timeSamples > endTimesample){
audio.Stop();
}
}
}
I think you might want : http://docs.unity3d.com/ScriptReference/AudioSource-time.html
Incidentally, this sounds like a job for coroutines, to me. $$anonymous$$any time-interval-specific jobs are ideal candidates for coroutines : http://docs.unity3d.com/$$anonymous$$anual/Coroutines.html
Thanks AlwaysSunny. I've been playing around with corountines for the last couple of hours, new stuff for me. I think I have a decent grip on them now. Going to check those links and see what I can do.. Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613170339im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Next enemy wave timer issue 0 Answers
Radial Sound Spectrum - Effect 0 Answers
Sound Timing 1 Answer
Trigger Start of Sound 1 Answer
Repeat Function 1 Answer