@simba-dev/react-native-media-player 1.22.0 → 1.23.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.
@@ -1868,14 +1868,26 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
1868
1868
  // no-op by design
1869
1869
  }
1870
1870
 
1871
+ /**
1872
+ * `override` because the TurboModule spec declares the pair: the
1873
+ * generated `NativeMpvPlayerSpec` declares `addListener` /
1874
+ * `removeListeners`, so this class has to implement them. Without
1875
+ * them in the spec, the new-architecture interop proxy exposed no
1876
+ * `addListener` and RN's `NativeEventEmitter` never attached the
1877
+ * native module (see `src/bridge/NativeMpvPlayer.ts`).
1878
+ */
1871
1879
  @ReactMethod
1872
- fun addListener(eventName: String) {
1880
+ override fun addListener(eventName: String) {
1873
1881
  // Required by NativeEventEmitter. MPVLib listener registration is
1874
1882
  // owned by initialize()/onCatalystInstanceDestroy(), not JS callers.
1875
1883
  }
1876
1884
 
1885
+ /**
1886
+ * `override` for the same reason as `addListener` above: the pair is
1887
+ * declared in the TurboModule spec.
1888
+ */
1877
1889
  @ReactMethod
1878
- fun removeListeners(count: Double) {
1890
+ override fun removeListeners(count: Double) {
1879
1891
  // Required by NativeEventEmitter. Keep the native listener attached
1880
1892
  // for the lifetime of this module instance.
1881
1893
  }
@@ -2437,13 +2449,23 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2437
2449
  /**
2438
2450
  * Exit PiP mode and finish the activity (close player session).
2439
2451
  * Called from JS when user taps "Close" in PiP RemoteActions.
2452
+ *
2453
+ * `finish()`, NOT `finishAndRemoveTask()`.
2454
+ *
2455
+ * The consumer app has ONE Activity, so `getCurrentActivity()` IS
2456
+ * that Activity. `finishAndRemoveTask()` removes the whole TASK
2457
+ * from Recents — not just the PiP window — so closing the player
2458
+ * made the entire app disappear from the Recents list.
2459
+ * `finish()` closes the PiP window and leaves the task (and the
2460
+ * app's Recents entry) in place, which is what "close the player"
2461
+ * means.
2440
2462
  */
2441
2463
  @ReactMethod
2442
2464
  @Override
2443
2465
  override fun exitPipAndFinish() {
2444
2466
  val activity = getCurrentActivity()
2445
2467
  if (activity == null) return
2446
- activity.finishAndRemoveTask()
2468
+ activity.finish()
2447
2469
  }
2448
2470
 
2449
2471
  // ── Media Notification Service ─────────────────────────────────────────
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "1.22.0",
3
+ "version": "1.23.0",
4
4
  "description": "React Native video & audio player powered by libmpv — Picture-in-Picture, MediaSession, foreground service, and a customizable TypeScript UI. Built for Android. Designed as a drop-in replacement for react-native-video / react-native-track-player / expo-av. See CHANGELOG.md for per-version notes.",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -351,6 +351,25 @@ export interface Spec extends TurboModule {
351
351
  // ── Debug (V12 Phase 39) ───────────────────────────────────────────────
352
352
  setDebugLogging(enabled: boolean): void;
353
353
  dumpObservedProperties(): number;
354
+
355
+ // ── Event emitter plumbing (React Native's NativeEventEmitter) ─────────
356
+ //
357
+ // React Native's `NativeEventEmitter` REQUIRES the native module to
358
+ // expose `addListener` / `removeListeners`. TurboModule codegen only
359
+ // exposes spec-declared methods, so while these two were missing from
360
+ // this spec the interop proxy had no `addListener` at all — and RN
361
+ // 0.86's `NativeEventEmitter` warns twice and never attaches the
362
+ // native module.
363
+ //
364
+ // The Kotlin side already declares the pair (`MpvBridgeModule
365
+ // .addListener` / `removeListeners`), so this is the spec catching up
366
+ // with the implementation, not a new API.
367
+ //
368
+ // Types: Kotlin `String` ↔ TS `string`; Kotlin `Double` ↔ TS `number`
369
+ // (codegen maps a TS `number` onto `double` in the generated Java
370
+ // spec).
371
+ addListener(eventName: string): void;
372
+ removeListeners(count: number): void;
354
373
  }
355
374
 
356
375
  export default TurboModuleRegistry.getEnforcing<Spec>('MpvPlayerModule');
@@ -516,6 +516,50 @@ export function PlayerProvider({
516
516
  [JSON.stringify(config ?? {})],
517
517
  );
518
518
 
519
+ // ── Engine initialisation (B-010) ─────────────────────────────────────
520
+ //
521
+ // `initPlayer()` runs HERE, in the render phase, and NOT in the mount
522
+ // effect. React commits passive effects CHILD-FIRST, so an effect here
523
+ // would run AFTER every descendant's effect — and after a
524
+ // descendant's RENDER, which is earlier still.
525
+ //
526
+ // The bridge's `play / pause / loadFile / seek*` methods all hit
527
+ // `ensurePtr()` first, which throws
528
+ // `IllegalStateException("MpvPlayerModule not initialized. Call
529
+ // initPlayer() first.")` while `nativePtr == 0`. `<SimbaVideo>` reads
530
+ // the handle during ITS render, so an effect-based init left the
531
+ // surface bound to a null handle for the life of that mount.
532
+ //
533
+ // A parent's render function completes before any child's render
534
+ // begins, so initialising here means the handle exists before ANY
535
+ // descendant renders.
536
+ //
537
+ // TRADE-OFF: this is a side effect in the render phase, which React
538
+ // does not recommend. It is safe ONLY because `initPlayer()` is
539
+ // idempotent: the Kotlin side short-circuits with `return true` when
540
+ // `nativePtr != 0` (`MpvBridgeModule.initPlayer()`), so a second call
541
+ // — a StrictMode double render, a second provider — is a no-op and
542
+ // never a second engine.
543
+ const bridge = getMpvPlayerModule();
544
+ const engineInitialisedRef = useRef(false);
545
+ if (!engineInitialisedRef.current) {
546
+ engineInitialisedRef.current = true;
547
+ try {
548
+ const ok = bridge.initPlayer();
549
+ if (!ok) {
550
+ // eslint-disable-next-line no-console
551
+ console.warn(
552
+ '[simba-player] initPlayer returned false on provider mount - ' +
553
+ 'the native mpv instance did not allocate. Check MpvBridgeModule logs.',
554
+ );
555
+ }
556
+ } catch (e) {
557
+ // Silent HERE on purpose: the bridge is absent in jest / web, so
558
+ // there is no native engine to initialise and nothing to report.
559
+ // The provider still serves the config to TS consumers.
560
+ }
561
+ }
562
+
519
563
  useEffect(() => {
520
564
  const native = getNativeModule();
521
565
  if (native === null) {
@@ -551,32 +595,12 @@ export function PlayerProvider({
551
595
  // desync during the gap between hydration and subscription mount.)
552
596
  // The 1Hz position poll this effect used to also start was removed in
553
597
  // v1.10.0 — the event stream is authoritative for position and duration.
598
+ //
599
+ // The engine is NOT initialised here. `initPlayer()` runs in the render
600
+ // phase above (see the B-010 note there), so the native handle already
601
+ // exists by the time this effect runs — and, more importantly, before
602
+ // any descendant renders.
554
603
  useEffect(() => {
555
- const bridge = getMpvPlayerModule();
556
-
557
- // V22.0.0 / 1.5.7 (B-010 final / D-035): ensure the native mpv
558
- // instance is created BEFORE the user can tap any playback
559
- // control. The bridge's `play / pause / loadFile / seek*`
560
- // methods all hit `ensurePtr()` first, which throws
561
- // `IllegalStateException("MpvPlayerModule not initialized. Call
562
- // initPlayer() first.")` when `nativePtr == 0`. Calling
563
- // `initPlayer()` here in the provider's mount effect is
564
- // idempotent (`nativePtr != 0` on the Kotlin side short-circuits
565
- // with `return true`) so this is safe even if a future caller
566
- // also initialises. Runs once per PlayerProvider mount.
567
- try {
568
- const ok = bridge.initPlayer();
569
- if (!ok) {
570
- // eslint-disable-next-line no-console
571
- console.warn(
572
- '[simba-player] initPlayer returned false on provider mount - ' +
573
- 'the native mpv instance did not allocate. Check MpvBridgeModule logs.',
574
- );
575
- }
576
- } catch (e) {
577
- // The bridge is missing (jest / web) - fall through silently.
578
- }
579
-
580
604
  // ── 1. Initial hydration from sync bridge getters ───────────────────
581
605
  try {
582
606
  const initial = hydratePlayerState(bridge);
@@ -740,9 +764,11 @@ export function PlayerProvider({
740
764
  // ── 2b. Observe the mpv properties those events are derived from ───
741
765
  // Step 2 subscribes to the JS event names, but mpv only *emits*
742
766
  // them for properties JS registered with `mpv_observe_property`.
743
- // Registration must happen after `initPlayer()` (step 0) so the
744
- // native handle exists; the Kotlin side also queues any name that
745
- // arrives early, so ordering is safe either way.
767
+ // Registration must happen after `initPlayer()` so the native handle
768
+ // exists. `initPlayer()` runs in the render phase above, so by the
769
+ // time this effect runs the handle is already there; the Kotlin side
770
+ // also queues any name that arrives early, so ordering is safe
771
+ // either way.
746
772
  //
747
773
  // A failure here must not abort the subscription above or the poll
748
774
  // below — this list is additive.
@@ -61,20 +61,30 @@ export function SimbaVideo(props: SimbaVideoProps): React.ReactElement {
61
61
  const { nativePtr, resizeMode = 'contain', paused, muted, volume, style, ...rest } = props;
62
62
 
63
63
  /**
64
- * `PlayerProvider` calls `initPlayer()` on mount, and the native side
65
- * short-circuits when a handle already exists, so inside a provider the
66
- * handle is always available before any child renders. Resolving it during
67
- * render rather than in an effect is deliberate: an effect would mount the
68
- * surface unbound first and leave `MpvRenderView` waiting for a
69
- * `setNativePtr` that only arrives on a later commit.
64
+ * The native handle this surface is bound to.
65
+ *
66
+ * `PlayerProvider` initialises the engine during ITS OWN render, not
67
+ * in an effect, and the native side short-circuits when a handle
68
+ * already exists — so inside a provider the handle is available before
69
+ * any child renders (a parent's render completes before any child's
70
+ * render begins).
71
+ *
72
+ * The handle is read during render rather than in an effect on
73
+ * purpose: an effect would mount the surface unbound first and leave
74
+ * `MpvRenderView` waiting for a `setNativePtr` that only arrives on a
75
+ * later commit.
76
+ *
77
+ * The bridge read is deliberately NOT memoised. `getNativePtr()` does
78
+ * not throw when the engine does not exist yet — it answers 0 — so a
79
+ * memoised read could latch that 0 and keep the surface bound to
80
+ * nothing for the life of the mount. mpv's handle is stable for the
81
+ * life of the engine, so re-reading it per render is one cheap sync
82
+ * call and can never be stale.
70
83
  *
71
84
  * Outside a provider, or when an app manages more than one engine, pass
72
85
  * `nativePtr` explicitly.
73
86
  */
74
- const resolvedPtr = useMemo(
75
- () => nativePtr ?? getMpvPlayerModule().getNativePtr(),
76
- [nativePtr],
77
- );
87
+ const resolvedPtr = nativePtr ?? getMpvPlayerModule().getNativePtr();
78
88
 
79
89
  const videoZoom = useMemo(() => {
80
90
  switch (resizeMode) {