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

Commit

Permalink
fix audio ctx resume
Browse files Browse the repository at this point in the history
  • Loading branch information
slmjkdbtl committed Dec 29, 2023
1 parent 10b54b6 commit f27b222
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default (opt: {
fpsCounter: new FPSCounter(),
timeScale: 1,
skipTime: false,
isHidden: false,
numFrames: 0,
mousePos: new Vec2(0),
mouseDeltaPos: new Vec2(0),
Expand Down Expand Up @@ -133,6 +134,10 @@ export default (opt: {
return state.dt * state.timeScale
}

function isHidden() {
return state.isHidden
}

function time() {
return state.time
}
Expand Down Expand Up @@ -796,8 +801,10 @@ export default (opt: {
if (document.visibilityState === "visible") {
// prevent a surge of dt when switch back after the tab being hidden for a while
state.skipTime = true
state.isHidden = false
state.events.trigger("show")
} else {
state.isHidden = true
state.events.trigger("hide")
}
}
Expand Down Expand Up @@ -854,6 +861,7 @@ export default (opt: {
fps,
numFrames,
quit,
isHidden,
setFullscreen,
isFullscreen,
setCursor,
Expand Down
20 changes: 13 additions & 7 deletions src/kaboom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1188,19 +1188,27 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {

src.connect(audio.masterNode)

if (!opt.paused) {
// TODO: check debug.paused and tab hidden state
function resumeAudioCtx() {
if (debug.paused) return
if (app.isHidden() && !gopt.backgroundAudio) return
audio.ctx.resume()
}

function play() {
resumeAudioCtx()
el.play()
}

if (!opt.paused) {
play()
}

el.onended = () => onEndEvents.trigger()

return {

play() {
audio.ctx.resume()
el.play()
play()
},

seek(time: number) {
Expand All @@ -1224,8 +1232,7 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
if (p) {
el.pause()
} else {
audio.ctx.resume()
el.play()
play()
}
},

Expand Down Expand Up @@ -6166,7 +6173,6 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
function handleErr(err: Error) {

console.error(err)

audio.ctx.suspend()

// TODO: this should only run once
Expand Down

0 comments on commit f27b222

Please sign in to comment.