Search Unity

WebGL audio not looping correctly

Discussion in 'Web' started by Brathnann, Feb 5, 2016.

  1. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I have a couple of audio files that loop for background music. In the editor, they play correctly. But once uploaded to the web as a webgl build, they only play about halfway and then start over again.

    I have tried both wav and ogg files and several different music files, but they all seem to run into this problem.

    They play fine if looping is not selected and the build is uploaded (they play through and then stop at the end like expected).

    Both firefox and chrome experience the odd half-loop problem. Also, if it helps, this is a facebook canvas game.

    Does anybody have an idea of what might be causing this or a possible fix?

    Thank you for any help provided.
     
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    can you reproduce it locally ? what version of Unity ?

    would you mind submitting a bug report with a project we can use to reproduce it ?
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Not sure if it's a bug or just by design. We discovered that if the audio is at 22k hz, it wasn't looping properly. Setting it to 44k hz allowed it to loop properly.
     
  4. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Yes, there is currently a bug (fixed in 5.5) with audio clips not imported at 44kHz. Web Audio resamples everything to 44Khz anyways, so for now, just set up your clip to be resamples to 44kHz in the editor, and you should be good.
     
  5. sirrus

    sirrus

    Joined:
    Jun 10, 2012
    Posts:
    250
    This didn't seem to work for us. We have a WAV file downloaded via assetbundle and per the suggestion here, set "Override Sample Rate" and forced it to 44.1khz. It still does not loop properly in the WebGL build.

    Did I miss something?
     
  6. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Yes, that should work. If not, can you file a bug report with a repro case?
     
  7. javier-mazzurco

    javier-mazzurco

    Joined:
    Jan 22, 2016
    Posts:
    6
    this workarround worked fine for me! thanks!
     
  8. archiesteel

    archiesteel

    Joined:
    Jun 29, 2015
    Posts:
    15
    FYI the workaround doesn't work 100% (version 5.4.2). It loops at the correct place, but there is a small audible "tick" when it does, while in the editor and standalone/mobile builds it is seamless.

    You can hear an example of this in this game (New game --> Choose Avatar --> Skip Tutorial):

    http://www.mohawkironworkers.com/rivetrampage/

    It helps to lower the SFX volume in the pause menu to hear the tick better. Anyway, it's not a huge deal, but I do remember it working seamlessly in an earlier engine version. Maybe version 5.5 will fix it.

    Cheers!
     
  9. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    Any upcoming fix for one shots in Chrome only playing through the right-hand speaker?
    Also these one shots seems to cackle in Chrome as well.
     
  10. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Have you built on Windows? It would be good to try Mac since we have a different audio asset import pipeline.
     
  11. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    Yes, built on Windows 10. Are you saying building on Mac fixes this issue?
     
  12. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    I don't know how the webgl audio pipeline is "in general" on Windows, but if you use mono audio files, you should only ever build on a mac. Currently there's a bug with Unity and webgl on windows where it will convert your mono files to stereo (greatly reducing the quality). We only build webgl on the mac for this reason. Bonus points if you use the cache server in which case building on a mac is not good enough (a Windows computer could have polluted the cache), in this situation you need to reimport audio every time you build (or build asset bundles in our case)! :(

    And to be a little more on topic: I'm pretty sure this is the same bug but with 48khz (non-44.1khz?) audio files setting the time from script does not function correctly. In webgl it will always set the time slightly off (having unity resample the audio files to 44.1khz fixes the issue).
     
    Meltdown likes this.
  13. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    The files I am having the issue with are not in mono. That's the thing. I've re-exported them from Studio One and they are certainly not mono. And yes I've tried saving them at different sample rates, and it doesn't make a difference.

    Why this only happens on Chrome I have no idea, all other browsers are fine.
     
  14. sirrus

    sirrus

    Joined:
    Jun 10, 2012
    Posts:
    250
    I know this thread is a bit old but this is still an issue for us in 5.3.7p3. If it's fixed in 5.5, that's great, but we're not in a position to upgrade at the moment. Can I still submit a bug report?
     
  15. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    As far as I can tell all 5.5 does is disallow you from setting the sample rate (forces 44.1khz). Forcing 44.1khz fixed our audio timing issues in 5.4.x. My only suggestion would be is ensure your asset bundles were actually updated after you forced 44.1khz (or maybe try a simple test without asset bundles).
     
  16. sirrus

    sirrus

    Joined:
    Jun 10, 2012
    Posts:
    250
    This is without assetbundles altogether. Just a blank scene with 2 audio sources each with a clip that loops. One works great, the other doesnt (both sound fine in the editor).

    (edit) Both are forced to 44.1khz also.
     
  17. angralon

    angralon

    Joined:
    Feb 25, 2014
    Posts:
    9
    I still have this in Unity 5.5.1f1

    I activate my GameObject with the AudioSource via script. But the audio then is only played once and doesn't loop at all.
     
  18. rahimaga

    rahimaga

    Joined:
    Mar 25, 2019
    Posts:
    8
  19. adam05jazz

    adam05jazz

    Joined:
    Jun 27, 2018
    Posts:
    1
    I'm having the exact same issue in 2020.2.2f1. Any progress on a reliable solution!?
     
  20. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    273
    @adam05jazz We've been fixing a lot of audio bugs and backporting them to 2020.3, could you try upgrading to the latest 2020.3 and seeing if you're still seeing this problem?
     
  21. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    386
    Hello, I just tested with Unity 2022.1.2 and there is always the looping problem with WebGL.
    I can upload my test case if you want: there is no script, only an audio source and a sample.
    Regards
     
  22. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    273
    @BachmannT a test case would be great! :) I'll also forward this to our engineer working on the audio bugs.
     
    JimPlamondon likes this.
  23. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    386
    Hello, wonderful!
    Bellow a link to download the package:
    https://1drv.ms/u/s!Ao-u2_LaRuYimcsoq6ZplfxDQ4_MTw?e=pIYdjw
    There is two samples the original and the same but resampled at 44.1 khz with wavosaur.
    The two samples have the same behavior: loop start and loop end are correctly taken into account when playing in Unity editor (and Windows or MacOS app) but not when playing in a WebGL app. The sound always loops at the start of the sample.

    Tested with 2022.1.2.

    Thanks
    Regards
     
    Last edited: Aug 17, 2022
  24. JimPlamondon

    JimPlamondon

    Joined:
    Jul 8, 2022
    Posts:
    3
    Ruba --

    Upvote for the urgency of fixing this bug.

    I'm developing interactive components for online music education courseware using Thierry's Maestro MIDI Toolkit, and this bug kills my components. So long as this bug isn't fixed, I can't share my Web-based prototypes to get feedback on them...and I really need that feedback!

    Thanks for your attention to fixing this bug!

    Respectfully,

    Jim Plamondon
     
  25. MarcelPursche

    MarcelPursche

    Unity Technologies

    Joined:
    Mar 3, 2021
    Posts:
    45
  26. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    386
    Hello Marcel,
    We got a good rate vote for this issue and now it’s active. Good news!
    If you want, I could help or support your team in charge of this issue.
    We are really awaiting for a solution ! Unity Web application is a genuine discrimination in comparison with other platforms.
    Thanks a lot.
    Thierry
     
  27. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    386
    Hello
    No news about this issue? I see there is always the issue in Unity 2023.
    Regards
    Thierry
     
  28. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    386
  29. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    410
    Hi sir! I do have good news in fact!

    My fix has been approved this week, it should land in the latest version and be backported after that. I can't provide datea, but know that some latency :rolleyes: was added in the mix :rolleyes: because we've been migration the audio engine versioning system. We should be up to speed pretty soon.

    Cheers!
     
    cihadturhan_unity likes this.
  30. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    386
    You are my hero! Thanks