Search Unity

WebGL audio not looping correctly

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

  1. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,066
    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,653
    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:
    5,066
    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,633
    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,633
    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,436
    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,653
    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,436
    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,436
    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.
     
unityunity