Skip to content

Commit b32e460

Browse files
committed
Fix pause_soundfile example in Safari by calling getAudioContext().resume() on user interaction before any other audio code
1 parent f0b6713 commit b32e460

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

examples/pause_soundfile/sketch.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// ====================
22
// DEMO: pause sound when the user presses a key, resume on release
33
// ====================
4+
'use strict';
45

56
var soundFile;
7+
var audioContextStarted = false;
68

79
function preload() {
810
// create a SoundFile
@@ -14,16 +16,26 @@ function setup() {
1416
createCanvas(400, 400);
1517
background(0, 255, 0);
1618

17-
soundFile.loop();
19+
userStartAudio().then(function() {
20+
soundFile.loop();
21+
audioContextStarted = true;
22+
});
23+
1824
createP('Press any key to pause. Resume when the key is released')
1925
}
2026

2127
function keyTyped() {
22-
soundFile.pause();
23-
background(255, 0, 0);
28+
if (!audioContextStarted) {
29+
return;
30+
}
31+
soundFile.pause();
32+
background(255, 0, 0);
2433
}
2534

2635
function keyReleased() {
27-
soundFile.play();
28-
background(0, 255, 0);
36+
if (!audioContextStarted) {
37+
return;
38+
}
39+
soundFile.play();
40+
background(0, 255, 0);
2941
}

0 commit comments

Comments
 (0)