Search Unity

Unity WebGL audio not looping when started with playdelayed()

Discussion in 'Web' started by deltron1830, Apr 16, 2018.

  1. deltron1830

    deltron1830

    Joined:
    Mar 20, 2013
    Posts:
    27
    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.

    Code (CSharp):
    1.     public override void MatchStarted()
    2.     {
    3.         Intro.Play();
    4.         Loop.PlayDelayed(Intro.clip.length);
    5.     }
    PlayScheduled has exactly the same effect.

    Code (CSharp):
    1.     public override void MatchStarted()
    2.     {
    3.         Intro.Play();
    4.         Loop.PlayScheduled(AudioSettings.dspTime + Intro.clip.length);
    5.     }

    Play(ulong) has the same effect

    Code (CSharp):
    1.     public override void MatchStarted()
    2.     {
    3.         Intro.Play();
    4.         Loop.Play((ulong)(44100 * Intro.clip.length));
    5.     }
    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.
     
    sidneymelo likes this.
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Hi,
    we have an open bug here about the looping not working when using PlayScheduled. Unfortunately, we don't know yet when this is going to be fixed.
     
  3. deltron1830

    deltron1830

    Joined:
    Mar 20, 2013
    Posts:
    27
    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.



    Code (CSharp):
    1. const float startlooptime = 11.875f;
    2. const float endlooptime = 74.375f;
    3.  
    4. public override void MatchStarted()
    5. {
    6.     Loop.Play();
    7. }
    8.  
    9. void Update()
    10. {
    11.     if (!Loop.isPlaying) return;
    12.  
    13.     if (Loop.time > endlooptime) Loop.time = startlooptime + (Loop.time - endlooptime);
    14. }
    15.  
    16.  
     
  4. TimmermanV

    TimmermanV

    Joined:
    Sep 27, 2014
    Posts:
    4
    This is really unfortunate. I hope this gets fixed soon. I voted on the issue.
     
  5. sidneymelo

    sidneymelo

    Joined:
    Jan 24, 2015
    Posts:
    1
    I voted too, this issue troubles me a lot
     
  6. Tunztunztunz

    Tunztunztunz

    Joined:
    Jan 7, 2016
    Posts:
    8
    Has this been fixed in 8 years?
     
  7. matt-nexus

    matt-nexus

    Joined:
    Aug 4, 2021
    Posts:
    10
    Last edited: Dec 29, 2021