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 16, 2019
1 parent ce48b1b commit 9c5f0ef
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions examples/pause_soundfile/sketch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ====================
// DEMO: pause sound when the user presses a key, resume on release
// ====================
'use strict';

var soundFile;

Expand All @@ -14,16 +15,35 @@ function setup() {
createCanvas(400, 400);
background(0, 255, 0);

soundFile.loop();
if (getAudioContext().state === 'running') {
soundFile.loop();
}

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

function touchStarted() {
if (getAudioContext().state !== 'running') {
getAudioContext().resume();
soundFile.loop();
}
}

function keyTyped() {
soundFile.pause();
background(255, 0, 0);
if (getAudioContext().state !== 'running') {
getAudioContext().resume();
soundFile.loop();
}
soundFile.pause();
background(255, 0, 0);
}

function keyReleased() {
if (getAudioContext().state !== 'running') {
getAudioContext().resume();
soundFile.loop();
} else {
soundFile.play();
background(0, 255, 0);
}
background(0, 255, 0);
}

0 comments on commit 9c5f0ef

Please sign in to comment.