From 5e8534cfec073ef86db0971bd75261a28383b107 Mon Sep 17 00:00:00 2001 From: ilyak Date: Mon, 8 Feb 2021 20:48:35 +0300 Subject: [PATCH] webaudio: unlock audio context on user interaction --- miniaudio.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/miniaudio.h b/miniaudio.h index 1f178b70..0d718e4a 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -31613,6 +31613,26 @@ static ma_result ma_context_init__webaudio(ma_context* pContext, const ma_contex miniaudio.get_device_by_index = function(deviceIndex) { return miniaudio.devices[deviceIndex]; }; + + miniaudio.unlock_event_types = (function(){ + return ['touchstart', 'touchend', 'click']; + })(); + + miniaudio.unlock = function() { + for(var i = 0; i < miniaudio.devices.length; ++i) { + var device = miniaudio.devices[i]; + if (device != null && device.webaudio != null) { + device.webaudio.resume(); + } + } + miniaudio.unlock_event_types.map(function(event_type) { + document.removeEventListener(event_type, miniaudio.unlock, true); + }); + }; + + miniaudio.unlock_event_types.map(function(event_type) { + document.addEventListener(event_type, miniaudio.unlock, true); + }); } return 1;