- Home /
Unity WebGL audio not looping when started with playdelayed()
I have an audioSource in which loop is set to true. I am starting it playing by calling playdelayed()
In the editor its plays fine. In a webgl build it does not loop. It just plays once.
Also in firefox it does not start playing at the correct time, it is a good fraction of a second off.
public override void MatchStarted()
{
Intro.Play();
Loop.PlayDelayed(Intro.clip.length);
}
PlayScheduled has exactly the same effect.
public override void MatchStarted()
{
Intro.Play();
Loop.PlayScheduled(AudioSettings.dspTime + Intro.clip.length);
}
Play(ulong) has the same effect
public override void MatchStarted()
{
Intro.Play();
Loop.Play((ulong)(44100 * Intro.clip.length));
}
I'm posting this here seeking workarounds. Any suggestions? As you can see from my code, my use case is a piece of music with an intro and a loop.
...
ps. I have seen some posts saying that if the bitrate is not 44.1 KHz this can cause looping issues on WebGl. The audio file in question is set to override the bitrate to 44.1 in its import settings.
Answer by deltron1830 · Apr 16, 2018 at 01:32 PM
Ok here is my workaround. I added the intro and loop together into one file. I also added on a little bit more of the start of the loop onto the end, and I made a note of exactly what time the loop should start and end at.
const float startlooptime = 11.875f;
const float endlooptime = 74.375f;
public override void MatchStarted()
{
Loop.Play();
}
void Update()
{
if (!Loop.isPlaying) return;
if (Loop.time > endlooptime) Loop.time = startlooptime + (Loop.time - endlooptime);
}
Cheers for this insight man! This issue is still around in 2020.1.6f1. I had the exact same use case and tests, almost down to the variable names, haha. Great idea - thanks for sharing!
Your answer
![](https://koobas.hobune.stream/wayback/20220612160845im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Audio loop after time 2 Answers
How can I loop a song from a specific point? 1 Answer
[iOS] Streaming audio with loop 1 Answer