From 7593ffa78800ca2ce60db3381ddd44e05406a68a Mon Sep 17 00:00:00 2001 From: Marcin Bachry Date: Wed, 5 Feb 2025 18:11:58 +0100 Subject: [PATCH] Fix battery drain from Web Audio --- src/audio/BackgroundAudio.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/audio/BackgroundAudio.ts b/src/audio/BackgroundAudio.ts index c90016eef91..bb1e5c27bf0 100644 --- a/src/audio/BackgroundAudio.ts +++ b/src/audio/BackgroundAudio.ts @@ -19,6 +19,10 @@ export class BackgroundAudio { private audioContext = createAudioContext(); private sounds: Record = {}; + public constructor() { + this.audioContext.suspend(); + } + public async pickFormatAndPlay>( urlPrefix: string, formats: F, @@ -48,6 +52,12 @@ export class BackgroundAudio { source.buffer = this.sounds[url]; source.loop = loop; source.connect(this.audioContext.destination); + + this.audioContext.resume(); + source.onended = () => { + this.audioContext.suspend(); + }; + source.start(); return source; }