CreateJS / SoundJS

A Javascript library for working with Audio. It provides a consistent API for loading and playing audio on different browsers and devices. Currently supports WebAudio, HTML5 Audio, Cordova / PhoneGap, and a Flash fallback.
http://createjs.com/
MIT License
4.42k stars 838 forks source link

Mobile Hybrid App: Audio not working (Android) #267

Closed pluginalan closed 7 years ago

pluginalan commented 7 years ago

This has taken me no less than three painful days to track down, so hopefully can help somebody else out.

When SoundJS/PreloadJS are operating in a hybrid app context using file:// protocol (most likely Android), calls to load XHR content succeed (i.e. the item is visible in Chrome debug tools network tab), but the response available to CreateJS is undefined. In SoundJS, this means that any sound will just 'fail to play' with a warning.

The root cause of this is because the xmlHttpRequest status response for file:// loaded content is '0', even for equivalent http code 200 (success). Internally, this is considered an error, according to the following in the _checkError functions (both SoundJS and PreloadJS):

switch (status) { case 0: case 404: // Not Found return new Error(status); }

I'v resolved in my own apps by changing this block as such:

switch (status) { case 0: console.warn("SoundJS encountered status code 0 for request:", this._request); return null; case 404: // Not Found return new Error(status); }

http:// contexts should never return 0 code; I can't think of any other context which would (except also a 'failed' load via file:// protocol - not sure how I'd handle that one without further study)

lannymcnie commented 7 years ago

This was fixed in SoundJS NEXT in March of last year. It was partially reverted because CORS issues with XHR cause this scenario. The current NEXT version (released soon) only treats status:0 as an error when on the http or https protocol.