Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Add support for WebKit not playing audio unless it is tied to a user interaction #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/morse-pro-player-waa.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class MorsePlayerWAA {
this.frequency = undefined;
this.startPadding = 0; // number of ms to wait before playing first note of initial sequence
this.endPadding = 0; // number of ms to wait at the end of a sequence before playing the next one (or looping)
this.openAudioContext = undefined; // Safari requires user interaction to play audio. You can set this to an audio context opened by user interaction so that morse code can be initiated by non-user events (network or a USB keyer)

this._cTimings = [];
this._isPlaying = false;
Expand All @@ -61,7 +62,7 @@ export default class MorsePlayerWAA {
* @access: private
*/
_initialiseAudioNodes() {
this.audioContext = new this.audioContextClass();
this.audioContext = this.openAudioContext || new this.audioContextClass();
this.splitterNode = this.audioContext.createGain(); // this node is here to attach other nodes to in subclass
this.lowPassNode = this.audioContext.createBiquadFilter();
this.lowPassNode.type = "lowpass";
Expand Down Expand Up @@ -235,7 +236,8 @@ export default class MorsePlayerWAA {
clearInterval(this._timer);
clearInterval(this._stopTimer);
clearInterval(this._startTimer);
this.audioContext.close();
// Don't close the audio context if we have one that was set by the client, since we may not be able to play audio again after that.
if (this.openAudioContext == null) this.audioContext.close();
this.soundStoppedCallback();
}
}
Expand Down