Skip to content

Commit

Permalink
Fix battery drain from Web Audio
Browse files Browse the repository at this point in the history
  • Loading branch information
mbachry committed Feb 5, 2025
1 parent 4a381c2 commit 7593ffa
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/audio/BackgroundAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class BackgroundAudio {
private audioContext = createAudioContext();
private sounds: Record<string, AudioBuffer> = {};

public constructor() {
this.audioContext.suspend();

Check failure on line 23 in src/audio/BackgroundAudio.ts

View workflow job for this annotation

GitHub Actions / Jest (1)

RoomNotificationStateStore › Emits no event when a room has no unreads

TypeError: this.audioContext.suspend is not a function at new suspend (src/audio/BackgroundAudio.ts:23:27) at new LegacyCallHandler (src/LegacyCallHandler.tsx:171:31) at Function.get instance [as instance] (src/LegacyCallHandler.tsx:176:42) at VisibilityProvider.instance [as isRoomVisible] (src/stores/room-list/filters/VisibilityProvider.ts:38:31) at RoomNotificationStateStore.isRoomVisible [as emitUpdateIfStateChanged] (src/stores/notifications/RoomNotificationStateStore.ts:119:45) at EventEmitter.emitUpdateIfStateChanged (src/stores/notifications/RoomNotificationStateStore.ts:100:14) at Object.emit (test/unit-tests/stores/RoomNotificationStateStore-test.ts:40:16)

Check failure on line 23 in src/audio/BackgroundAudio.ts

View workflow job for this annotation

GitHub Actions / Jest (1)

RoomNotificationStateStore › Emits an event when a room has unreads

TypeError: this.audioContext.suspend is not a function at new suspend (src/audio/BackgroundAudio.ts:23:27) at new LegacyCallHandler (src/LegacyCallHandler.tsx:171:31) at Function.get instance [as instance] (src/LegacyCallHandler.tsx:176:42) at VisibilityProvider.instance [as isRoomVisible] (src/stores/room-list/filters/VisibilityProvider.ts:38:31) at RoomNotificationStateStore.isRoomVisible [as emitUpdateIfStateChanged] (src/stores/notifications/RoomNotificationStateStore.ts:119:45) at EventEmitter.emitUpdateIfStateChanged (src/stores/notifications/RoomNotificationStateStore.ts:100:14) at Object.emit (test/unit-tests/stores/RoomNotificationStateStore-test.ts:52:16)

Check failure on line 23 in src/audio/BackgroundAudio.ts

View workflow job for this annotation

GitHub Actions / Jest (1)

RoomNotificationStateStore › Emits an event when a feature flag changes notification state

TypeError: this.audioContext.suspend is not a function at new suspend (src/audio/BackgroundAudio.ts:23:27) at new LegacyCallHandler (src/LegacyCallHandler.tsx:171:31) at Function.get instance [as instance] (src/LegacyCallHandler.tsx:176:42) at VisibilityProvider.instance [as isRoomVisible] (src/stores/room-list/filters/VisibilityProvider.ts:38:31) at RoomNotificationStateStore.isRoomVisible [as emitUpdateIfStateChanged] (src/stores/notifications/RoomNotificationStateStore.ts:119:45) at EventEmitter.emitUpdateIfStateChanged (src/stores/notifications/RoomNotificationStateStore.ts:100:14) at Object.emit (test/unit-tests/stores/RoomNotificationStateStore-test.ts:63:16)
}

public async pickFormatAndPlay<F extends Array<keyof typeof formatMap>>(
urlPrefix: string,
formats: F,
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 7593ffa

Please sign in to comment.