Skip to content

Commit

Permalink
Fix pause_soundfile example in Safari by calling getAudioContext().re…
Browse files Browse the repository at this point in the history
…sume() on user interaction before any other audio code
  • Loading branch information
oshoham committed Aug 21, 2019
1 parent f0b6713 commit b32e460
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions examples/pause_soundfile/sketch.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// ====================
// DEMO: pause sound when the user presses a key, resume on release
// ====================
'use strict';

var soundFile;
var audioContextStarted = false;

function preload() {
// create a SoundFile
Expand All @@ -14,16 +16,26 @@ function setup() {
createCanvas(400, 400);
background(0, 255, 0);

soundFile.loop();
userStartAudio().then(function() {
soundFile.loop();
audioContextStarted = true;
});

createP('Press any key to pause. Resume when the key is released')
}

function keyTyped() {
soundFile.pause();
background(255, 0, 0);
if (!audioContextStarted) {
return;
}
soundFile.pause();
background(255, 0, 0);
}

function keyReleased() {
soundFile.play();
background(0, 255, 0);
if (!audioContextStarted) {
return;
}
soundFile.play();
background(0, 255, 0);
}

0 comments on commit b32e460

Please sign in to comment.