@simba-dev/react-native-media-player 1.0.8 → 1.2.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.
- package/package.json +2 -2
- package/src/bridge/MpvPlayerModule.ts +652 -70
- package/src/bridge/__tests__/errorContract.test.ts +10 -6
- package/src/components/PlayerProvider.tsx +288 -7
- package/src/hooks/PlayerResumeProvider.tsx +21 -0
- package/src/hooks/SimbaPlayer.tsx +93 -0
- package/src/hooks/useLaunchParams.tsx +64 -0
- package/src/hooks/useOpenWithResume.tsx +236 -0
- package/src/hooks/usePlayerActivity.ts +103 -0
- package/src/index.ts +32 -0
- package/src/types/__tests__/player.test.tsx +494 -0
- package/src/types/player.ts +947 -72
- package/src/types/__tests__/player.test.ts +0 -166
|
@@ -33,8 +33,11 @@ describe('Phase 38 — error contract (TypeScript side)', () => {
|
|
|
33
33
|
// The native module isn't wired in jest — verify by checking
|
|
34
34
|
// that a setConfig call (the only Promise-returning method)
|
|
35
35
|
// resolves immediately rather than rejecting with the documented
|
|
36
|
-
// E_CONFIG_PARSE_FAILED code.
|
|
37
|
-
|
|
36
|
+
// E_CONFIG_PARSE_FAILED code. V13 Phase 50: setConfig now
|
|
37
|
+
// resolves with the count of parsed top-level keys (matches the
|
|
38
|
+
// Kotlin implementation at MpvBridgeModule.kt). The no-op
|
|
39
|
+
// fallback resolves to 0.
|
|
40
|
+
await expect(bridge.setConfig('not-valid-json{')).resolves.toBe(0);
|
|
38
41
|
});
|
|
39
42
|
|
|
40
43
|
it('no-op bridge methods do not throw on edge-case inputs', () => {
|
|
@@ -56,11 +59,12 @@ describe('Phase 38 — error contract (TypeScript side)', () => {
|
|
|
56
59
|
// for malformed JSON. The no-op fallback resolves silently —
|
|
57
60
|
// this is by design: jest unit tests should not crash on
|
|
58
61
|
// malformed config; the real parsing happens at runtime on a
|
|
59
|
-
// device.
|
|
62
|
+
// device. V13 Phase 50: the resolution value is the parsed-key
|
|
63
|
+
// count (0 for malformed input, matches Kotlin).
|
|
60
64
|
const bridge = getMpvPlayerModule();
|
|
61
|
-
await expect(bridge.setConfig('')).resolves.
|
|
62
|
-
await expect(bridge.setConfig('not-json')).resolves.
|
|
63
|
-
await expect(bridge.setConfig('{')).resolves.
|
|
65
|
+
await expect(bridge.setConfig('')).resolves.toBe(0);
|
|
66
|
+
await expect(bridge.setConfig('not-json')).resolves.toBe(0);
|
|
67
|
+
await expect(bridge.setConfig('{')).resolves.toBe(0);
|
|
64
68
|
});
|
|
65
69
|
|
|
66
70
|
it('typed bridge interface exposes the 6 documented methods', () => {
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import React, {
|
|
1
|
+
import React, {
|
|
2
|
+
createContext,
|
|
3
|
+
useContext,
|
|
4
|
+
useEffect,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
} from 'react';
|
|
2
9
|
import { NativeModules } from 'react-native';
|
|
3
10
|
import {
|
|
4
11
|
PlayerConfig,
|
|
@@ -6,6 +13,23 @@ import {
|
|
|
6
13
|
ResolvedPlayerConfig,
|
|
7
14
|
resolvePlayerConfig,
|
|
8
15
|
} from '../types/config';
|
|
16
|
+
import {
|
|
17
|
+
applyPlayerEvent,
|
|
18
|
+
DEFAULT_PROGRESS,
|
|
19
|
+
DEFAULT_STATE,
|
|
20
|
+
hydratePlayerState,
|
|
21
|
+
PlayerProgress,
|
|
22
|
+
PlayerProgressContext,
|
|
23
|
+
PlayerState,
|
|
24
|
+
PlayerStateContext,
|
|
25
|
+
} from '../types/player';
|
|
26
|
+
import {
|
|
27
|
+
getMpvPlayerModule,
|
|
28
|
+
PlayerEventName,
|
|
29
|
+
PlayerEventPayloads,
|
|
30
|
+
removeAllListeners,
|
|
31
|
+
subscribePlayerEvent,
|
|
32
|
+
} from '../bridge/MpvPlayerModule';
|
|
9
33
|
|
|
10
34
|
/**
|
|
11
35
|
* React Context carrying the resolved PlayerConfig. Consumers should
|
|
@@ -123,6 +147,83 @@ function getNativeModule():
|
|
|
123
147
|
return null;
|
|
124
148
|
}
|
|
125
149
|
|
|
150
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
151
|
+
// V13 Phase 51: PlayerStateContext + PlayerProgressContext + provider
|
|
152
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Internal: the state + progress context provider. The full
|
|
156
|
+
* `PlayerProvider` (below) is the public surface; this
|
|
157
|
+
* lower-level component just exposes the state + progress
|
|
158
|
+
* contexts with already-computed values, so the public
|
|
159
|
+
* provider can keep its config-push logic and just slot the
|
|
160
|
+
* state in.
|
|
161
|
+
*
|
|
162
|
+
* The contexts themselves are defined in `types/player.ts`
|
|
163
|
+
* (not here) so non-JSX consumers can import them without
|
|
164
|
+
* forcing the JSX transform on the whole module.
|
|
165
|
+
*/
|
|
166
|
+
function PlayerStateProvider({
|
|
167
|
+
state,
|
|
168
|
+
progress,
|
|
169
|
+
children,
|
|
170
|
+
}: {
|
|
171
|
+
state: PlayerState;
|
|
172
|
+
progress: PlayerProgress;
|
|
173
|
+
children: React.ReactNode;
|
|
174
|
+
}): React.ReactElement {
|
|
175
|
+
return (
|
|
176
|
+
<PlayerStateContext.Provider value={state}>
|
|
177
|
+
<PlayerProgressContext.Provider value={progress}>
|
|
178
|
+
{children}
|
|
179
|
+
</PlayerProgressContext.Provider>
|
|
180
|
+
</PlayerStateContext.Provider>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* All 22 player event names. Must match `PlayerEventName` in
|
|
186
|
+
* `MpvPlayerModule.ts` (and the Kotlin `MpvBridgeModule.kt`
|
|
187
|
+
* `RCTDeviceEventEmitter` strings).
|
|
188
|
+
*
|
|
189
|
+
* Kept here (not in the bridge module) to avoid pulling the React
|
|
190
|
+
* context machinery into a pure-types module; the provider is the
|
|
191
|
+
* only consumer of this constant anyway.
|
|
192
|
+
*/
|
|
193
|
+
const ALL_PLAYER_EVENTS: readonly PlayerEventName[] = [
|
|
194
|
+
'onFileLoaded',
|
|
195
|
+
'onPlaybackStateChanged',
|
|
196
|
+
'onPositionChanged',
|
|
197
|
+
'onDurationChanged',
|
|
198
|
+
'onPropertyChanged',
|
|
199
|
+
'onTracksChanged',
|
|
200
|
+
'onChapterChanged',
|
|
201
|
+
'onVideoParamsChanged',
|
|
202
|
+
'onError',
|
|
203
|
+
'onBuffering',
|
|
204
|
+
'onCacheState',
|
|
205
|
+
'onSeekable',
|
|
206
|
+
'onSeeking',
|
|
207
|
+
'onEndFile',
|
|
208
|
+
'onPlaybackRestart',
|
|
209
|
+
'onEndReached',
|
|
210
|
+
'onAudioDeviceChanged',
|
|
211
|
+
'onVolumeChanged',
|
|
212
|
+
'onSpeedChanged',
|
|
213
|
+
'videoReconfig',
|
|
214
|
+
'onPipModeChanged',
|
|
215
|
+
'onPipPlayPause',
|
|
216
|
+
'onPipExpand',
|
|
217
|
+
'onPipClose',
|
|
218
|
+
];
|
|
219
|
+
|
|
220
|
+
/** How often the provider polls `getPosition` + `getDuration` from the bridge. */
|
|
221
|
+
const POSITION_POLL_INTERVAL_MS = 1000;
|
|
222
|
+
|
|
223
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
224
|
+
// PlayerProvider
|
|
225
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
226
|
+
|
|
126
227
|
export interface PlayerProviderProps {
|
|
127
228
|
/**
|
|
128
229
|
* Partial PlayerConfig — every field is optional and falls back to
|
|
@@ -161,7 +262,8 @@ export interface PlayerProviderProps {
|
|
|
161
262
|
|
|
162
263
|
/**
|
|
163
264
|
* Wraps the consumer app (or any subtree) and provides the resolved
|
|
164
|
-
* PlayerConfig + renderControls slot
|
|
265
|
+
* PlayerConfig + renderControls slot + live player state to all
|
|
266
|
+
* consumers below it.
|
|
165
267
|
*
|
|
166
268
|
* On mount and whenever the resolved config changes, the provider
|
|
167
269
|
* pushes the JSON-encoded config to the native side via
|
|
@@ -175,6 +277,22 @@ export interface PlayerProviderProps {
|
|
|
175
277
|
* tree with `<PlayerProvider renderControls={...}>` and the
|
|
176
278
|
* `<PlayerRoot>` component reads it via `useRenderControls()`.
|
|
177
279
|
*
|
|
280
|
+
* V13 Phase 51: in addition to config + renderControls, the provider
|
|
281
|
+
* now holds the live player state. The state is updated by:
|
|
282
|
+
* - **Initial hydration** (mount): synchronous bridge getters fill in
|
|
283
|
+
* position / duration / playback state / volume / mute / speed /
|
|
284
|
+
* loop / playlist / tracks / chapters / video params.
|
|
285
|
+
* - **Event subscriptions** (mount): all 22 mpv events are subscribed
|
|
286
|
+
* via `subscribePlayerEvent` and dispatched to `applyPlayerEvent`.
|
|
287
|
+
* - **1Hz position/duration poll** (mount): `setInterval` calls the
|
|
288
|
+
* sync `getPosition` / `getDuration` getters every 1000ms.
|
|
289
|
+
*
|
|
290
|
+
* State is held in a `useState` (rendered, for React) + `useRef`
|
|
291
|
+
* (current, for event handlers to read without stale closures) pair.
|
|
292
|
+
* On each event, we read the ref, compute the next state, write the
|
|
293
|
+
* ref, and call setState. This is the canonical "useRef + useState"
|
|
294
|
+
* pattern for "live state from imperative event sources".
|
|
295
|
+
*
|
|
178
296
|
* The provider is idempotent: re-rendering with the same config does
|
|
179
297
|
* not trigger an extra native push (the `JSON.stringify` dep is the
|
|
180
298
|
* cheap identity check).
|
|
@@ -184,10 +302,7 @@ export function PlayerProvider({
|
|
|
184
302
|
renderControls,
|
|
185
303
|
children,
|
|
186
304
|
}: PlayerProviderProps): React.ReactElement {
|
|
187
|
-
//
|
|
188
|
-
// consumers who create a fresh object on every render don't trigger
|
|
189
|
-
// a native push storm. The native side is a no-op when the JSON is
|
|
190
|
-
// identical to the previously-cached one.
|
|
305
|
+
// ── Config + renderControls (V12) ──────────────────────────────────────
|
|
191
306
|
const resolved = useMemo<ResolvedPlayerConfig>(
|
|
192
307
|
() => resolvePlayerConfig(config),
|
|
193
308
|
[JSON.stringify(config ?? {})],
|
|
@@ -212,10 +327,176 @@ export function PlayerProvider({
|
|
|
212
327
|
});
|
|
213
328
|
}, [resolved]);
|
|
214
329
|
|
|
330
|
+
// ── V13 Phase 51: live player state ────────────────────────────────────
|
|
331
|
+
const [state, setState] = useState<PlayerState>(DEFAULT_STATE);
|
|
332
|
+
const [progress, setProgress] = useState<PlayerProgress>(DEFAULT_PROGRESS);
|
|
333
|
+
// The refs are the source of truth for event handlers. They read the
|
|
334
|
+
// latest snapshot, apply their patch, and write back — this avoids
|
|
335
|
+
// the stale-closure trap of `useState` setters in long-lived event
|
|
336
|
+
// subscriptions.
|
|
337
|
+
const stateRef = useRef<PlayerState>(DEFAULT_STATE);
|
|
338
|
+
const progressRef = useRef<PlayerProgress>(DEFAULT_PROGRESS);
|
|
339
|
+
// Tracks whether the 1Hz poll should fire. The poll runs in a child
|
|
340
|
+
// effect that mounts the interval; the unmount cleanup is symmetric.
|
|
341
|
+
// We mount/unmount ONCE for the provider's lifetime (no deps), so
|
|
342
|
+
// the poll survives config changes.
|
|
343
|
+
|
|
344
|
+
// Initial hydration + event subscriptions + 1Hz poll all live in a
|
|
345
|
+
// single mount-only effect so they share the same lifecycle. (If we
|
|
346
|
+
// split them across multiple effects, the ref + state would briefly
|
|
347
|
+
// desync during the gap between hydration and subscription mount.)
|
|
348
|
+
useEffect(() => {
|
|
349
|
+
const bridge = getMpvPlayerModule();
|
|
350
|
+
|
|
351
|
+
// ── 1. Initial hydration from sync bridge getters ───────────────────
|
|
352
|
+
try {
|
|
353
|
+
const initial = hydratePlayerState(bridge);
|
|
354
|
+
stateRef.current = initial;
|
|
355
|
+
setState(initial);
|
|
356
|
+
const initialProgress: PlayerProgress = {
|
|
357
|
+
positionMs: initial.positionMs,
|
|
358
|
+
durationMs: initial.durationMs,
|
|
359
|
+
isBuffering: initial.isBuffering,
|
|
360
|
+
isSeeking: initial.isSeeking,
|
|
361
|
+
seekable: initial.seekable,
|
|
362
|
+
cacheRanges: [],
|
|
363
|
+
cacheFill: 0,
|
|
364
|
+
};
|
|
365
|
+
progressRef.current = initialProgress;
|
|
366
|
+
setProgress(initialProgress);
|
|
367
|
+
} catch (e) {
|
|
368
|
+
// Hydration is best-effort; if any getter throws we just keep
|
|
369
|
+
// the defaults. (A typical cause is a stub bridge in a test
|
|
370
|
+
// environment that doesn't implement every getter.)
|
|
371
|
+
// eslint-disable-next-line no-console
|
|
372
|
+
console.warn('[simba-player] hydratePlayerState failed:', e);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// ── 2. Subscribe to all 22 mpv events ───────────────────────────────
|
|
376
|
+
// Each event handler reads the current state + progress from the
|
|
377
|
+
// refs, applies the patch via the pure `applyPlayerEvent`
|
|
378
|
+
// reducer, writes back to the refs, and schedules a re-render
|
|
379
|
+
// via setState/setProgress. The functional setState form keeps
|
|
380
|
+
// the progress equality short-circuit correct under React's
|
|
381
|
+
// batching.
|
|
382
|
+
const unsubs: Array<() => void> = [];
|
|
383
|
+
for (const event of ALL_PLAYER_EVENTS) {
|
|
384
|
+
const unsub = subscribePlayerEvent(
|
|
385
|
+
event,
|
|
386
|
+
(payload: PlayerEventPayloads[typeof event]) => {
|
|
387
|
+
const { state: nextState, progress: nextProgress } =
|
|
388
|
+
applyPlayerEvent(
|
|
389
|
+
stateRef.current,
|
|
390
|
+
progressRef.current,
|
|
391
|
+
event,
|
|
392
|
+
payload,
|
|
393
|
+
);
|
|
394
|
+
// Skip the setState if the new state is reference-equal
|
|
395
|
+
// (a true no-op event for this state) — prevents
|
|
396
|
+
// re-render storms on `videoReconfig` / PiP no-ops.
|
|
397
|
+
if (nextState !== stateRef.current) {
|
|
398
|
+
stateRef.current = nextState;
|
|
399
|
+
setState(nextState);
|
|
400
|
+
}
|
|
401
|
+
// Progress: apply the patch via the functional setState
|
|
402
|
+
// form so we can compare against the previous value (the
|
|
403
|
+
// ref may have been updated by another event handler in
|
|
404
|
+
// the same batch).
|
|
405
|
+
setProgress((prev) => {
|
|
406
|
+
// Cheap reference + 7-field equality check.
|
|
407
|
+
if (
|
|
408
|
+
prev.positionMs === nextProgress.positionMs &&
|
|
409
|
+
prev.durationMs === nextProgress.durationMs &&
|
|
410
|
+
prev.isBuffering === nextProgress.isBuffering &&
|
|
411
|
+
prev.isSeeking === nextProgress.isSeeking &&
|
|
412
|
+
prev.seekable === nextProgress.seekable &&
|
|
413
|
+
prev.cacheFill === nextProgress.cacheFill &&
|
|
414
|
+
prev.cacheRanges.length === nextProgress.cacheRanges.length
|
|
415
|
+
) {
|
|
416
|
+
return prev;
|
|
417
|
+
}
|
|
418
|
+
progressRef.current = nextProgress;
|
|
419
|
+
return nextProgress;
|
|
420
|
+
});
|
|
421
|
+
},
|
|
422
|
+
);
|
|
423
|
+
unsubs.push(unsub);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// ── 3. 1Hz position/duration poll ───────────────────────────────────
|
|
427
|
+
// Both `getPosition` and `getDuration` are sync React methods
|
|
428
|
+
// on the bridge, so the poll doesn't queue microtasks. We
|
|
429
|
+
// also pick up the playback state (`getPlaybackState`) here
|
|
430
|
+
// as a backup for the event stream (e.g. if the consumer is
|
|
431
|
+
// mounted mid-playback and missed the `onPlaybackStateChanged`
|
|
432
|
+
// event).
|
|
433
|
+
const pollId = setInterval(() => {
|
|
434
|
+
try {
|
|
435
|
+
const posSec = bridge.getPosition();
|
|
436
|
+
const durSec = bridge.getDuration();
|
|
437
|
+
const positionMs = Number.isFinite(posSec)
|
|
438
|
+
? Math.round(posSec * 1000)
|
|
439
|
+
: stateRef.current.positionMs;
|
|
440
|
+
const durationMs = Number.isFinite(durSec)
|
|
441
|
+
? Math.round(durSec * 1000)
|
|
442
|
+
: stateRef.current.durationMs;
|
|
443
|
+
|
|
444
|
+
if (
|
|
445
|
+
positionMs === stateRef.current.positionMs &&
|
|
446
|
+
durationMs === stateRef.current.durationMs
|
|
447
|
+
) {
|
|
448
|
+
return; // no movement — skip the setState
|
|
449
|
+
}
|
|
450
|
+
const next: PlayerState = {
|
|
451
|
+
...stateRef.current,
|
|
452
|
+
positionMs,
|
|
453
|
+
durationMs,
|
|
454
|
+
};
|
|
455
|
+
stateRef.current = next;
|
|
456
|
+
setState(next);
|
|
457
|
+
const nextProgress: PlayerProgress = {
|
|
458
|
+
...progressRef.current,
|
|
459
|
+
positionMs,
|
|
460
|
+
durationMs,
|
|
461
|
+
};
|
|
462
|
+
progressRef.current = nextProgress;
|
|
463
|
+
setProgress(nextProgress);
|
|
464
|
+
} catch (e) {
|
|
465
|
+
// Bridge threw — keep the last known position. (The no-op
|
|
466
|
+
// bridge returns 0/0, which falls into the "no movement"
|
|
467
|
+
// early-return above, so it doesn't spam setState.)
|
|
468
|
+
// eslint-disable-next-line no-console
|
|
469
|
+
console.warn('[simba-player] position poll failed:', e);
|
|
470
|
+
}
|
|
471
|
+
}, POSITION_POLL_INTERVAL_MS);
|
|
472
|
+
|
|
473
|
+
// ── Cleanup ─────────────────────────────────────────────────────────
|
|
474
|
+
return () => {
|
|
475
|
+
for (const unsub of unsubs) {
|
|
476
|
+
try {
|
|
477
|
+
unsub();
|
|
478
|
+
} catch {
|
|
479
|
+
// never let a single bad unsub abort the rest
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
clearInterval(pollId);
|
|
483
|
+
// Best-effort: remove any lingering listeners from the emitter
|
|
484
|
+
// so the next mount of the provider starts clean. (Strict-mode
|
|
485
|
+
// double-invocation can otherwise leave phantom listeners.)
|
|
486
|
+
try {
|
|
487
|
+
removeAllListeners();
|
|
488
|
+
} catch {
|
|
489
|
+
// ignore — emitter may be absent in jest
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
}, []);
|
|
493
|
+
|
|
215
494
|
return (
|
|
216
495
|
<PlayerConfigContext.Provider value={resolved}>
|
|
217
496
|
<PlayerRenderControlsContext.Provider value={renderControls ?? null}>
|
|
218
|
-
{
|
|
497
|
+
<PlayerStateProvider state={state} progress={progress}>
|
|
498
|
+
{children}
|
|
499
|
+
</PlayerStateProvider>
|
|
219
500
|
</PlayerRenderControlsContext.Provider>
|
|
220
501
|
</PlayerConfigContext.Provider>
|
|
221
502
|
);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { PlayerResumeContext } from './useOpenWithResume';
|
|
3
|
+
import type { PlayerResumeProviderProps } from './useOpenWithResume';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* JSX shim for `<PlayerResumeProvider>`. The context itself lives in
|
|
7
|
+
* `useOpenWithResume.ts` (a `.ts` file); the JSX-using component
|
|
8
|
+
* lives here (a `.tsx` file) so TypeScript's legacy JSX transform
|
|
9
|
+
* (`jsx: "react-native"` in the project's tsconfig) can parse the
|
|
10
|
+
* JSX without a `.ts`-file-with-JSX parse error.
|
|
11
|
+
*/
|
|
12
|
+
export function PlayerResumeProvider({
|
|
13
|
+
lookup,
|
|
14
|
+
children,
|
|
15
|
+
}: PlayerResumeProviderProps): React.ReactElement {
|
|
16
|
+
return (
|
|
17
|
+
<PlayerResumeContext.Provider value={lookup}>
|
|
18
|
+
{children}
|
|
19
|
+
</PlayerResumeContext.Provider>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { PlayerProvider } from '../components/PlayerProvider';
|
|
3
|
+
import type { PlayerConfig } from '../types/config';
|
|
4
|
+
import { PlayerResumeProvider } from './useOpenWithResume';
|
|
5
|
+
import type { PlayerResumeLookup } from './useOpenWithResume';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* V13+ DX: the single-entry-point wrapper for the SIMBA player
|
|
9
|
+
* module. This is the **only** component a consumer's app root
|
|
10
|
+
* needs to integrate the module. It composes the three layers a
|
|
11
|
+
* typical app needs:
|
|
12
|
+
*
|
|
13
|
+
* 1. `<PlayerProvider>` — the config + state context (Phase 51).
|
|
14
|
+
* 2. `<PlayerResumeProvider>` — the bookmark-aware resume
|
|
15
|
+
* lookup used by `useOpenWithResume` (Phase 51/52 + DX).
|
|
16
|
+
*
|
|
17
|
+
* Without `<SimbaPlayer>`, the consumer would have to nest both
|
|
18
|
+
* providers manually, which is verbose and error-prone for junior
|
|
19
|
+
* developers. With `<SimbaPlayer>`, the entire integration is
|
|
20
|
+
* **one import + one wrapper component**:
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* // App.tsx (the consumer's root)
|
|
25
|
+
* import { SimbaPlayer } from '@simba-dev/react-native-media-player';
|
|
26
|
+
*
|
|
27
|
+
* export default function App() {
|
|
28
|
+
* const playerLookup = useBookmarkLookup();
|
|
29
|
+
* return (
|
|
30
|
+
* <SimbaPlayer
|
|
31
|
+
* config={{ theme: { accent: '#FFD700' } }}
|
|
32
|
+
* lookup={playerLookup}
|
|
33
|
+
* >
|
|
34
|
+
* <RootNavigator />
|
|
35
|
+
* </SimbaPlayer>
|
|
36
|
+
* );
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* The `lookup` prop is optional — pass it to enable
|
|
41
|
+
* `useOpenWithResume`'s auto-resume behavior. If you only need
|
|
42
|
+
* `usePlayerActivity().openPlayer(...)` (no resume lookup), you
|
|
43
|
+
* can omit `lookup` and the inner `<PlayerResumeProvider>` is
|
|
44
|
+
* still mounted but with a no-op lookup that always returns 0.
|
|
45
|
+
*
|
|
46
|
+
* **Junior-dev rule of thumb:** if your screen calls
|
|
47
|
+
* `openPlayer({resumeId: item.id})`, you need a `lookup` here.
|
|
48
|
+
* If you only call `openPlayer({uri, title, type})` without
|
|
49
|
+
* `resumeId`, the `lookup` is optional.
|
|
50
|
+
*/
|
|
51
|
+
export interface SimbaPlayerProps {
|
|
52
|
+
/**
|
|
53
|
+
* Partial PlayerConfig. Every field is optional; missing
|
|
54
|
+
* fields fall back to the defaults in `resolvePlayerConfig`.
|
|
55
|
+
*/
|
|
56
|
+
config?: PlayerConfig;
|
|
57
|
+
/**
|
|
58
|
+
* Bookmark-aware resume lookup. If provided, the module's
|
|
59
|
+
* `useOpenWithResume` hook will resolve `resumeId` arguments
|
|
60
|
+
* to a `startPositionMs` via this function. If omitted,
|
|
61
|
+
* `useOpenWithResume` is a no-op for resume (every call starts
|
|
62
|
+
* from 0).
|
|
63
|
+
*/
|
|
64
|
+
lookup?: PlayerResumeLookup;
|
|
65
|
+
children: React.ReactNode;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The one-line integration point. See `SimbaPlayerProps` for
|
|
70
|
+
* usage.
|
|
71
|
+
*/
|
|
72
|
+
export function SimbaPlayer({
|
|
73
|
+
config,
|
|
74
|
+
lookup,
|
|
75
|
+
children,
|
|
76
|
+
}: SimbaPlayerProps): React.ReactElement {
|
|
77
|
+
return (
|
|
78
|
+
<PlayerProvider config={config}>
|
|
79
|
+
<PlayerResumeProvider lookup={lookup ?? noopLookup}>
|
|
80
|
+
{children}
|
|
81
|
+
</PlayerResumeProvider>
|
|
82
|
+
</PlayerProvider>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* No-op lookup used when the consumer doesn't pass a `lookup`
|
|
88
|
+
* prop. Always returns `undefined` so `useOpenWithResume` falls
|
|
89
|
+
* back to 0 (or the consumer-provided `startPositionMs`).
|
|
90
|
+
*/
|
|
91
|
+
const noopLookup: PlayerResumeLookup = {
|
|
92
|
+
getResumePosition: () => undefined,
|
|
93
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { getMpvPlayerModule, type LaunchParams } from '../bridge/MpvPlayerModule';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* V13 Phase 54: read the activity's launch params (one-shot).
|
|
6
|
+
*
|
|
7
|
+
* The native `PlayerActivity` is launched with a payload
|
|
8
|
+
* `{uri, title, type, startPositionMs}` from the most recent
|
|
9
|
+
* `openPlayer(...)` call (see `LaunchParams` in
|
|
10
|
+
* `MpvPlayerModule.ts`). The Kotlin side keeps the params as a
|
|
11
|
+
* single-shot queue — the first reader consumes them, subsequent
|
|
12
|
+
* calls return `null`.
|
|
13
|
+
*
|
|
14
|
+
* In the JS layer, this hook is what `PlayerActivity`'s React
|
|
15
|
+
* tree calls to fetch the params (typically in the activity's
|
|
16
|
+
* top-level component, e.g. `AppContent`). After reading, the
|
|
17
|
+
* hook exposes the params as React state so the consumer can:
|
|
18
|
+
*
|
|
19
|
+
* 1. Conditionally render `<PlayerRoot />` (the activity's
|
|
20
|
+
* player surface + controls) when params are present.
|
|
21
|
+
* 2. Render the regular navigator when no params (e.g. when
|
|
22
|
+
* launched from the home screen icon, no recent playback
|
|
23
|
+
* was queued).
|
|
24
|
+
*
|
|
25
|
+
* The hook is **idempotent at the React level** — calling it
|
|
26
|
+
* from multiple components reads the same native one-shot
|
|
27
|
+
* queue. The first mount wins; subsequent mounts see `null`.
|
|
28
|
+
* (This matches the Kotlin-side behavior; the alternative is a
|
|
29
|
+
* shared React state, but that requires hoisting the hook to
|
|
30
|
+
* a common ancestor.)
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* function AppContent() {
|
|
35
|
+
* const launchParams = useLaunchParams();
|
|
36
|
+
* if (launchParams) {
|
|
37
|
+
* return <PlayerRoot />;
|
|
38
|
+
* }
|
|
39
|
+
* return <RootNavigator />;
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* For consumers that just want the data without the React
|
|
44
|
+
* subscription (e.g. for analytics), use
|
|
45
|
+
* `getMpvPlayerModule().getLaunchParams()` directly. Note that
|
|
46
|
+
* a direct call consumes the one-shot queue the same way the
|
|
47
|
+
* hook does — so don't call it twice.
|
|
48
|
+
*/
|
|
49
|
+
export function useLaunchParams(): LaunchParams | null {
|
|
50
|
+
const [params, setParams] = useState<LaunchParams | null>(null);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
const bridge = getMpvPlayerModule();
|
|
54
|
+
try {
|
|
55
|
+
const next = bridge.getLaunchParams();
|
|
56
|
+
setParams(next);
|
|
57
|
+
} catch {
|
|
58
|
+
// Bridge threw — assume no launch params.
|
|
59
|
+
setParams(null);
|
|
60
|
+
}
|
|
61
|
+
}, []);
|
|
62
|
+
|
|
63
|
+
return params;
|
|
64
|
+
}
|