howler.js icon indicating copy to clipboard operation
howler.js copied to clipboard

elusive audio distortion bug ,because audioContext init is not expected correctly for the sampleRate is not set right on IOS

Open underbayan opened this issue 10 years ago • 24 comments

i also found a solution for this ,you can get answer here :https://github.com/Jam3/ios-safe-audio-context

underbayan avatar Jan 26 '16 05:01 underbayan

Are these issues related? https://www.scirra.com/forum/viewtopic.php?f=147&t=165414&p=1005298#p1005298

makdas avatar Jan 26 '16 09:01 makdas

I have had also an issue with audio completely distorted on iOS, when running in the PhoneGap port of my game esviji.

I'm currently using Howler.js 2.0.0 beta 7.

nhoizey avatar Jan 26 '16 10:01 nhoizey

Im also using howler but v1.1.29.... Are you having distortion on first launch of the app or any launch thereafter?

makdas avatar Jan 26 '16 11:01 makdas

Never on first launch, it seems, more after a while, after having switched to other apps momentarily.

nhoizey avatar Jan 26 '16 12:01 nhoizey

I have experienced this as well - one way I fixed this was to use appcache to explicitly tell the browser to always redownload the audio assets. This is an awful solution as it also means that the data usage goes up significantly for frequent users, but it fixed the problem where the sounds were all barbled with a really low bit rate.

Something else to note: Sounds that were not played before did not encounter this problem, only sounds that had previously been loaded. I'm not sure if they were also played, but the action of downloading them and then reloading the context where the asset would be cached causes this problem. I would bet this is a low level (obviously something outside of HowlerJS's jurisdiction) recovery problem where the bit rate variable is not saved with the audio asset, or it is, but it's ignoring the value.

DFortun81 avatar Jan 27 '16 20:01 DFortun81

Thanks @DFortun81 , can you give me a snippet of how you implemented appcache? Or better yet, how do i tell the appcache manifest file to re download the audio assets?

CACHE MANIFEST

NETWORK: mysoundfile.mp3

I also found that clearing the cache or using web inspector to refresh the app solved the problem

makdas avatar Jan 28 '16 08:01 makdas

Here's a sample appcache that I have for an example project for my coworkers. I have 3 graphics assets, a couple of audio files, and a bunch of javascript. I only want to cache the graphics assets for mobile. JavaScript can have frequent changes and so having those assets cache could have extremely bad effects on our game(s).

CACHE MANIFEST
# 2016-01-29 v1.0.0
CACHE:
icon.png
background.png
mycachedimage.png
NETWORK:
*
FALLBACK:

In your HTML file(s), set the html manifest like so:

<!DOCTYPE html>
<html manifest="cache.appcache">
    <head>
        ...

Using JavaScript, use the updateready event to reload the page context: (otherwise appcache does absolutely nothing)

// Check for an update to the application cache.
var appcache = window.applicationCache;
if(appcache) {
    appcache.addEventListener('updateready', function() {
      appcache.swapCache();  // The fetch was successful, swap in the new cache.
      window.location.reload();
    });
}

NOTE: If you are building your website for both desktop and mobile, make a mobile subdomain that only caches the graphics and have the desktop version also cache the audio as it doesn't have this problem. NOTE2: If you want to make an 'offline' version of your website, don't use the star notation (*) for the NETWORK section and only explicitly specify the audio assets. The example above was more for a game developer whose game needs a network connection to play.

DFortun81 avatar Jan 29 '16 14:01 DFortun81

Can anyone confirm if https://github.com/Jam3/ios-safe-audio-context fixes the issue? It would be trivial to implement into howler.js if so. Also, does this only happen in something like PhoneGap/Cordova or can it be replicated in the browser (I've been unable to replicate in browser, but maybe I'm doing something different).

goldfire avatar Jan 29 '16 20:01 goldfire

@goldfire I have not tried that particular hack yet, but I can pretty quickly merge it with my local copy to test if it works. I'll edit when I have completed my test.

EDIT: It seems to have fixed the problem!

DFortun81 avatar Jan 29 '16 20:01 DFortun81

I have the same issue (Howler 1.1.26 though) and the ios-safe-audio-context-fix didn't work for me, neither did the suggested cache.appcache method. Unless there's something that's been fixed in Howler itself since 1.1.26 that could cause the problem, the fix doesn't work.

Pattern seems to be:

  • Clear cache in Safari settings
  • Open Safari and your webapp - sound works.
  • Reload Safari - sound works.
  • But if you close Safari and open again - sound is distorted. Reload - sound works.
  • Close Safari and open and sound is distorted again.

This is the case 100% of the times tested (iPhone 5 (IOS 9.2) and on two different iPhone 6 (IOS 9.2 and IOS 9.2.1)). I am not able to reproduce the problem on a iPhone 5S (IOS 9.2.1) at all.

EDIT: Tested Buzz (which uses Audio tag), which seems to work flawlessly. A Howler method for enforcing Audio tag would be a nice feature since it already has Audio tag as a fallback, I think. That way it would at least be possible to do a quick'n'dirty-fix.

UPDATE: @goldfire It's an issue for regular web as well, btw :)

patrickfabrizius avatar Feb 01 '16 10:02 patrickfabrizius

The problem seems to have come back this morning. Not too sure why it was working flawlessly on Friday and Saturday after we put in that hack.

DFortun81 avatar Feb 01 '16 15:02 DFortun81

@DFortun81 can you confirm that my pattern holds?

patrickfabrizius avatar Feb 01 '16 15:02 patrickfabrizius

@patrickfabrizius That was most definitely related to how it worked. Settings -> Safari -> Clear History And Website Data to clear the browser cache for Safari.

EDIT: It seems as though the problem this morning isn't related to the hack. None of my code after the hack is run seems to be executing. Perhaps a null reference? I need to do some debugging. Will comment shortly.

EDIT2: Ah, that's weird. The AudioContext class is null and it's trying to execute the new command on it. Re-obfuscating and I'll get back to you all on whether the hack still works. Can't imagine it will if the context is null.

EDIT3: Yup. It still doesn't work.

DFortun81 avatar Feb 01 '16 15:02 DFortun81

hi, is there an example of the hack being implemented into howler?

makdas avatar Feb 02 '16 07:02 makdas

Does anyone have a basic test-case with reproducible steps?

goldfire avatar Feb 05 '16 18:02 goldfire

@goldfire I think you can take anything that uses Howler and then run thru the steps I've described above, it should highlight the problem.

patrickfabrizius avatar Feb 05 '16 19:02 patrickfabrizius

@patrickfabrizius Sorry, didn't see you had updated that post. I hadn't been force closing Safari, which is why I hadn't been able to get it to work until now. You can force HTML5 audio by setting buffer to true on 1.x and html5 to true on 2.0.

goldfire avatar Feb 05 '16 19:02 goldfire

Using 2.0 I'm able to consistently fix this issue by immediately calling Howler.unload() before setting up any new sounds. This destroys the current context and creates a new one, with the new one having a correct sampleRate of 44100. This is similar to what the above hack seems to be doing. Can anyone else confirm the same thing (the global unload is only on the 2.0 branch)?

goldfire avatar Feb 05 '16 19:02 goldfire

Based on my initial tests, adding the Howler.unload() before loading sounds fixed the distortion for iOS. I'll chime back if this changes with further testing.

Thanks for the working on this. This has been an annoying bug.

davidfig avatar Feb 06 '16 02:02 davidfig

Howler.unload() before loading sounds worked for me too.

viniciusbo avatar Feb 17 '16 19:02 viniciusbo