phaserjs / phaser-ce

Phaser CE is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
http://phaser.io
MIT License
1.34k stars 491 forks source link

Error loading audio in Safari #588

Closed ferryhalim closed 5 years ago

ferryhalim commented 6 years ago

This Issue is about (pick one, ✏️ delete others)

samme commented 6 years ago

What's the error?

Are you sure the same example works in v2.10?

samme commented 6 years ago

https://codepen.io/samme/full/PELLvm/

ferryhalim commented 6 years ago

InvalidStateError: DOM Exception 11: An attempt was made to use an object that is not, or is no longer, usable.

Forgot to mention, this error appears on mobile Safari (tested on iPod Touch 6, iPad Air 2). Desktop Safari is unaffected.

Game works fine before upgrading. And it works fine if I replaced Phaser,js with old version 2.10. Then I tried using 2.11, but this time I replaced all the audio code with the code from 2.10 and the game works fine. So something in the 2.11 audio code breaks the audio loading for mobile Safari.

samme commented 6 years ago

Which v2.10?

samme commented 6 years ago

Where does the error come from?

InvalidStateError: DOM Exception 11: An attempt was made to use an object that is not, or is no longer, usable.

ferryhalim commented 6 years ago

Grabbing audio code from v2.10.2 and putting it in 2.11 fixed the problem.

I preload all audio files (including the background music) and then do a check in update to make sure all of them are decoded using isSoundDecoded. When all of them are decoded, I try to play the background music. That's when the error occured.

samme commented 6 years ago

What's the error trace?

pieshop commented 5 years ago

Same issue here when building with "phaser-ce": "2.13.2",

Reverting to "phaser-ce": "2.10.5" and the background audio plays fine. Will add more details.

safari_web_inspector

SBCGames commented 5 years ago

Same issue with Phaser 2.11.0 here too. Only on iOS + Safari. Error is thrown when I call play on background music (Phaser.Sound loaded in .m4a file format). More, game was tested on various iOS versions with these results: iOS 8.3, 12.4, 12.4.1 ... all OK, iOS 9.3.5 ... error (tested on two different devices by two different sponsors with the same result)

It is not easy to get some output from distant testers, but finally I got this (playMusic is where I call play on Phaser.Sound with background music). It is the same as @pieshop 's output.

 debug log: [Error] InvalidStateError: DOM Exception 11: An attempt was made to use an object that is not, or is no longer, usable.
   start (phaser.min.js:3:616567)
   _startSource (phaser.min.js:3:616567)
   play (phaser.min.js:3:612548)
   playMusic (futoshiki.js:3378)
   create (futoshiki.js:8478)
   loadComplete (phaser.min.js:3:249040)
   preUpdate (phaser.min.js:3:246466)
   updateLogic (phaser.min.js:3:292952)
   update (phaser.min.js:3:292377)
   updateRAF (phaser.min.js:3:512405)
   _onLoop (phaser.min.js:3:512284)

UPDATE: I found, that I already reported the same bug for specific combination Windows 10 Phone (Lumia 532) + Edge browser here: https://github.com/photonstorm/phaser-ce/issues/635 In that case I could test by myself and found, that problem was undefined parameter for duration when calling start() on AudioBufferSourceNode. In that case also last working Phaser version was 2.10.5.

I also found, that there are non-Phaser people having the same problem like this: https://stackoverflow.com/questions/34483229/webkit-webaudio-invalidstateerror-dom-exception-11-when-calling-buffersource-sta They say, problem started on iOS 9, which are error devices I tested it on. It looks, that newer versions of iOS do not have this problem.

samme commented 5 years ago

I can can change Phaser.Sound#_startSource() so it never passes undefined in the duration argument (instead passing only 2 args).

samme commented 5 years ago

I presume this fixes it but I don't have iOS 9 to test: phaser-ce-5565d69854cfebcb2448bd7dcd5051e8666b8b6f.js.zip

SBCGames commented 5 years ago

@samme tested at least on Windows 10 + Lumia (issue https://github.com/photonstorm/phaser-ce/issues/635) and it works well. I may get access to iOS 9 device next week. If yes, I will test and report results. At least, my older workaround is currently in process of testing by one of the sponsors, who reported freezes before. If it ends well, it will not test final solution, but will at least confirm, that problem was "undefined".

SBCGames commented 5 years ago

@samme I have confirmation, that it now works also on iOS 9 devices.