@banou/media-player 0.10.26 → 0.10.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@
2
2
  import type { ReactNode } from 'react'
3
3
  import type { MediaChapter } from '../engine'
4
4
  import type { DownloadedRange, VolumeNormalizerControl } from './source-feature'
5
- import type { DelegatedTracks, ExternalThumbnails, PassThroughPictureInPicture, PlayerMedia } from './media'
5
+ import type { DelegatedSelection, DelegatedTracks, ExternalThumbnails, PassThroughPictureInPicture, PlayerMedia } from './media'
6
6
  import type { ExposePlayerOptions } from '../remote'
7
7
 
8
8
  import { useEffect, useMemo, useRef, useState } from 'react'
@@ -18,6 +18,7 @@ import { usePassThroughPictureInPicture } from './hooks/use-pass-through-picture
18
18
  import { useVolumeNormalizer } from './hooks/use-volume-normalizer'
19
19
  import { createPlaybackTap } from '../audio/playback-tap'
20
20
  import Chrome from './components/chrome'
21
+ import { languageOf } from '../language'
21
22
 
22
23
  /**
23
24
  * The source, read a range at a time: the player never downloads the whole file. Both fields travel
@@ -122,6 +123,20 @@ type CommonOptions = {
122
123
  */
123
124
  onSeek?: (fraction: number) => void
124
125
  onPlaybackError?: (error: unknown) => void
126
+
127
+ /**
128
+ * The viewer picked an audio track in the settings menu, named by its language as the file or the source names it:
129
+ * its tag (`jpn`, `en-US`), else its title or label (`Japanese`). What an app saves to pass back as
130
+ * `preferredAudioLanguage`; the player saves nothing. A track that names no language reports nothing, and on a media
131
+ * the player does not own the report waits for the source's switch to succeed.
132
+ */
133
+ onPreferredAudioLanguageChange?: (language: string) => void
134
+ /**
135
+ * As `onPreferredAudioLanguageChange`, for the subtitles menu, with null for subtitles turned off. A track that names
136
+ * no language reports only when it turns subtitles on, by its title or label (else `und`), so the next media starts
137
+ * with them on too.
138
+ */
139
+ onPreferredSubtitleLanguageChange?: (language: string | null) => void
125
140
  }
126
141
 
127
142
  /**
@@ -204,6 +219,19 @@ export type MediaPlayerLocalOptions =
204
219
  normalizeVolume?: boolean
205
220
  /** The viewer flipped the switch, to `normalizeVolume`: what an app saves to pass back as `normalizeVolume`. */
206
221
  onNormalizeVolumeChange?: (normalizeVolume: boolean) => void
222
+
223
+ /**
224
+ * The language a file starts its audio in, as `onPreferredAudioLanguageChange` reported it or as any tag or name
225
+ * (`ja`, `jpn`, `Japanese`). Its full track wins over a commentary, and a file with none plays its default. A
226
+ * track the viewer picks wins for that file. Read when a file starts, so a change applies from the next one.
227
+ */
228
+ preferredAudioLanguage?: string
229
+ /**
230
+ * As `preferredAudioLanguage`, for subtitles, where signs and songs or forced tracks count as partial and a file
231
+ * with none starts on its first track. null starts every file with subtitles off, and a preference that names no
232
+ * language starts on the track titled as it is.
233
+ */
234
+ preferredSubtitleLanguage?: string | null
207
235
  }
208
236
 
209
237
  /**
@@ -259,6 +287,11 @@ export type MediaPlayerRemoteOptions =
259
287
 
260
288
  export type MediaPlayerOptions = MediaPlayerLocalOptions | MediaPlayerRemoteOptions
261
289
 
290
+ // a label is read as a name, never as a tag
291
+ const trackOf = (selection: DelegatedSelection, id: string) => ({ title: selection.options.find((option) => option.id === id)?.label })
292
+
293
+ const afterSwitch = (result: void | Promise<void>, then: () => void) => result instanceof Promise ? result.then(then) : then()
294
+
262
295
  const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, children?: ReactNode }) => {
263
296
  // Narrowed on the field that carries the difference: a remote arm brings its own media and, by
264
297
  // construction, no bytes. Both halves stay null-safe so the engine hooks below can be called
@@ -434,15 +467,24 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
434
467
  // the document, and it renders the result itself.
435
468
  const subtitles = remote?.subtitles?.selection
436
469
  const audio = remote?.audioTracks?.selection
437
- // `select` is published as-is rather than wrapped, so its promise reaches the menu: the menu is the
438
- // only place that can hold the popover open, keep the tick off an unfinished switch, and show a
439
- // failure, and a promise nothing awaits is an unhandled rejection.
470
+ // `select`'s promise reaches the menu, which is the only place that can hold the popover open, keep the tick off an
471
+ // unfinished switch, and show a failure, and a promise nothing awaits is an unhandled rejection.
472
+ const reports = useRef({ audio: options.onPreferredAudioLanguageChange, subtitles: options.onPreferredSubtitleLanguageChange })
473
+ reports.current = { audio: options.onPreferredAudioLanguageChange, subtitles: options.onPreferredSubtitleLanguageChange }
440
474
  useEffect(() => {
441
475
  if (!subtitles) return
442
476
  setSourceState({
443
477
  subtitleTracks: subtitles.options.map(({ id, label, disabled }) => ({ id, label, disabled })),
444
478
  selectedSubtitleTrack: subtitles.selectedId ?? undefined,
445
- selectSubtitleTrack: (id) => subtitles.select(id == null ? null : String(id)),
479
+ selectSubtitleTrack: (id) => {
480
+ const picked = id == null ? null : String(id)
481
+ const track = picked === null ? undefined : trackOf(subtitles, picked)
482
+ const wasOff = subtitles.selectedId === null
483
+ return afterSwitch(subtitles.select(picked), () => {
484
+ const language = picked === null ? null : languageOf(track) ?? (wasOff ? track?.title : undefined)
485
+ if (language !== undefined) reports.current.subtitles?.(language)
486
+ })
487
+ },
446
488
  subtitleOffLabel: subtitles.offLabel,
447
489
  })
448
490
  }, [setSourceState, subtitles])
@@ -452,7 +494,10 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
452
494
  setSourceState({
453
495
  audioTracks: audio.options.map(({ id, label, disabled }) => ({ id, label, disabled })),
454
496
  selectedAudioTrack: audio.selectedId ?? undefined,
455
- selectAudioTrack: (id) => audio.select(String(id)),
497
+ selectAudioTrack: (id) => afterSwitch(audio.select(String(id)), () => {
498
+ const language = languageOf(trackOf(audio, String(id)))
499
+ if (language !== undefined) reports.current.audio?.(language)
500
+ }),
456
501
  })
457
502
  }, [setSourceState, audio])
458
503