@banou/media-player 0.10.35 → 0.10.37

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.
@@ -19,7 +19,7 @@ import { useVolumeNormalizer } from './hooks/use-volume-normalizer'
19
19
  import { createPlaybackTap } from '../audio/playback-tap'
20
20
  import Chrome from './components/chrome'
21
21
  import { LabelsProvider } from './labels'
22
- import { languageOf } from '../language'
22
+ import { languageOf, playsIn } from '../language'
23
23
 
24
24
  /**
25
25
  * The source, read a range at a time: the player never downloads the whole file. Both fields travel
@@ -169,6 +169,11 @@ type CommonOptions = {
169
169
  * with them on too.
170
170
  */
171
171
  onPreferredSubtitleLanguageChange?: (language: string | null) => void
172
+ /**
173
+ * True when the viewer picked subtitles in the language the audio plays in: what an app saves to pass back as
174
+ * `alwaysShowSubtitles`, so the next media in it starts with them on too.
175
+ */
176
+ onAlwaysShowSubtitlesChange?: (always: boolean) => void
172
177
  }
173
178
 
174
179
  /**
@@ -254,8 +259,8 @@ export type MediaPlayerLocalOptions =
254
259
 
255
260
  /**
256
261
  * The language a file starts its audio in, as `onPreferredAudioLanguageChange` reported it or as any tag or name
257
- * (`ja`, `jpn`, `Japanese`). Its full track wins over a commentary, and a file with none plays its default. A
258
- * track the viewer picks wins for that file. Read when a file starts, so a change applies from the next one.
262
+ * (`ja`, `jpn`, `Japanese`). Only a full track counts, never a commentary or an audio description, and a file with
263
+ * none plays its original. A track the viewer picks wins for that file. Read when a file starts, so a change applies from the next one.
259
264
  */
260
265
  preferredAudioLanguage?: string
261
266
  /**
@@ -264,6 +269,17 @@ export type MediaPlayerLocalOptions =
264
269
  * language starts on the track titled as it is.
265
270
  */
266
271
  preferredSubtitleLanguage?: string | null
272
+ /**
273
+ * The language the media was made in, as its source knows it (`ja` for anime): a file with no full track in
274
+ * `preferredAudioLanguage` plays its full track in this one, else its default, its first track. Read as the
275
+ * preferences are.
276
+ */
277
+ originalLanguage?: string
278
+ /**
279
+ * False starts a file with subtitles off when its audio plays in `preferredSubtitleLanguage`, as it does when the
280
+ * viewer's own language is both. True, the default, starts them as `preferredSubtitleLanguage` says.
281
+ */
282
+ alwaysShowSubtitles?: boolean
267
283
  }
268
284
 
269
285
  /**
@@ -528,8 +544,8 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
528
544
  const audio = remote?.audioTracks?.selection
529
545
  // `select`'s promise reaches the menu, which is the only place that can hold the popover open, keep the tick off an
530
546
  // unfinished switch, and show a failure, and a promise nothing awaits is an unhandled rejection.
531
- const reports = useRef({ audio: options.onPreferredAudioLanguageChange, subtitles: options.onPreferredSubtitleLanguageChange })
532
- reports.current = { audio: options.onPreferredAudioLanguageChange, subtitles: options.onPreferredSubtitleLanguageChange }
547
+ const latest = useRef({ options, audio })
548
+ latest.current = { options, audio }
533
549
  useEffect(() => {
534
550
  if (!subtitles) return
535
551
  setSourceState({
@@ -541,7 +557,10 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
541
557
  const wasOff = subtitles.selectedId === null
542
558
  return afterSwitch(subtitles.select(picked), () => {
543
559
  const language = picked === null ? null : languageOf(track) ?? (wasOff ? track?.title : undefined)
544
- if (language !== undefined) reports.current.subtitles?.(language)
560
+ const now = latest.current
561
+ if (language !== undefined) now.options.onPreferredSubtitleLanguageChange?.(language)
562
+ const playing = now.audio && trackOf(now.audio, now.audio.selectedId ?? '')
563
+ if (language && playsIn(playing, language)) now.options.onAlwaysShowSubtitlesChange?.(true)
545
564
  })
546
565
  },
547
566
  subtitleOffLabel: subtitles.offLabel,
@@ -555,7 +574,7 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
555
574
  selectedAudioTrack: audio.selectedId ?? undefined,
556
575
  selectAudioTrack: (id) => afterSwitch(audio.select(String(id)), () => {
557
576
  const language = languageOf(trackOf(audio, String(id)))
558
- if (language !== undefined) reports.current.audio?.(language)
577
+ if (language !== undefined) latest.current.options.onPreferredAudioLanguageChange?.(language)
559
578
  }),
560
579
  })
561
580
  }, [setSourceState, audio])