Hi there! Currently i’m making a custom object oriented audio module and i’m running into an issue with looping audio.
I want to try to be able to loop “segments” of a longer audio seamlessly, so my script is checking for the current TimePosition. That all works but when I try setting TimePosition there’s a noticeable delay in the audio before it starts playing again. This isn’t the greatest code so if you have a fix or improvements please lmk!
Object is the Sound object in SoundService loop is a variable (in this case set to true) Segments is a dictionary full of different segments of audio segment is a variable that defines what audio segment is playing end_time and start_time are the timestamps of where the audio should loop
I know the audio is fully loaded
I know that there isn’t a pause in the audio file, I checked in audacity
--//Not full script, but this is the problematic part
--//This function is wrapped in a coroutine
while self.Object.Playing == true or self.Object.IsPaused==true do
if self.Object.TimePosition>=self.Segments[segment].end_time then
if loop==true then
self.Object.TimePosition = self.Segments[segment].start_time --//This is the problem child
else
self.Object:Stop()
break
end
end
task.wait() --//Needed to prevent timeout execution
end
If SoundTimePosition is not matching your expectations the most common cause is reading the value before the sound is fully loaded or before playback has properly started that happened to me before
TimePosition will not update correctly if the sound is still loading or if you check it immediately after calling Play because sound playback is handled asynchronously and may not reflect the real-time position right away
To get accurate values make sure the sound is completely loaded before checking TimePosition and avoid reading it immediately after triggering playback instead monitor the value over time using a stepped update loop
If the sound is streamed or large in size there may also be initial delays before TimePosition updates consistently especially on slower connections or first-time playback or anything likes these
This is a known behavior and syncing visuals or logic with audio requires careful timing and confirmation that the sound is fully ready before relying on its time properties
The sound is entirely loaded, I have it load in the sound before it plays for the first time and the issue persists no-matter how many times it loops. This is on the client btw.
To get accurate values make sure the sound is completely loaded before checking TimePosition and avoid reading it immediately after triggering playback instead monitor the value over time using a stepped update loop
I don’t think reading the value is the issue, setting it is. The pause comes from the Object.TimePosition if i’m not mistaken. Thanks for your reply nonetheless
Setting TimePosition during playback can cause delays I tried using this when I had this issue before because the engine/Roblox-Engine needs to buffer or decode audio from the new position especially with streamed or large files this is expected behavior with the current audio system and can cause brief hiccups for smoother seeking consider using shorter sounds or preloading smaller clips to reduce buffering another option is to stop the sound before setting TimePosition then play it again which can sometimes minimize the pause
There’s a property for Sounds that allows you to loop Audios without having to make your own looper. If you enable PlayBackRegionsEnabled, it’ll open 2 new properties (have to scroll down to find them after enabling), LoopRegion and PlaybackRegion.
Both the new properties take in 2 numbers, the first number (called Min) represents where the sound will start off, the second number (called Max) represents where the sound will end.
If the Sound’s property Loop is disabled, it’ll use the PlaybackRegion settings and if the Loop property is enabled, then LoopRegion will be used. You’ll likely have to use both Regions to make a seamless Audio playback.
I can’t go too in depth with how to loop your Sound since well, I’ve never really used the SoundLoopRegions in the first place and because I don’t have the segment’s start time and all that stuff so I’ll just summarize how you can do it. Anyway, I would
First, set the Loop property to false and just leave the PlaybackRegion alone.
Second, change the LoopRegion’s Min property to the start_time and the Max property to the end_time.
And the last part depends kinda depends on what you’re doing. I’ll just say, make a task.delay and wait the start_time+3 (add another 3 seconds in case the audio is delayed back a little for some reason) then check if sound is still playing and enable the Sound’s Loop property. You might run into some problems if the sound is constantly changing the Loop property and you’ll likely have to add on to the code too since, again, I don’t know what you’re doing for the Sound. Anyway here’s the code you should use after starting the sound or something idk.
task.delay(start_time+3,function()
if Object.IsPlaying then
Object.Looping = true
end
end)
self.Object.PlaybackRegionsEnabled=true
self.Object.LoopRegion =NumberRange.new(self.Segments[segment].start_time, self.Segments[segment].end_time)
self.Object:Play()
task.wait(1)
if self.Object.IsPlaying then
self.Object.Looped = true
end