Bug with async loading sounds in html5

Monkey Forums/Monkey Bug Reports/Bug with async loading sounds in html5

Timotron(Posted 2014) [#1]
Ran into a bug with async loading sounds in html5, using monkey build 78h

Effectively sounds that are sent off to load async will sometimes/ often disappear and never report back fail or success.

I created a test, that shows a list of sounds being async loaded from my server, where I am limiting the number of async sounds being loaded to 20 at a time and displaying which ones are loading.

Note: The result of the code will vary each time you restart the test, sometimes no sounds will report back and sometimes most of the list will load correctly.




nikoniko(Posted 2014) [#2]
Do you try other browser for test?

It may be limitation of Monkey, browser, server, your inet connection.


devolonter(Posted 2014) [#3]
It seems something has changed in the latest Chrome versions. The canplaythrough and error events will never be fired if you load media files right after page loading… Quick search didn’t give any results.

But you can try to fix this issue with this ugly hack. Replace audio.load(); line in the modules /mojo/native/asyncsoundloader.js file by the following:
setTimeout(function() {
	audio.load();
	audio.volume=0;
	audio.play();
}, 250);


It works for me. Hope that it might help you too.


devolonter(Posted 2014) [#4]
OK, after some research I’ve found the solution.

That should work:
BBAsyncSoundLoaderThread.prototype.Start=function(){

	this._sample=null;
	if( !this._device.okay ) return;
	
	var audio=new Audio();
	if( !audio ) return;
	
	var thread=this;
	
	thread._sample=null;
	thread._result=false;
	thread._running=true;

	audio.src=BBGame.Game().PathToUrl( this._path );
	audio.preload='auto';	
	
	var success=function( e ){
		thread._sample=new gxtkSample( audio );
		thread._result=true;
		thread._running=false;
		audio.removeEventListener( 'canplaythrough',success,false );
		audio.removeEventListener( 'error',error,false );
	}
	
	var error=function( e ){
		thread._running=false;
		audio.removeEventListener( 'canplaythrough',success,false );
		audio.removeEventListener( 'error',error,false );
	}
	
	audio.addEventListener( 'canplaythrough',success,false );
	audio.addEventListener( 'error',error,false );
	
	//additional check
	var timer=setInterval( function() {	
		var buffered=audio.buffered;
		var duration=audio.duration;

		if ( buffered.length > 0 ) {
			if ( buffered.end(0)>=duration-1 ) {
				clearInterval( timer );
			}
		} else if ( !thread._running ) {
			clearInterval( timer );
		}
	}, 200 );
	
	audio.load();
}



marksibly(Posted 2014) [#5]
Woah, very weird bug!

Devolonter's fix works here, although it doesn't appear to actually do anything - it just reads some values!

In fact, I can reduce it to...

var timer=setInterval( function(){
		if( !thread._running ) clearInterval( timer );
	},200 );


...and it still seems to fix the problem. Does it for you guys too?

This suggests that Chrome has some kind of bug where it's forgetting to poll something somewhere, or ignoring an event, and the timer's just giving it a bit of a 'wake up and check again' poke.

Another fix is to stick this at the top:

#HTML5_WEBAUDIO_ENABLED=True

Pleased to say the new webaudio stuff worked first time!


devolonter(Posted 2014) [#6]
Yep, the reduced version works fine for me too!

BTW. It seems that Web Audio is a preferred way for sound stuff in modern browsers (and mobile too). Probably it makes sense to turn #HTML5_WEBAUDIO_ENABLED to True by default?


Timotron(Posted 2014) [#7]
I will give the web audio a spin tomorrow, thank you for your time.

Tim


marksibly(Posted 2014) [#8]
> Probably it makes sense to turn #HTML5_WEBAUDIO_ENABLED to True by default?

I would love to do this, and eventually rip out the crappy 'media player' code too.

I'm just not sure how widely adopted it is...will run a few tests here.

[edit]
Webaudio appears to be supported on Chrome, Firefox, Safari, Opera (and works really well!)...but not (surprise!) IE11.
[/edit]

Think I'll still enable webaudio by default though, but leave the crappy media player stuff in there as a fallback for IE11. And I guess there may be other browsers that don't yet support webaudio...


impixi(Posted 2014) [#9]
Interesting. Even the mobile browsers apparently support Web Audio API these days:

http://caniuse.com/audio-api