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.
Operation Process:
The current project is a WebGL game embedded in a WebView within Android and iOS apps.
1.Enter the WebView.
2.During initialization, I call Resumed_AudioContext_j() through jslib.
3.The user clicks the button to watch an ad, which triggers a jslib call to the native app to play the ad. At this point, the native side overlays the ad page on top of the WebView. After watching the ad, the user clicks the close button. At this time, I call Resumed_AudioContext_j() again when the user clicks, returning to the WebView page.
4.At this point, the game cannot play any sound, and the app must be completely closed and reopened.
5.If the user plays the game directly without watching the ad, the game can play sound normally.
6.This issue only occurs on certain iOS devices.
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);
UnityWebGame.SendMessage('WAebGame', 'Resumed_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);
},