File tree Expand file tree Collapse file tree 2 files changed +40
-3
lines changed Expand file tree Collapse file tree 2 files changed +40
-3
lines changed Original file line number Diff line number Diff line change @@ -2,10 +2,22 @@ import StartAudioContext from 'startaudiocontext';
2
2
import bowser from 'bowser' ;
3
3
4
4
let AUDIO_CONTEXT ;
5
- if ( ! bowser . msie ) {
6
- AUDIO_CONTEXT = new ( window . AudioContext || window . webkitAudioContext ) ( ) ;
7
5
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 . ontouchstart === 'undefined' ?
12
+ 'mousedown' :
13
+ 'touchstart' ;
14
+ const initAudioContext = ( ) => {
15
+ document . removeEventListener ( event , initAudioContext ) ;
16
+ AUDIO_CONTEXT = new ( window . AudioContext ||
17
+ window . webkitAudioContext ) ( ) ;
18
+ StartAudioContext ( AUDIO_CONTEXT ) ;
19
+ } ;
20
+ document . addEventListener ( event , initAudioContext ) ;
9
21
}
10
22
11
23
/**
Original file line number Diff line number Diff line change
1
+ import 'web-audio-test-api' ;
2
+ import SharedAudioContext from '../../../src/lib/audio/shared-audio-context' ;
3
+
4
+ describe ( 'Shared Audio Context' , ( ) => {
5
+ const audioContext = new AudioContext ( ) ;
6
+
7
+ test ( 'returns empty object without user gesture' , ( ) => {
8
+ const sharedAudioContext = new SharedAudioContext ( ) ;
9
+ expect ( sharedAudioContext ) . toMatchObject ( { } ) ;
10
+ } ) ;
11
+
12
+ test ( 'returns AudioContext when mousedown is triggered' , ( ) => {
13
+ const sharedAudioContext = new SharedAudioContext ( ) ;
14
+ const event = new Event ( 'mousedown' ) ;
15
+ document . dispatchEvent ( event ) ;
16
+ expect ( sharedAudioContext ) . toMatchObject ( audioContext ) ;
17
+ } ) ;
18
+
19
+ test ( 'returns AudioContext when touchstart is triggered' , ( ) => {
20
+ const sharedAudioContext = new SharedAudioContext ( ) ;
21
+ const event = new Event ( 'touchstart' ) ;
22
+ document . dispatchEvent ( event ) ;
23
+ expect ( sharedAudioContext ) . toMatchObject ( audioContext ) ;
24
+ } ) ;
25
+ } ) ;
You can’t perform that action at this time.
0 commit comments