File tree Expand file tree Collapse file tree 1 file changed +17
-5
lines changed Expand file tree Collapse file tree 1 file changed +17
-5
lines changed Original file line number Diff line number Diff line change 1
1
// ====================
2
2
// DEMO: pause sound when the user presses a key, resume on release
3
3
// ====================
4
+ 'use strict' ;
4
5
5
6
var soundFile ;
7
+ var audioContextStarted = false ;
6
8
7
9
function preload ( ) {
8
10
// create a SoundFile
@@ -14,16 +16,26 @@ function setup() {
14
16
createCanvas ( 400 , 400 ) ;
15
17
background ( 0 , 255 , 0 ) ;
16
18
17
- soundFile . loop ( ) ;
19
+ userStartAudio ( ) . then ( function ( ) {
20
+ soundFile . loop ( ) ;
21
+ audioContextStarted = true ;
22
+ } ) ;
23
+
18
24
createP ( 'Press any key to pause. Resume when the key is released' )
19
25
}
20
26
21
27
function keyTyped ( ) {
22
- soundFile . pause ( ) ;
23
- background ( 255 , 0 , 0 ) ;
28
+ if ( ! audioContextStarted ) {
29
+ return ;
30
+ }
31
+ soundFile . pause ( ) ;
32
+ background ( 255 , 0 , 0 ) ;
24
33
}
25
34
26
35
function keyReleased ( ) {
27
- soundFile . play ( ) ;
28
- background ( 0 , 255 , 0 ) ;
36
+ if ( ! audioContextStarted ) {
37
+ return ;
38
+ }
39
+ soundFile . play ( ) ;
40
+ background ( 0 , 255 , 0 ) ;
29
41
}
You can’t perform that action at this time.
0 commit comments