Skip to content

Commit

Permalink
fix: workaround for firefox not playing nice with audiocontext (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellvix authored Dec 2, 2024
1 parent 9316df5 commit dc59817
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/js/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@
*/
class Audio {
constructor() {
this.AudioContext = window['AudioContext'] || window['webkitAudioContext'];
this.audioContext = new AudioContext();
this.compressor = this.compressorSetup(this.audioContext);
this.fixAudioContext();
}

fixAudioContext() {
if (!this.audioContext) {
this.AudioContext =
window['AudioContext'] || window['webkitAudioContext'];
this.audioContext = new AudioContext();
this.compressor = this.compressorSetup(this.audioContext);
} else if (this.audioContext.state === 'suspended') {
this.audioContext.resume().then(() => {
console.log('AudioContext resumed');
});
}
}

/**
Expand Down Expand Up @@ -38,11 +49,6 @@ class Audio {
* Triggers playOscillator() with the correct parameters.
*/
playTone(params = null) {
// workaround for FF starting audio context in a suspended state
if (this.audioContext.state === 'suspended') {
this.audioContext.resume();
}

let currentDuration = constants.duration;
let volume = constants.vol;
if (params != null) {
Expand Down
4 changes: 4 additions & 0 deletions src/js/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ class Display {
this.announceText(resources.GetString('son_off'));
}
}

if (constants.sonifMode != 'off') {
audio.fixAudioContext();
}
}

/**
Expand Down

0 comments on commit dc59817

Please sign in to comment.