Skip to content

Build your personal knowledge base with Trilium Notes

client/audio player: fix some issues

Adorian Doranb255e6962928
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
index 491e711ca0..02d3c31959 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
+++ b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
@@ -339,13 +339,16 @@ export function useMediaSessionController(note: FNote, noteContext: NoteContext
media.removeEventListener("timeupdate", syncPositionState);
media.removeEventListener("seeked", syncPositionState);
// We no longer own the session (handover/hidden/unmount); clear so the OS overlay doesn't keep a
- // stale "playing" state. A new owner immediately sets its own.
- mediaSession.playbackState = "none";
- if (typeof mediaSession.setPositionState === "function") {
- try { mediaSession.setPositionState(); } catch { /* nothing to clear */ }
+ // stale "playing" state — but only if another player hasn't already taken the slot, otherwise this
+ // late cleanup would clobber the new owner's freshly-set state.
+ if (activeMediaPlayer === self || activeMediaPlayer === null) {
+ mediaSession.playbackState = "none";
+ if (typeof mediaSession.setPositionState === "function") {
+ try { mediaSession.setPositionState(); } catch { /* nothing to clear */ }
+ }
}
};
- }, [ ownsSession, mediaRef ]);
+ }, [ ownsSession, mediaRef, self ]);
// Auto-play the freshly-opened sibling once it can play (only when reached via navigation).
useEffect(() => {
diff --git a/apps/client/src/widgets/type_widgets/file/useAudioAnalyser.ts b/apps/client/src/widgets/type_widgets/file/useAudioAnalyser.ts
index ce7532e648..c0d23912cf 100644
--- a/apps/client/src/widgets/type_widgets/file/useAudioAnalyser.ts
+++ b/apps/client/src/widgets/type_widgets/file/useAudioAnalyser.ts
@@ -37,7 +37,16 @@ export function useAudioAnalyser(mediaRef: RefObject<HTMLMediaElement>): () => U
}
};
media.addEventListener("play", onPlay);
- return () => media.removeEventListener("play", onPlay);
+ return () => {
+ media.removeEventListener("play", onPlay);
+ // Release the audio resources on unmount: AudioContexts are limited (Chromium throws once too many
+ // are open), and a WeakMap entry alone never frees the underlying context.
+ const bundle = analyserCache.get(media);
+ if (bundle) {
+ void bundle.context.close();
+ }
+ analyserCache.delete(media);
+ };
}, [ mediaRef ]);
return useCallback(() => {