I’m currently facing an issue with a project built using Unity WebGL. The problem arises specifically on browsers that use Webkit.
Despite my efforts to resume the AudioContext when the page becomes visible or when triggered by a user interaction (e.g., touch or click events), the audio remains suspended. I’ve tried implementing the usual workarounds, like triggering the AudioContext.resume() within direct user interactions and even adding it to button click events within the Unity UI, but none of these approaches seem to work consistently across all Webkit browsers.
I also came across a suggestion to request microphone access via navigator.mediaDevices.getUserMedia as a potential workaround, which indeed allows the audio to resume. However, this isn’t an ideal solution due to the unnecessary permissions request and the potential impact on user experience.
Is anyone else experiencing similar issues, or does anyone have alternative solutions or suggestions to ensure audio resumes properly on Webkit browsers? Any insights or advice would be greatly appreciated.
The affected devices are as follows:
iPhone 8: The AudioContext becomes ineffective, resulting in no sound.
iPhone XR: Occasionally, the AudioContext may become ineffective.
Resumed_AudioContext_j: function() {
var audioContext = Module.audioContext || new (window.AudioContext || window.webkitAudioContext)();
Module.audioContext = audioContext;
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
function unlockAudioContext() {
console.log(audioContext.state);
if (audioContext.state === 'suspended') {
audioContext.resume().then(() => {
console.log('AudioContext resumed successfully.');
console.log(audioContext.state);
AFaWebGame.SendMessage('AFaWAebGame', 'ResumedAudio_CallBack');
}).catch((error) => {
console.error('Failed to resume AudioContext:', error);
});
}
document.removeEventListener('touchstart', unlockAudioContext);
document.removeEventListener('touchend', unlockAudioContext);
}
console.log(audioContext.state);
document.addEventListener('touchstart', unlockAudioContext);
document.addEventListener('touchend', unlockAudioContext);
},
Tony Liao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.