Skip to content

Commit

Permalink
fix safari audio bug where error prevents leaving worldmap locations …
Browse files Browse the repository at this point in the history
…-- reveals another safari bug where decodeAudioData doesn't return a promise to start playing music immediately
  • Loading branch information
orsi committed Oct 9, 2020
1 parent d0a12ac commit 9d80499
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
22 changes: 20 additions & 2 deletions script/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ var AudioEngine = {
var fadeTime = AudioEngine._audioContext.currentTime + AudioEngine.FADE_TIME * 2;

// fade out event music and stop
if (AudioEngine._currentEventAudio) {
if (AudioEngine._currentEventAudio &&
AudioEngine._currentEventAudio.source &&
AudioEngine._currentEventAudio.source.buffer) {
var currentEventGainValue = AudioEngine._currentEventAudio.envelope.gain.value;
AudioEngine._currentEventAudio.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentEventAudio.envelope.gain.setValueAtTime(currentEventGainValue, AudioEngine._audioContext.currentTime);
Expand Down Expand Up @@ -212,10 +214,26 @@ var AudioEngine = {
return AudioEngine._getMissingAudioBuffer();
}

return AudioEngine._audioContext.decodeAudioData(buffer, function (decodedData) {
var decodeAudioDataPromise = AudioEngine._audioContext.decodeAudioData(buffer, function (decodedData) {
AudioEngine.AUDIO_BUFFER_CACHE[src] = decodedData;
return AudioEngine.AUDIO_BUFFER_CACHE[src];
});

// Safari WebAudio does not return a promise based API for
// decodeAudioData, so we need to fake it if we want to play
// audio immediately on first fetch
if (decodeAudioDataPromise) {
return decodeAudioDataPromise;
} else {
return new Promise(function (resolve, reject) {
var fakePromiseId = setInterval(function() {
if (AudioEngine.AUDIO_BUFFER_CACHE[src]) {
resolve(AudioEngine.AUDIO_BUFFER_CACHE[src]);
clearInterval(fakePromiseId);
}
}, 20);
});
}
});
}
},
Expand Down
27 changes: 9 additions & 18 deletions script/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,10 @@
});
}

// Disable this for now
// $('<span>')
// .addClass('volume menuBtn')
// .text(_('sound on.'))
// .click(() => Engine.toggleVolume())
// .appendTo(menu);

$('<span>')
.addClass('volume menuBtn')
.text(_('sound off.'))
.click(Engine.toggleVolume)
.text(_('sound on.'))
.click(() => Engine.toggleVolume())
.appendTo(menu);

$('<span>')
Expand Down Expand Up @@ -222,7 +215,7 @@
$.Dispatch('stateUpdate').subscribe(Engine.handleStateUpdates);

$SM.init();
// AudioEngine.init(); Disable this for now
AudioEngine.init();
Notifications.init();
Events.init();
Room.init();
Expand All @@ -246,24 +239,22 @@
Engine.triggerHyperMode();
}

// Disable this for now
// Engine.toggleVolume(Boolean($SM.get('config.soundOn')));
// if(!AudioEngine.isAudioContextRunning()){
// document.addEventListener('click', Engine.resumeAudioContext, true);
// }
Engine.toggleVolume(Boolean($SM.get('config.soundOn')));
if(!AudioEngine.isAudioContextRunning()){
document.addEventListener('click', Engine.resumeAudioContext, true);
}

Engine.saveLanguage();
Engine.travelTo(Room);

// Disable this for now
// setTimeout(notifyAboutSound, 3000);
setTimeout(notifyAboutSound, 3000);

},
resumeAudioContext: function () {
AudioEngine.tryResumingAudioContext();

// turn on music!
AudioEngine.setMasterVolume($SM.get('config.soundOn') ? 1.0 : 0.0, 0);
AudioEngine.setMasterVolume($SM.get('config.soundOn') ? 1.0 : 0.0, 0);

document.removeEventListener('click', Engine.resumeAudioContext);
},
Expand Down

0 comments on commit 9d80499

Please sign in to comment.