- Home /
AudioSource play certain time range?
I have audio files containing several lines of dialogue each and I want to play just one phrase / line. Is it possible to play, for example, only 13.21 - 14.57 seconds into the AudioClip instantly?
AudioSource.time is get only, so I don't see any other options for setting the offset for which a AudioSource starts at.
I know how to wait and stop the sound, I just need to know how to play from an offset of seconds or samples. (C#)
Well, I never messed with that side of AudioSource before, but looking at the Unity references on it, these 3 functions might be some use maybe?
PlayScheduled Plays the clip at a specific time on the absolute time-line that AudioSettings.dspTime reads from.
SetScheduledEndTime Changes the time at which a sound that has already been scheduled to play will end. Notice that depending on the ti$$anonymous$$g not all rescheduling requests can be fulfilled.
SetScheduledStartTime Changes the time at which a sound that has already been scheduled to play will start.
Seems to look like what you want to do?
All of those delay the Play. Not offset of the time at which the clip starts at. So if I PlayScheduled(13.21f) the game will wait for 13.21 seconds and then start from 0 seconds into the clip. What I want is to play immediately 13.21 seconds into the AudioClip, but I don't think it even is possible at this rate.
Answer by amanpu · Sep 11, 2015 at 10:28 AM
AudioSource.time is not get only, you can also set time using time property and seek starting time wherever you want.
If I understand your question properly then following should work :
void Start()
{
audioSource.time = 13.21f; // assuming that you already have reference to your AudioSource
audioSource.Play();
}
void Update()
{
if(audioSource.time > 14.57f){
audioSource.Stop();
}
}
Answer by kalanadis · Dec 02, 2015 at 04:07 PM
@NibblesMeKibbles Try following 3 lines in order it works.
audioSource.time = 13.21f
audioSource.Play();
audioSource.SetScheduledEndTime(AudioSettings.dspTime+(14.57f-13.21f));
Thanks!
void PlaySoundInterval(float fromSeconds, toSeconds)
{
audioSource.time = fromSeconds;
audioSource.Play();
audioSource.SetScheduledEndTime(AudioSettings.dspTime+(toSeconds-fromSeconds));
}