Audioclip timesample to seek start position but not the end

I know from documentation that

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.

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?


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