Skip to content

Commit 39ceac4

Browse files
committed
fix: initialize audio context only with user interaction
1 parent cc22bfc commit 39ceac4

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/lib/audio/shared-audio-context.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@ import StartAudioContext from 'startaudiocontext';
22
import bowser from 'bowser';
33

44
let AUDIO_CONTEXT;
5-
if (!bowser.msie) {
6-
AUDIO_CONTEXT = new (window.AudioContext || window.webkitAudioContext)();
75

8-
StartAudioContext(AUDIO_CONTEXT);
6+
if (!bowser.msie) {
7+
/**
8+
* AudioContext can be initialized only when user interaction event happens
9+
*/
10+
const event =
11+
typeof document.ontouchend === 'undefined' ? 'mouseup' : 'touchend';
12+
const initAudioContext = () => {
13+
document.removeEventListener(event, initAudioContext);
14+
AUDIO_CONTEXT = new (window.AudioContext ||
15+
window.webkitAudioContext)();
16+
StartAudioContext(AUDIO_CONTEXT);
17+
};
18+
document.addEventListener(event, initAudioContext);
919
}
1020

1121
/**

0 commit comments

Comments
 (0)