- Home /
Audioclip timesample to seek start position but not the end
I know from documentation that
AudioSource.timeSamples
Use this to read current playback time or to seek to a new playback time in samples, if you want more precise timing than what time variable allows.
http://unity3d.com/support/documentation/ScriptReference/AudioSource-timeSamples.html
However with this property is possible to control (seek) only the starting point of the clip. Not the ending !
I have a very long audio (.wav) and I need to play a sound exactly starting from a timeSample position for a quantity in timesamples (which is variable and depending of the words). The sound sequence to play is NOT decided when I compile the application.
I don't want to split the file into smaller clips and yield, I would like to map the track and, in the code, using the start and end position play the sub clip only.
Is this possible in Unity3D, why the clip can have a seek but not a duration and has to be played till the end?
Thanks.
Answer by aldonaletto · Sep 29, 2011 at 04:00 PM
You can check the current timeSamples and stop the sound when necessary. It's not 100% precise since you must check it in some periodic function, but doing it at FixedUpdate gives a 20mS resolution, which is precise enough for most cases.
timeSamples is based on the sound sample rate, thus you must know it - just click the sound in your Project view and the Inspector will show the sample rate in the Preview window at the bottom.
The script could be something like this:
var endPlay: int = 0; // sample count to stop - 0 disables endPlay
function PlayTo(endSample: int){ endPlay = endSample; // set the end time... audio.Play(); // and start the sound }
function FixedUpdate(){ if (endPlay > 0 && audio.timeSamples >= endPlay){ audio.Stop(); // stop the sound when time reached } }
Your answer
![](https://koobas.hobune.stream/wayback/20220612231139im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making sounds wait for each other to finish 2 Answers
Playing a sound clip from another object 1 Answer
Get audio from device... -1 Answers
Help with sound clips [C#] 0 Answers
Play Sound when GUI button is pressed. 7 Answers