Skip to content

Build your personal knowledge base with Trilium Notes

client/media player: start to auto play after previous/next

Adorian Doran86cab1abd336
diff --git a/apps/client/src/widgets/type_widgets/file/Audio.tsx b/apps/client/src/widgets/type_widgets/file/Audio.tsx
index 21715fb087..e3a8070195 100644
--- a/apps/client/src/widgets/type_widgets/file/Audio.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Audio.tsx
@@ -26,7 +26,7 @@ export default function AudioPreview({ note, noteContext }: { note: FNote, noteC
useEffect(() => setError(false), [note.noteId]);
const onError = useCallback(() => setError(true), []);
- const siblingNavigation = useMediaSiblingNavigation(note, noteContext, "audio/");
+ const siblingNavigation = useMediaSiblingNavigation(note, noteContext, "audio/", audioRef);
if (error) {
return <NoItems icon="bx bx-volume-mute" text={t("media.unsupported-format", { mime: note.mime.replace("/", "-") })} />;
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
index 31e166e323..f24d45d776 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
+++ b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
@@ -134,14 +134,40 @@ export function VolumeControl({ mediaRef }: { mediaRef: RefObject<HTMLVideoEleme
);
}
+/** Set when jumping to a sibling, so the media opened next auto-plays — like a playlist. */
+let autoPlayNextMedia = false;
+
/**
* Sibling navigation across the note's same-mime media siblings (e.g. all `video/` files in the parent),
- * with PageUp/PageDown wired to previous/next. Edge keys (Home/End) and Space are left to the player.
+ * with PageUp/PageDown wired to previous/next (Home/End and Space are left to the player). Jumping to a
+ * sibling auto-plays it once loaded, like a playlist.
*/
-export function useMediaSiblingNavigation(note: FNote, noteContext: NoteContext | undefined, mimePrefix: string) {
+export function useMediaSiblingNavigation(note: FNote, noteContext: NoteContext | undefined, mimePrefix: string, mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement>) {
const navigation = useSiblingNavigation(noteSiblingProvider(note, noteContext, { mimePrefix }));
- useSiblingKeyboard(navigation, noteContext, undefined, NO_KEYS, NO_KEYS, { edgeKeys: false });
- return navigation;
+ const wrapped = navigation && {
+ ...navigation,
+ navigatePrevious: () => { autoPlayNextMedia = true; navigation.navigatePrevious(); },
+ navigateNext: () => { autoPlayNextMedia = true; navigation.navigateNext(); }
+ };
+
+ useSiblingKeyboard(wrapped, noteContext, undefined, NO_KEYS, NO_KEYS, { edgeKeys: false });
+
+ // Auto-play the freshly-opened sibling once it can play (only when reached via navigation).
+ useEffect(() => {
+ if (!autoPlayNextMedia) return;
+ autoPlayNextMedia = false;
+ const media = mediaRef.current;
+ if (!media) return;
+ const play = () => { media.play().catch(() => {}); };
+ if (media.readyState >= media.HAVE_FUTURE_DATA) {
+ play();
+ } else {
+ media.addEventListener("canplay", play, { once: true });
+ return () => media.removeEventListener("canplay", play);
+ }
+ }, [ note.noteId, mediaRef ]);
+
+ return wrapped;
}
/** "Skip to previous/next sibling" control, styled like the other media buttons; renders nothing without siblings. */
diff --git a/apps/client/src/widgets/type_widgets/file/Video.tsx b/apps/client/src/widgets/type_widgets/file/Video.tsx
index aa501ed1ab..dc7afef490 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Video.tsx
@@ -39,7 +39,7 @@ export default function VideoPreview({ note, noteContext }: { note: FNote, noteC
}, [togglePlayback]);
const onKeyDown = useKeyboardShortcuts(videoRef, wrapperRef, togglePlayback, flashControls);
- const siblingNavigation = useMediaSiblingNavigation(note, noteContext, "video/");
+ const siblingNavigation = useMediaSiblingNavigation(note, noteContext, "video/", videoRef);
if (error) {
return <NoItems icon="bx bx-video-off" text={t("media.unsupported-format", { mime: note.mime.replace("/", "-") })} />;