SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

mediaSync

Why JS: seeking a media element in place and marking what plays need its API.

tsx
import { mediaSync } from "@sdxc/ui/mixins";
Centering 3 kgLesson 7 · 11:43
  1. Today it's three kilos, which changes how you sit.
  2. Squeeze from the base and let the clay rise into a cone.
  3. Now press it back down with the heel of your hand.
  4. Lock your elbow against your thigh; the wheel does the work.
  5. Rest one finger on the side. If it doesn't move, you're centred.
View code
tsx
// VideoPlayer's island runs mediaSync's seeking and marks on its chapters and transcript.
<VideoPlayer duration={703} chapters={chapters} transcript={transcript}>
	<VideoPlayer.Screen label="Centering 3 kg" captionsLabel="English" chaptersLabel="Chapters">
		<VideoPlayer.Source src="/lesson-2160.webm" type="video/webm" label="2160p" badge="4K" />
		<VideoPlayer.Source src="/lesson-1080.webm" type="video/webm" label="1080p" badge="HD" />
		<VideoPlayer.Source src="/lesson.mp4" type="video/mp4" />
		<VideoPlayer.Controls label="Video controls">
			<VideoPlayer.Progress label="Seek" />
			<VideoPlayer.Play label="Play" pauseLabel="Pause" />
			<VideoPlayer.Time />
			<VideoPlayer.CaptionsToggle label="Subtitles/closed captions" />
			<VideoPlayer.Settings label="Settings">
				<VideoPlayer.SettingsToggle name="ambient" label="Ambient mode" checked />
				<VideoPlayer.CaptionsMenu label="Subtitles/CC" offLabel="Off" backLabel="Back" />
				<VideoPlayer.SettingsMenu name="sleep" label="Sleep timer" backLabel="Back">
					<VideoPlayer.SettingsOption value="off" checked>Off</VideoPlayer.SettingsOption>
					<VideoPlayer.SettingsOption value="15">15 minutes</VideoPlayer.SettingsOption>
					<VideoPlayer.SettingsOption value="end">End of video</VideoPlayer.SettingsOption>
				</VideoPlayer.SettingsMenu>
				<VideoPlayer.SpeedMenu label="Playback speed" normalLabel="Normal" backLabel="Back" />
				<VideoPlayer.QualityMenu label="Quality" backLabel="Back" />
			</VideoPlayer.Settings>
			<VideoPlayer.Theater label="Theater mode" />
			<VideoPlayer.Fullscreen label="Full screen" />
		</VideoPlayer.Controls>
	</VideoPlayer.Screen>
	<VideoPlayer.Scrubber />
	<VideoPlayer.Caption meta="Lesson 7 · 11:43">Centering 3 kg</VideoPlayer.Caption>
	<VideoPlayer.Side>
		<VideoPlayer.Chapters label="Chapters" />
		<VideoPlayer.Transcript label="Transcript" />
	</VideoPlayer.Side>
</VideoPlayer>

Usage

Makes a player's a[data-start] time links seek and play its first <audio> or <video>, and marks the playing item of each marks list, as installMediaSync does. The player is the host's closest root match; the host is usually a hidden marker in the player's island.

Signature

typescript
mediaSync(options: MediaSync.MixinOptions): MixinDescriptor<HTMLElement>

Returns a mixin descriptor for an element inside the player.

Parameters

ParameterTypeRequiredDescription
optionsMediaSync.MixinOptionsYesThe player's root selector and the lists to mark.

Examples

tsx
<span hidden mix={[mediaSync({ root: '[data-slot="episode"]', marks: ["[data-cue]"] })]} />

The events, constants and types its module publishes, each imported from @sdxc/ui/mixins too.

Function

markPlaying

Sets aria-current="true" on the last item of a playing-order list whose start is at or before time, and removes it from the rest, writing only what changes.

typescript
markPlaying(items: Iterable<HTMLElement>, time: number): HTMLElement | undefined
ParameterTypeRequiredDescription
itemsIterable<HTMLElement>YesThe list's items in playing order, each with data-start or data-cue.
timenumberYesThe media's current time in seconds.
Function

installMediaSync

Keeps a player's time links and marks in step with its media until signal aborts: a plain click on an a[data-start] seeks and plays, a modified click still opens the file, and every seek and time update marks the item playing in each list.

typescript
installMediaSync(root: HTMLElement, media: HTMLMediaElement, signal: AbortSignal, options: MediaSync.Options): void
ParameterTypeRequiredDescription
rootHTMLElementYesThe player's root, which holds the links and the lists.
mediaHTMLMediaElementYesThe <audio> or <video> the links seek.
signalAbortSignalYesEnds the listeners when it aborts.
optionsMediaSync.OptionsYesThe lists to mark, and what to run after each update.
tsx
installMediaSync(root, audio, signal, { marks: ['[data-slot="chapters"] a'] });
Interface

MediaSync.Options

What one player's sync marks and reports.

MemberTypeRequiredDescription
marksreadonly string[]YesSelectors, each matching one playing-order list under the player whose items carry their start in data-start or data-cue; the item playing carries aria-current="true".
onTime(time: number, changed: HTMLElement[]) => voidNoRuns after every seek and time update, with the items that newly became current.
Interface

MediaSync.MixinOptions

Configuration accepted by mediaSync.

MemberTypeRequiredDescription
rootstringYesSelector of the player's root, the closest ancestor of the host that matches.
@sdxc/ui

Can’t find it? Contact us