Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions app/components/media-session.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { PlaybackMedia } from "./playback-audio";
import { useEffect, useEffectEvent } from "react";

type QueueControls = {
previousTrack?: () => void;
nextTrack?: () => void;
};

export function attachMediaSession(
media: PlaybackMedia,
metadata: MediaMetadataInit,
onError: () => void,
onPlay: () => Promise<void> = () => media.play(),
onPause: () => void = () => media.pause(),
{ previousTrack, nextTrack }: QueueControls = {},
) {
if (!("mediaSession" in navigator)) return () => {};
const session = navigator.mediaSession;
Expand All @@ -27,8 +33,8 @@ export function attachMediaSession(
seekto: null,
seekbackward: null,
seekforward: null,
previoustrack: null,
nexttrack: null,
previoustrack: previousTrack ?? null,
nexttrack: nextTrack ?? null,
};
const setHandlers = (clear = false) => {
for (const [action, handler] of Object.entries(handlers)) {
Expand All @@ -46,7 +52,9 @@ export function attachMediaSession(
clearPosition();
setHandlers();
const updateState = () => {
session.metadata = media.error ? null : description;
// Reassigning unchanged metadata can make WebKit reload the artwork.
const current = media.error ? null : description;
if (session.metadata !== current) session.metadata = current;
session.playbackState = media.error
? "none"
: !media.paused && !media.ended
Expand Down Expand Up @@ -84,10 +92,15 @@ export function useMediaSession(
onError: () => void,
onPlay: () => Promise<void>,
onPause?: () => void,
{ previousTrack, nextTrack }: QueueControls = {},
) {
const fail = useEffectEvent(onError);
const play = useEffectEvent(onPlay);
const pause = useEffectEvent(() => (onPause ? onPause() : media?.pause()));
const previous = useEffectEvent(() => previousTrack?.());
const next = useEffectEvent(() => nextTrack?.());
const canPrevious = !!previousTrack;
const canNext = !!nextTrack;
const { src, sizes, type } = artwork ?? {};
useEffect(() => {
if (!media || title === undefined) return;
Expand All @@ -97,6 +110,10 @@ export function useMediaSession(
() => fail(),
() => play(),
() => pause(),
{
previousTrack: canPrevious ? () => previous() : undefined,
nextTrack: canNext ? () => next() : undefined,
},
);
}, [media, title, artist, album, src, sizes, type]);
}, [media, title, artist, album, src, sizes, type, canPrevious, canNext]);
}
8 changes: 8 additions & 0 deletions app/components/release-player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ function ReleasePlayer() {
},
() => playback.fail(),
playback.play,
undefined,
{
previousTrack: playback.previousTrack,
nextTrack:
queue && index + 1 < queue.tracks.length
? playback.nextTrack
: undefined,
},
);
if (!queue && !error) return null;
return (
Expand Down
2 changes: 1 addition & 1 deletion docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Use synthetic media and disposable accounts. Test listeners in a separate/privat

On a physical iPhone with HTTPS and the configured `APP_URL`, check uploads, actual audio, repeat pinch/pan/hold gestures, narrow layouts, downloads, background playback, interruptions, automatic release progression, and lock-screen resume. Verify title/artist/artwork updates, a demo without artwork, release cover replacement, and metadata cleanup after errors or leaving the player. Public navigation keeps the media session; private covers still require authorization. Browser automation cannot replace these device checks.

For lock-screen controls, verify title/artist/artwork and play/pause on the physical iPhone. The worklet player should have no reported elapsed time or progress timeline and no seek/previous/next commands. Check pause/resume after app switches, lock/unlock, track changes, repeat boundaries, production/stage navigation, interruptions and reaching the queue end. In-app waveform and slider seeking, repeat and automatic release progression must continue to work with the production worklet engine. Confirm that a previous player's timeline is cleared and errors, revocation and player exit clear metadata/handlers. `media-session` tests cover metadata, play/pause, position clearing and the absence of time reports and seeking actions. iOS controls the actual layout; native fallback elements may still expose a browser-owned timeline.
For lock-screen controls, verify title/artist/artwork and play/pause on the physical iPhone. The worklet player should have no reported elapsed time or progress timeline and no seek commands. Release Previous restarts after three seconds or goes to the preceding queued track near the start; Next is unavailable at the queue end; demos show no track navigation. Check pause/resume after app switches, lock/unlock, track changes, repeat boundaries, production/stage navigation, interruptions and reaching the queue end. In-app waveform and slider seeking, repeat and automatic release progression must continue to work with the production worklet engine. Confirm that a previous player's timeline is cleared and errors, revocation and player exit clear metadata/handlers. `media-session` tests cover metadata, play/pause, queue navigation, position clearing and the absence of time reports and seeking actions. iOS controls the actual layout; native fallback elements may still expose a browser-owned timeline.

On macOS Safari, verify the shared media-stream output with built-in speakers: audible pause and forward/backward seeks, repeat, source changes, playback across navigation, several minutes in a background tab, and interruption/resume. Normalization remains upstream of this output. Mobile Safari (including iPad desktop identity) and other browsers retain the direct Web Audio destination. Check their existing playback journeys too.

Expand Down
2 changes: 1 addition & 1 deletion docs/wavecase-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ The waveform is 220 pixels high, or 110 pixels on screens up to 600 pixels wide,

Comment time fields use minutes:seconds with optional hundredths, converted to numeric seconds on submission. Comment/reply/download dialogs trap focus, support Escape/close, and return focus to their trigger. A comment retains its selected version/thread while open and closes after successful submission.

Media Session provides title, artist, available artwork and play/pause only. Clear any previously published position state when attaching or leaving a player. Do not publish duration, position, playback rate, artificial rates or playback-state re-announcements. Do not register seek or previous/next track handlers. In-app waveform/slider seeking, repeat, automatic release progression, sample-accurate worklet playback, normalization and existing analytics remain available. Update metadata when tracks change; clear metadata and handlers on exit, failure or learned revocation. Private artwork keeps its access checks. The platform controls the final lock-screen layout: verify that the worklet path shows play/pause without a progress timeline on the physical iPhone; native fallback elements may expose their own browser-owned timeline.
Media Session provides title, artist, available artwork, play/pause and, for releases, previous/next track. Clear any previously published position state when attaching or leaving a player. Do not publish duration, position, playback rate, artificial rates or playback-state re-announcements. Do not register seek handlers. Release controls use the existing queue: Next selects the following queued track and is unavailable at the end; Previous restarts after three seconds or selects the preceding queued track near the beginning. Navigation preserves playing/paused state and never wraps or extends the queue. Demos register no track navigation. In-app waveform/slider seeking, repeat, automatic release progression, sample-accurate worklet playback, normalization and existing analytics remain available. Update metadata when tracks change; clear metadata and handlers on exit, failure or learned revocation. Private artwork keeps its access checks. The platform controls the final lock-screen layout: verify that the worklet path shows play/pause without a progress timeline on the physical iPhone; native fallback elements may expose their own browser-owned timeline.

### Analytics

Expand Down
51 changes: 51 additions & 0 deletions tests/media-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,57 @@ test("lock-screen controls publish metadata and play/pause without track time or
);
});

test("release queue controls use the shared player and disable next at the queue end", async (t) => {
const { session, handlers } = browserSession(t);
let assignments = 0;
let current = session.metadata;
Object.defineProperty(session, "metadata", {
get: () => current,
set: (value: MediaMetadataInit | null) => {
assignments++;
current = value;
},
});
const media = new Media();
let previous = 0;
let next = 0;
const attach = (canNext: boolean) =>
attachMediaSession(
media as unknown as HTMLAudioElement,
{ title: "Release track" },
() => {},
() => media.play(),
undefined,
{
previousTrack: () => {
previous++;
},
nextTrack: canNext
? () => {
next++;
}
: undefined,
},
);
const stop = attach(true);
handlers.get("previoustrack")!({ action: "previoustrack" });
handlers.get("nexttrack")!({ action: "nexttrack" });
assert.equal(previous, 1);
assert.equal(next, 1);
for (const action of ["seekto", "seekbackward", "seekforward"])
assert.equal(handlers.get(action), null);
assignments = 0;
await media.play();
media.pause();
assert.equal(assignments, 0, "unchanged metadata is not reassigned");
stop();
const stopLast = attach(false);
assert.equal(handlers.get("nexttrack"), null);
assert.ok(handlers.get("previoustrack"));
stopLast();
assert.ok([...handlers.values()].every((handler) => handler === null));
});

test("media failure and revocation clear private metadata and remote controls", async (t) => {
const { session, handlers, metadata } = browserSession(t);
const media = new Media();
Expand Down
Loading