@simba-dev/react-native-media-player 1.15.0 → 1.17.0

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.
@@ -22,7 +22,8 @@ import { loadAndPlay } from '../bridge/loadAndPlay';
22
22
  * - `artist` — `onPropertyChanged('metadata')` (parsed), hydration
23
23
  * - `album` — `onPropertyChanged('metadata')` (parsed), hydration
24
24
  * - `isPlaying` — `onPlaybackStateChanged`, `onEndFile`, `onPlaybackRestart`
25
- * - `positionMs` — 1Hz `bridge.getPosition()` poll + `onPositionChanged`
25
+ * - `positionMs` — `onPositionChanged` + hydration (the native event
26
+ * stream is authoritative; there is no position poll)
26
27
  * - `durationMs` — `onDurationChanged` + `onFileLoaded.file.duration` + hydration
27
28
  * - `isBuffering` — `onBuffering`
28
29
  * - `isSeeking` — `onSeeking`
@@ -50,6 +51,22 @@ export interface PlayerState {
50
51
  // ── V12 surface (preserved) ───────────────────────────────────────────
51
52
  /** True when mpv is actively playing (not paused, not stopped). */
52
53
  isPlaying: boolean;
54
+ /**
55
+ * True while a load is IN FLIGHT: the engine has been asked for a file
56
+ * and has not reported it yet. v1.17.0.
57
+ *
58
+ * This is the "the file is opening" fact, and it belongs to the lib
59
+ * because no consumer can derive it. mpv's core state is "playing"
60
+ * from the moment `loadfile` is called until the file has been
61
+ * parsed, so a phase derived from `isPlaying` can never tell
62
+ * "opening" from "playing" — and the playlist is not populated yet
63
+ * either, so "idle" is just as wrong.
64
+ *
65
+ * Device evidence (2026-10-20): a 9-second load over a throttled link
66
+ * showed NO loading indicator at all, because neither of the two
67
+ * available phases was the truth.
68
+ */
69
+ isLoading: boolean;
53
70
  /** Track title (from mpv `media-title` → launch title → "Simba Player"). */
54
71
  title: string;
55
72
  /** Track artist (from mpv `metadata/by-key/artist`). Empty when untagged. */
@@ -88,7 +105,19 @@ export interface PlayerState {
88
105
  currentChapter: MpvChapter | null;
89
106
  /** Video stream params (resolution, fps, codec). null for audio-only files. */
90
107
  videoParams: MpvVideoParams | null;
91
- /** Last error reported by mpv. Cleared on the next `onFileLoaded`. */
108
+ /**
109
+ * Last error reported by mpv. Cleared on the next `onFileLoaded`.
110
+ *
111
+ * `code` is the lib's stable NUMERIC code. It is never a string: the
112
+ * native payload used to carry one, so `Number("E_RENDERER_GONE")`
113
+ * was NaN and every failure reached the app with `code: NaN`.
114
+ * `codeName` is the readable string (`E_LOAD_FAILED`, …).
115
+ *
116
+ * `recoverable` means "PLAYBACK CONTINUED". false = the failure ended
117
+ * playback (mpv's END_FILE — the file failed). true = the engine
118
+ * complained but kept playing, so it is a degradation, not a failure.
119
+ * v1.17.0.
120
+ */
92
121
  error: { code: number; codeName?: string; recoverable: boolean; message: string } | null;
93
122
 
94
123
  /** Whether playlist shuffle is currently enabled. mpv `playlist-shuffle`. */
@@ -111,8 +140,11 @@ export interface PlaylistEntry {
111
140
  * `usePlayerProgress()` is separate from `usePlayer()` so consumers
112
141
  * that only need position/duration don't re-render on every
113
142
  * `onPropertyChanged('volume')` etc. The provider updates the
114
- * progress context ONLY on 1Hz ticks + the dedicated event updates
115
- * (`isBuffering`, `isSeeking`, `seekable`, `cacheRanges`, `cacheFill`).
143
+ * progress context from the native mpv event stream — position and
144
+ * duration arrive on the property events (4 Hz after native
145
+ * coalescing), and `isBuffering` / `isSeeking` / `seekable` /
146
+ * `cacheRanges` / `cacheFill` each have their own dedicated event —
147
+ * plus a one-shot hydration read at mount.
116
148
  */
117
149
  export interface PlayerProgress {
118
150
  // ── V12 surface (preserved) ───────────────────────────────────────────
@@ -128,9 +160,29 @@ export interface PlayerProgress {
128
160
  isSeeking: boolean;
129
161
  /** True when the current file is seekable. */
130
162
  seekable: boolean;
131
- /** Cached byte ranges. Empty for non-network sources. */
163
+ /**
164
+ * Buffered timeline ranges, in MILLISECONDS, relative to the stream start.
165
+ *
166
+ * The same unit as `positionMs` and `durationMs`, so a consumer never
167
+ * has to know that mpv's own `seekable-ranges` is in seconds — the
168
+ * native bridge converts, because it is the only layer that knows which
169
+ * unit a payload carried.
170
+ *
171
+ * Empty when the source exposes no time-space cache information. A byte
172
+ * offset is NOT a time and is never reported here: a range painted from
173
+ * byte offsets would claim that unbuffered data is buffered.
174
+ */
132
175
  cacheRanges: Array<{ start: number; end: number }>;
133
- /** Cache fill percent (0..100). 0 for non-network sources. */
176
+ /**
177
+ * Cache fill percent (0..100). From the dedicated `onCacheFill`
178
+ * event, which the native bridge feeds from mpv's
179
+ * `cache-buffering-state`.
180
+ *
181
+ * It is a LEVEL, not a stall: a partially filled cache is the
182
+ * normal state during playback. `isBuffering` is owned by
183
+ * `onBuffering` (mpv's `paused-for-cache`), so the two can never
184
+ * contradict each other.
185
+ */
134
186
  cacheFill: number;
135
187
  }
136
188
 
@@ -141,9 +193,9 @@ export interface PlayerProgress {
141
193
  * brightness, openPlayer, etc.).
142
194
  *
143
195
  * All methods resolve the bridge lazily at call time, so a stable
144
- * command object is built once at module load. The 1Hz-poll progress
145
- * context and the heavy event-subscription state context are
146
- * separate from the commands.
196
+ * command object is built once at module load. The event-driven
197
+ * progress context and the heavy event-subscription state context
198
+ * are separate from the commands.
147
199
  */
148
200
  export interface PlayerCommands {
149
201
  // ── V12 surface (preserved) ───────────────────────────────────────────
@@ -391,6 +443,7 @@ export const PlayerProgressContext = createContext<PlayerProgress | null>(null);
391
443
  export const DEFAULT_STATE: PlayerState = {
392
444
  // V12
393
445
  isPlaying: false,
446
+ isLoading: false,
394
447
  title: 'Simba Player',
395
448
  artist: '',
396
449
  album: '',
@@ -771,8 +824,8 @@ const BUILT_COMMANDS: PlayerCommands = buildCommands();
771
824
  * V13 Phase 51: hook returning the player's current state + commands.
772
825
  *
773
826
  * When called inside a `<PlayerProvider>`, the `state` object is
774
- * driven by mpv events (via `subscribePlayerEvent`) and the 1Hz
775
- * position/duration poll. Outside a provider, `state` is
827
+ * driven by mpv events (via `subscribePlayerEvent`) plus a one-shot
828
+ * hydration read at mount. Outside a provider, `state` is
776
829
  * `DEFAULT_STATE` (no throw — see Phase 24 design notes: this
777
830
  * hook is non-throwing so DefaultControls can render in any
778
831
  * environment, including web previews and tests).
@@ -811,9 +864,10 @@ export function usePlayer(): UsePlayerResult {
811
864
  * volume / track / chapter change.
812
865
  *
813
866
  * The provider updates the progress context only on:
814
- * - 1Hz `bridge.getPosition()` / `getDuration()` ticks
815
- * - `onBuffering` / `onSeeking` / `onSeekable` / `onCacheState`
816
- * events
867
+ * - the native mpv event stream (position, duration, volume, …)
868
+ * - `onBuffering` / `onSeeking` / `onSeekable` / `onCacheState` /
869
+ * `onCacheFill` events
870
+ * - a one-shot hydration read at mount
817
871
  *
818
872
  * Outside a provider, returns `DEFAULT_PROGRESS` (`0/0` + falsy
819
873
  * progress fields).
@@ -1035,7 +1089,7 @@ export function parseMetadata(
1035
1089
  * and return the next pair. Pure function — no React, no bridge calls.
1036
1090
  *
1037
1091
  * `state` carries the "static-ish" player state (title, isPlaying,
1038
- * volume, tracks, etc.) and `progress` carries the "1Hz-pollable"
1092
+ * volume, tracks, etc.) and `progress` carries the fast-moving
1039
1093
  * fields (position, duration, isBuffering, isSeeking, seekable,
1040
1094
  * cacheRanges, cacheFill). The two are kept separate so consumers
1041
1095
  * that only render a scrubber (using `usePlayerProgress`) don't
@@ -1086,6 +1140,13 @@ export function applyPlayerEvent(
1086
1140
  const p = (payload ?? {}) as Record<string, unknown>;
1087
1141
 
1088
1142
  switch (event) {
1143
+ case 'onLoadRequested': {
1144
+ // The engine has been asked for a file and has not reported it
1145
+ // yet: the file is OPENING. This is the window a loading
1146
+ // indicator belongs to, and it is the reason `isLoading` exists —
1147
+ // see `PlayerState.isLoading`.
1148
+ return { state: { ...state, isLoading: true }, progress };
1149
+ }
1089
1150
  case 'onFileLoaded': {
1090
1151
  const file = (p.file ?? undefined) as MpvFileInfo | undefined;
1091
1152
  return {
@@ -1096,6 +1157,8 @@ export function applyPlayerEvent(
1096
1157
  ? Math.round(file.duration * 1000)
1097
1158
  : state.durationMs,
1098
1159
  isPlaying: true,
1160
+ // The file has arrived, so the load is no longer in flight.
1161
+ isLoading: false,
1099
1162
  positionMs: 0,
1100
1163
  error: null, // clear any prior error
1101
1164
  },
@@ -1217,43 +1280,103 @@ export function applyPlayerEvent(
1217
1280
  return { state: { ...state, currentChapter: (p.chapter as MpvChapter | null) ?? null }, progress };
1218
1281
  case 'onVideoParamsChanged':
1219
1282
  return { state: { ...state, videoParams: (p.params as MpvVideoParams) ?? null }, progress };
1220
- case 'onError':
1283
+ case 'onError': {
1284
+ // The native payload carries a NUMERIC `code` (the lib's stable
1285
+ // integer contract) and a string `codeName`.
1286
+ //
1287
+ // It used to carry a STRING `code`, so `Number("E_RENDERER_GONE")`
1288
+ // was NaN and every failure reached the app with `code: NaN` and
1289
+ // `numeric=0`. A non-finite value now degrades to 0 rather than
1290
+ // poisoning the contract.
1291
+ const rawCode = Number(p.code);
1292
+ const code = Number.isFinite(rawCode) ? rawCode : 0;
1293
+ const recoverable = Boolean(p.recoverable ?? false);
1294
+ const error = {
1295
+ code,
1296
+ codeName: p.codeName != null ? String(p.codeName) : undefined,
1297
+ recoverable,
1298
+ message: String(p.message ?? ''),
1299
+ };
1300
+ // A NON-terminal failure must not mask a terminal one.
1301
+ //
1302
+ // mpv can report a fatal-level LOG line (a subsystem complaint,
1303
+ // playback continued) AFTER the file has already failed, and the
1304
+ // last error to arrive is not necessarily the one that ended
1305
+ // playback. A terminal error stays until the next `onFileLoaded`
1306
+ // clears it — except for a newer TERMINAL error, which wins, so a
1307
+ // retry that fails again reports the new failure and not the
1308
+ // previous one.
1309
+ const keepTerminal =
1310
+ recoverable && state.error != null && !state.error.recoverable;
1221
1311
  return {
1222
1312
  state: {
1223
1313
  ...state,
1224
- error: {
1225
- code: Number(p.code ?? 0),
1226
- codeName: p.codeName != null ? String(p.codeName) : undefined,
1227
- recoverable: Boolean(p.recoverable ?? false),
1228
- message: String(p.message ?? ''),
1229
- },
1314
+ error: keepTerminal ? state.error : error,
1230
1315
  },
1231
1316
  progress,
1232
1317
  };
1318
+ }
1233
1319
  case 'onBuffering': {
1234
- // `isBuffering` is the more useful consumer signal than `percent`;
1235
- // honor the explicit flag if present, otherwise treat any
1236
- // non-zero percent as buffering.
1237
- const isBuffering =
1238
- typeof p.isBuffering === 'boolean'
1239
- ? p.isBuffering
1240
- : ((p.percent as number) > 0 && (p.percent as number) < 100);
1320
+ // The STALL signal, and the only thing that may set `isBuffering`.
1321
+ //
1322
+ // It comes from mpv's `paused-for-cache`, which is true exactly while
1323
+ // playback is paused because the cache underran. The payload carries
1324
+ // no percentage: the property does not know the fill, and the old
1325
+ // code invented one (0 when stalled, 100 when not) which overwrote
1326
+ // the real fill with a fabricated number.
1327
+ //
1328
+ // The percent-based inference is gone with the percent. A partially
1329
+ // filled cache is the NORMAL state during playback, so
1330
+ // "percent < 100" is true for most of a stream's life — reading it
1331
+ // as "buffering" latched the spinner on during healthy playback.
1332
+ const isBuffering = Boolean(p.isBuffering);
1241
1333
  return {
1242
1334
  state: { ...state, isBuffering },
1243
1335
  progress: { ...progress, isBuffering },
1244
1336
  };
1245
1337
  }
1246
- case 'onCacheState':
1247
- // Cache state is progress-only — no state field, just progress
1248
- // updates for consumers that render a cache indicator.
1338
+ case 'onCacheFill': {
1339
+ // The cache FILL LEVEL — a percentage, not a stall. It comes from
1340
+ // mpv's `cache-buffering-state`, which changes continuously as the
1341
+ // cache refills, so it is a level and nothing else.
1342
+ //
1343
+ // It owns `cacheFill` and nothing else. `isBuffering` belongs to
1344
+ // `onBuffering`, and `cacheRanges` belongs to `onCacheState`. One
1345
+ // fact, one owner — the old code had two properties writing one
1346
+ // field with contradictory rules, and they disagreed.
1347
+ const percent = Number(p.percent ?? 0);
1249
1348
  return {
1250
1349
  state,
1251
1350
  progress: {
1252
1351
  ...progress,
1253
- cacheRanges: (p.ranges as Array<{ start: number; end: number }>) ?? [],
1254
- cacheFill: Number(p.fill ?? 0),
1352
+ cacheFill: Number.isFinite(percent) ? percent : 0,
1255
1353
  },
1256
1354
  };
1355
+ }
1356
+ case 'onCacheState': {
1357
+ // The buffered TIMELINE ranges, in MILLISECONDS.
1358
+ //
1359
+ // They come from mpv's `demuxer-cache-state.seekable-ranges`, which is
1360
+ // in seconds; the native bridge converts to milliseconds because it
1361
+ // is the only layer that knows which unit the payload carried. The
1362
+ // consumer receives one unit — the same one `positionMs` and
1363
+ // `durationMs` use — and never has to guess.
1364
+ //
1365
+ // A payload that carries no `ranges` at all leaves the previous value
1366
+ // alone. A payload that carries an EMPTY array is authoritative and
1367
+ // clears: that is mpv saying the cache is empty, which is different
1368
+ // from saying nothing.
1369
+ const ranges = p.ranges as
1370
+ | Array<{ start: number; end: number }>
1371
+ | undefined;
1372
+ return {
1373
+ state,
1374
+ progress: {
1375
+ ...progress,
1376
+ cacheRanges: Array.isArray(ranges) ? ranges : progress.cacheRanges,
1377
+ },
1378
+ };
1379
+ }
1257
1380
  case 'onSeekable':
1258
1381
  return {
1259
1382
  state: { ...state, seekable: Boolean(p.seekable) },
@@ -1265,7 +1388,8 @@ export function applyPlayerEvent(
1265
1388
  progress: { ...progress, isSeeking: Boolean(p.seeking) },
1266
1389
  };
1267
1390
  case 'onEndFile':
1268
- return { state: { ...state, isPlaying: false }, progress };
1391
+ // The file is over (ended, or failed). No load is in flight.
1392
+ return { state: { ...state, isPlaying: false, isLoading: false }, progress };
1269
1393
  case 'onPlaybackRestart':
1270
1394
  return {
1271
1395
  state: { ...state, isPlaying: true, positionMs: 0 },