@simba-dev/react-native-media-player 3.3.3 → 3.3.5

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/CHANGELOG.md CHANGED
@@ -1,4 +1,105 @@
1
1
 
2
+ ## [3.3.5] - 2026-11-06
3
+
4
+ ### Fixed
5
+
6
+ - **The HOME path was FLAKY: 2 of 5 HOME presses opened NO PiP window at all.** The 3.3.4 release
7
+ restored the entry — the OS ask is back inside `onPictureInPictureRequested()` — but the entry
8
+ path still cost up to 2.3 s, and the Activity is only RESUMED for a few hundred milliseconds
9
+ after the platform's request. Measured on emulator-5554 (API 37) on 2026-11-06, on the npm 3.3.4
10
+ build:
11
+
12
+ ```
13
+ 16:43:59.375 onPictureInPictureRequested: the platform asked the app to enter PiP
14
+ 16:43:59.432 hideConsumerChrome: hid the chrome view ... visibility=8
15
+ 16:43:59.805 commitPipParams: committed the PiP params (373 ms)
16
+ 16:44:01.003 the OS refused the PiP entry (1.2 s later)
17
+ ```
18
+
19
+ **⇒ THE CAUSE: the entry path read the engine's state FOUR times and made TWO binder round
20
+ trips.** `PlaybackState.live()` reads three mpv properties over JNI — `idle-active`,
21
+ `eof-reached`, `pause` — and each of those blocks until mpv's core thread answers. The entry
22
+ path read it for the "is media loaded" guard, then again for the params builder's `playing`
23
+ flag, then again for its `autoEnter` policy, and then it built the params a SECOND time for
24
+ the entry itself.
25
+
26
+ **The fix:**
27
+ - the state is read **ONCE**, and that one read answers the guard, the play/pause icon and the
28
+ auto-enter policy;
29
+ - the params are built **ONCE**, and the separate `setPictureInPictureParams` round trip before
30
+ the entry is gone — `enterPictureInPictureMode(params)` carries them.
31
+
32
+ **⇒ twelve blocking mpv property reads become three, one params build becomes one, and two
33
+ binder round trips become one.** The state is still read LIVE at the moment of the entry: the
34
+ play/pause icon in the PiP action row is not a cached flag.
35
+
36
+ ### Not claimed
37
+
38
+ - Whether the PiP window shows the app's chrome is **not** settled by this release, and this
39
+ release does not claim to settle it. Measured on 2026-11-06, on the npm 3.3.4 build: the PiP
40
+ window's **video layer is LIVE** — the frame advanced between two captures taken two minutes
41
+ apart — while its **UI layer is a FROZEN SNAPSHOT**, still showing the scrub row's `0:44 / 5:09`
42
+ from the moment of the entry. The window's draw counter did **not** move between the hide and
43
+ the entry (`6863 → 6863`): the hide and the entry run in the **same main-thread message**, so
44
+ no traversal can run between them, and the paused app never draws again. **The chrome-less
45
+ frame is never drawn — that is the measured cause, and it is a separate fix.**
46
+
47
+ ## [3.3.4] - 2026-11-06
48
+
49
+ ### Fixed
50
+
51
+ - **The HOME path opened NO Picture-in-Picture window at all.** This is a regression the 3.3.3
52
+ release shipped, and it is the reason this release exists. Measured on emulator-5554 (API 37) on
53
+ 2026-11-05, and again on 2026-11-06, on the 3.3.3 build:
54
+
55
+ ```
56
+ 15:21:28.392 onPictureInPictureRequested: the platform asked the app to enter PiP
57
+ 15:21:28.423 hideConsumerChrome: hid the chrome view ... visibility=8
58
+ 15:21:28.826 the PiP entry is scheduled for the next draw
59
+ 15:21:29.139 onUserLeaveHint: the user is leaving the app
60
+ 15:21:30.186 askForTheWindow: the OS refused the PiP entry
61
+ ```
62
+
63
+ `dumpsys activity activities` reported `mLastReportedPictureInPictureMode=false` — the entry
64
+ never happened. The cause is measured: 3.3.3 deferred the OS ask to the window's next draw, the
65
+ ask landed **1.8 s** after the platform's request, and by then the Activity had been paused.
66
+ `Activity.enterPictureInPictureMode` throws unless the Activity is RESUMED, so the OS refused
67
+ the entry.
68
+
69
+ **The fix:** the ask is synchronous again — inside `onPictureInPictureRequested()`, in that
70
+ tick, on every API level. The chrome is still hidden before the ask, and the params are still
71
+ committed before it.
72
+
73
+ ### Retracted
74
+
75
+ - **"The hide has to be DRAWN before the platform takes the window."** (3.3.3) That claim was
76
+ **never proven**. It rested on a single measurement — the window's draw counter did not move
77
+ between the hide and the entry — which is consistent with a stale frame AND with a draw that had
78
+ not been scheduled yet. A single screenshot cannot separate the two, and the release stated the
79
+ inference as a measured cause.
80
+
81
+ The mechanism built on that claim — a draw-gated entry on the API 31+ path, waiting for a
82
+ `ViewTreeObserver.OnDrawListener` — is removed here, in the same release that withdraws the
83
+ claim. The 3.3.3 CHANGELOG entry below still quotes the old wording on purpose: a retraction
84
+ that deletes the wrong text is not a retraction.
85
+
86
+ - **"A PiP window that still carries the chrome is better than no PiP window at all."** (3.3.3)
87
+ That was the decision behind the bounded wait, and it was wrong in practice: the wait did not
88
+ produce a PiP window with the chrome, it produced **no PiP window at all**. The entry is worth
89
+ more than the cosmetics.
90
+
91
+ - **"It returns whether the entry was started — on API 31+ that means it was scheduled for the
92
+ next draw."** (README) The entry is no longer scheduled for anything. The return value is the
93
+ OS's answer again: `true` when the Activity entered Picture-in-Picture, `false` when it was
94
+ refused.
95
+
96
+ ### Not claimed
97
+
98
+ - Whether the PiP window shows the app's chrome is **not** settled by this release. The hide is
99
+ applied natively before the entry, and on a playing video the PiP window comes up video-only; on
100
+ a paused video it has been seen carrying the app's full expanded chrome. The mechanism behind
101
+ that difference is still open, and it is measured per release rather than assumed.
102
+
2
103
  ## [3.3.3] - 2026-11-05
3
104
 
4
105
  ### Fixed
package/README.md CHANGED
@@ -387,9 +387,8 @@ path. The same library call serves both, so there is one entry and one owner. Wh
387
387
  hook matters is in [Why it is native](#why-it-is-native-and-why-your-activity-hands-over-the-activity).
388
388
 
389
389
  The library then hides your chrome, asks the OS for the window, and brings your chrome back when
390
- Picture-in-Picture ends. It returns whether the entry was **started** — on API 31+ that means it
391
- was scheduled for the next draw, because the chrome-less frame has to be on the screen before the
392
- platform takes the window — and `false` when the entry was refused, so you can log it:
390
+ Picture-in-Picture ends. It returns whether the OS **accepted** the entry, and `false` when the
391
+ entry was refused, so you can log it:
393
392
 
394
393
  ```kotlin
395
394
  val entered = com.simba.player.mpv.MpvBridgeModule.notifyUserLeaveHint(this)
@@ -419,10 +418,11 @@ your Activity is still **RESUMED**. That matters twice over:
419
418
 
420
419
  - `Activity.enterPictureInPictureMode()` throws `IllegalStateException` unless the Activity is
421
420
  resumed, so this is the only moment an explicit entry is legal at all.
422
- - the Activity is still **RESUMED** for a few hundred milliseconds after this hook, and that is
423
- the window the library uses to draw the chrome-less frame before it asks for the PiP window.
424
- Measured on emulator-5554 (API 37) on 2026-11-05: `onPictureInPictureRequested` at `33.942`,
425
- `onUserLeaveHint` at `34.300` — 358 ms of RESUMED time.
421
+ - the Activity is still **RESUMED** for a few hundred milliseconds after this hook, but the
422
+ library does NOT spend that window waiting: the OS ask happens **inside** this callback, in
423
+ this tick. Measured on emulator-5554 (API 37) on 2026-11-05 and again on 2026-11-06, an ask
424
+ deferred past the callback landed 1.8 s later, by which time the Activity had been paused, and
425
+ the OS **refused** the entry — so no PiP window opened at all.
426
426
 
427
427
  That is why the library does **not** arm the platform's auto-enter switch: with it armed there
428
428
  is no pre-transition hook to use.
@@ -441,23 +441,26 @@ Three things have to be right for that to work:
441
441
  2. **The entry happens while your Activity is resumed.** On API 31+ that means
442
442
  `onPictureInPictureRequested()`; see
443
443
  [Why it is native](#why-it-is-native-and-why-your-activity-hands-over-the-activity).
444
- 3. **The hide has to be DRAWN.** This is the one that is easy to get wrong, and it was measured:
445
- on API 31+ the library hides your chrome root and then asks the OS for the window — and the
446
- window showed the app's full expanded chrome anyway. The reason is that
447
- `View.setVisibility(GONE)` only **schedules** a traversal: in that run the window's draw
448
- counter did not move at all between the hide and the entry, so the hide never reached the
449
- pixels. The PiP window's UI layer is the app's **last drawn frame** — the video keeps playing
450
- inside it because the video is a `SurfaceView`, a separate layer the platform re-parents —
451
- so a frame that was never drawn cannot be in it.
452
-
453
- The library therefore forces the window to schedule a draw that carries the hidden chrome
454
- (`invalidate()` + `requestLayout()`) and asks the OS for the window **from that draw**. If the
455
- window never draws, the entry goes on anyway after a bounded wait: a PiP window that still
456
- carries your chrome is better than no PiP window at all.
457
-
458
- You can reproduce the defect without that fix: pause the video, so the app has nothing to
459
- animate and stops drawing, and then press HOME. With the video playing, the same press gives
460
- a clean, video-only PiP window.
444
+ 3. **The hide is applied natively, before the entry — and the PiP window keeps a FROZEN snapshot of
445
+ the app's UI layer.** Measured on emulator-5554 (API 37) on 2026-11-06, on the npm 3.3.4
446
+ build:
447
+
448
+ - the hide lands on the **right view** — the chrome root goes `GONE` and **stays** gone (the
449
+ same view identity before the hide and after `onPictureInPictureModeChanged(true)`), and
450
+ every child of it reads `shown=false` when the platform reports the mode change;
451
+ - the window's draw counter does **not** move between the hide and the entry (`6863 → 6863`),
452
+ because the hide and the entry run in the **same main-thread message** — and a traversal can
453
+ only run *between* main-thread messages;
454
+ - the PiP window's **video layer is LIVE** (the frame advanced between two captures taken two
455
+ minutes apart) while its **UI layer is the app's last drawn frame** (the scrub row still read
456
+ `0:44 / 5:09` in both captures).
457
+
458
+ So the PiP window carries the chrome because **the chrome-less frame was never drawn**. That
459
+ is the measured cause. The library does **not** claim to have fixed it — the 3.3.5 CHANGELOG's
460
+ "Not claimed" section says so in full.
461
+
462
+ If your app's PiP window shows your chrome, the first thing to check is whether the app is
463
+ still drawing at the moment of the entry.
461
464
 
462
465
  `PictureInPictureParams.setSourceRectHint(...)` is **not** part of this. It is a TRANSITION
463
466
  hint: it tells the platform which region of your window the PiP window should animate FROM. It
@@ -621,9 +624,8 @@ on leave does nothing, the Activity is not calling `MpvBridgeModule.notifyUserLe
621
624
  from `onPictureInPictureRequested()` (API 31+) or from `onUserLeaveHint()` (API 24-30) - see
622
625
  [Picture-in-Picture](#picture-in-picture).
623
626
 
624
- **The Picture-in-Picture window shows your app's chrome.** The chrome is hidden in the entry and
625
- that hide is **drawn** before the window is asked for — so the entry has to run while your Activity
626
- is resumed. On API
627
+ **The Picture-in-Picture window shows your app's chrome.** The chrome is hidden in the entry,
628
+ before the window is asked for — so the entry has to run while your Activity is resumed. On API
627
629
  31+ that means `onPictureInPictureRequested()` must return `true` and call the library:
628
630
  `onUserLeaveHint()` alone is **never called** there, because the platform's auto-enter switch
629
631
  transitions the window itself (measured: zero log lines on a HOME press). The library logs the
@@ -7,14 +7,12 @@ import android.graphics.Rect
7
7
  import android.media.MediaMetadataRetriever
8
8
  import android.util.Log
9
9
  import android.view.View
10
- import android.view.ViewTreeObserver
11
10
  import java.io.File
12
11
  import java.io.FileOutputStream
13
12
  import java.lang.ref.WeakReference
14
13
  import java.util.concurrent.ExecutorService
15
14
  import java.util.concurrent.Executors
16
15
  import java.util.concurrent.RejectedExecutionException
17
- import java.util.concurrent.atomic.AtomicBoolean
18
16
  import com.facebook.react.bridge.Arguments
19
17
  import com.facebook.react.bridge.Promise
20
18
  import com.facebook.react.bridge.ReadableArray
@@ -47,18 +45,6 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
47
45
  const val NAME = "MpvPlayerModule"
48
46
  private const val TAG = "MpvBridgeModule"
49
47
 
50
- /**
51
- * How long the native HOME entry waits for the window to DRAW the
52
- * chrome-less frame before it asks for the PiP window.
53
- *
54
- * Measured on emulator-5554 (API 37) on 2026-11-05: the platform
55
- * asked for PiP at `33.942` and the Activity was still RESUMED at
56
- * `34.300` — 358 ms of RESUMED time. The wait has to fit inside
57
- * that window, because `enterPictureInPictureMode` throws unless
58
- * the Activity is resumed.
59
- */
60
- private const val PIP_ENTRY_DRAW_TIMEOUT_MS = 250L
61
-
62
48
  // ── captureFrame: resume-rail thumbnails ──────────────────────────
63
49
  // `MediaMetadataRetriever` needs a real filesystem path for its
64
50
  // output. `filesDir` (NOT cacheDir) is deliberate: the platform
@@ -277,8 +263,14 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
277
263
  * 2026-11-04: with the hint committed, the PiP window showed the
278
264
  * app's full chrome, scaled down.
279
265
  *
280
- * The chrome is kept out by HIDING it before the entry — and by
281
- * having that hide DRAWN before the platform takes the window.
266
+ * The chrome is kept out by HIDING it before the entry.
267
+ *
268
+ * Whether that hide is SUFFICIENT — whether the PiP window still
269
+ * shows the chrome — is MEASURED per release, not assumed. The
270
+ * v3.3.3 release asserted that the hide had to be DRAWN before
271
+ * the platform took the window; that assertion was never proven,
272
+ * and the mechanism built on it broke the entry. See
273
+ * `notifyUserLeaveHint(activity)`.
282
274
  *
283
275
  * ## Why the restore still happens here
284
276
  *
@@ -492,110 +484,59 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
492
484
  *
493
485
  * @param activity the Activity that is leaving. It is the window
494
486
  * the PiP entry applies to.
495
- * @return `true` when the entry was STARTED — on API 31+ that
496
- * means it was scheduled for the next draw, because the
497
- * window must carry the chrome-less frame before the
498
- * platform takes it — and `false` when the entry was
499
- * refused.
487
+ * @return `true` when the Activity entered Picture-in-Picture,
488
+ * `false` when the entry was refused.
500
489
  */
501
490
  /**
502
- * Ask for the PiP window — but only AFTER the window has DRAWN the
503
- * chrome-less frame.
504
- *
505
- * ## Why the entry waits for a draw
506
- *
507
- * Measured on emulator-5554 (API 37) on 2026-11-05, on the v3.3.1
508
- * build:
509
- *
510
- * - the native hide lands on the RIGHT view: the chrome root went
511
- * GONE and STAYED GONE — the same view identity before the hide
512
- * and after the mode change;
513
- * - and yet the PiP window showed the app's FULL expanded chrome,
514
- * scaled down, over the live video.
515
- *
516
- * The reason is measured, not guessed. `View.setVisibility(GONE)`
517
- * only SCHEDULES a traversal: in that run the window's draw counter
518
- * did NOT move between the hide and the entry, so the hide never
519
- * reached the pixels.
520
- *
521
- * The PiP window's UI layer is the app's last drawn frame. The
522
- * video keeps playing inside it because the video is a
523
- * `SurfaceView` — a separate layer the platform re-parents into
524
- * the PiP window — while the app's UI layer is a snapshot. A
525
- * snapshot taken before the chrome-less frame was drawn carries the
526
- * chrome.
527
- *
528
- * So: hide the chrome, FORCE the window to schedule a draw that
529
- * carries the hidden chrome, and ask for the window from that draw.
530
- *
531
- * ## The wait is BOUNDED
532
- *
533
- * If the window never draws, the entry goes on anyway: a PiP
534
- * window that still carries the chrome is better than no PiP
535
- * window at all. The bound is
536
- * [PIP_ENTRY_DRAW_TIMEOUT_MS], which fits inside the RESUMED window
537
- * the platform leaves before it pauses the Activity.
491
+ * ## Why the window is asked for INSIDE the callback
492
+ *
493
+ * The ask is SYNCHRONOUS: it happens in this callback, in this
494
+ * tick, on every API level.
495
+ *
496
+ * Measured on emulator-5554 (API 37) on 2026-11-05, and again on
497
+ * 2026-11-06, on the v3.3.3 build — which DEFERRED the ask to the
498
+ * window's next draw:
499
+ *
500
+ * ```
501
+ * 15:21:28.392 onPictureInPictureRequested: the platform asked the app to enter PiP
502
+ * 15:21:28.423 hideConsumerChrome: hid the chrome view ... visibility=8
503
+ * 15:21:28.826 the PiP entry is scheduled for the next draw
504
+ * 15:21:29.139 onUserLeaveHint: the user is leaving the app
505
+ * 15:21:30.186 askForTheWindow: the OS refused the PiP entry
506
+ * ```
507
+ *
508
+ * The deferred ask landed 1.8 s after the platform's request —
509
+ * long after the Activity had been paused.
510
+ * `Activity.enterPictureInPictureMode` throws unless the Activity
511
+ * is RESUMED, so the OS refused the entry and **NO PiP window
512
+ * opened at all**. Both runs were identical, and
513
+ * `dumpsys activity activities` reported
514
+ * `mLastReportedPictureInPictureMode=false`.
515
+ *
516
+ * A PiP window that still carries the chrome is a defect. NO PiP
517
+ * window at all is worse, so the entry wins over the cosmetics.
518
+ *
519
+ * ## What v3.3.3 claimed, and why it is withdrawn
520
+ *
521
+ * v3.3.3 asserted that the chrome's hide had to be DRAWN before
522
+ * the platform took the window, and it gated the entry on a
523
+ * `ViewTreeObserver.OnDrawListener`. That claim was **never
524
+ * proven**. The one measurement it rested on — the window's draw
525
+ * counter did not move between the hide and the entry — is
526
+ * consistent with a stale frame AND with a draw that had not been
527
+ * scheduled yet, and a single screenshot cannot separate the two.
528
+ *
529
+ * The mechanism built on that claim broke the entry outright (see
530
+ * above). It is withdrawn here, in the same release that removes
531
+ * the code.
532
+ *
533
+ * ## What is still open
534
+ *
535
+ * Whether the PiP window shows the chrome is MEASURED, not
536
+ * assumed. The hide-before-ask is kept because it is the only
537
+ * ordering that can help and because it is harmless; whether it
538
+ * is SUFFICIENT is not settled by this file.
538
539
  */
539
- private fun enterPictureInPictureAfterDraw(activity: Activity): Boolean {
540
- val root = activity.window?.decorView
541
- if (root == null) {
542
- // No window to wait for: ask for the window now.
543
- return enterPictureInPicture(activity)
544
- }
545
- val observer = root.viewTreeObserver
546
- val asked = AtomicBoolean(false)
547
- val listener = object : ViewTreeObserver.OnDrawListener {
548
- override fun onDraw() {
549
- if (!asked.compareAndSet(false, true)) return
550
- removeDrawListener(observer, this)
551
- askForTheWindow(activity)
552
- }
553
- }
554
- // FORCE the window to schedule a draw that carries the hidden
555
- // chrome. The visibility change alone did not, and that is the
556
- // whole defect.
557
- root.invalidate()
558
- root.requestLayout()
559
- try {
560
- observer.addOnDrawListener(listener)
561
- } catch (e: IllegalStateException) {
562
- // The tree is not alive: there is nothing to wait for.
563
- return enterPictureInPicture(activity)
564
- }
565
- // The bound: a window that never draws must not hold the entry.
566
- root.postDelayed({
567
- if (asked.compareAndSet(false, true)) {
568
- removeDrawListener(observer, listener)
569
- askForTheWindow(activity)
570
- }
571
- }, PIP_ENTRY_DRAW_TIMEOUT_MS)
572
- return true
573
- }
574
-
575
- /**
576
- * Remove a draw listener without letting a dead view tree throw.
577
- */
578
- private fun removeDrawListener(
579
- observer: ViewTreeObserver,
580
- listener: ViewTreeObserver.OnDrawListener,
581
- ) {
582
- try {
583
- observer.removeOnDrawListener(listener)
584
- } catch (e: IllegalStateException) {
585
- // The tree died with the window: there is nothing to remove.
586
- }
587
- }
588
-
589
- /**
590
- * The one call that asks the OS for the window, and the one place
591
- * the OS's answer is read.
592
- */
593
- private fun askForTheWindow(activity: Activity) {
594
- if (!enterPictureInPicture(activity)) {
595
- Log.w(TAG, "companion.askForTheWindow: the OS refused the PiP entry")
596
- }
597
- }
598
-
599
540
  fun notifyUserLeaveHint(activity: Activity): Boolean {
600
541
  Log.i(
601
542
  TAG,
@@ -623,7 +564,19 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
623
564
  }
624
565
  // 3. No media loaded: a PiP window for a stopped session is
625
566
  // a bug, not a feature.
626
- if (!isMediaLoaded()) {
567
+ //
568
+ // The state is read ONCE, here, and the SAME read answers
569
+ // this guard and the params below. It used to be read
570
+ // twice — once for the guard and once again inside the
571
+ // params builder — and `PlaybackState.live()` reads three
572
+ // mpv properties over JNI, each of which blocks until mpv's
573
+ // core thread answers. Measured on emulator-5554 (API 37) on
574
+ // 2026-11-06: the entry path made TWELVE such reads, and
575
+ // the entry succeeded only when the whole path happened to
576
+ // fit inside the few hundred milliseconds the Activity stays
577
+ // RESUMED.
578
+ val state = PlaybackState.live()
579
+ if (!PlaybackState.hasMedia(state)) {
627
580
  Log.i(TAG, "companion.notifyUserLeaveHint: no media loaded; not entering PiP")
628
581
  return false
629
582
  }
@@ -649,49 +602,49 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
649
602
  // FULL chrome, scaled down.
650
603
  hideConsumerChrome()
651
604
  rememberPipActivity(activity)
652
- // 5. Commit the params BEFORE the window is asked for.
605
+ // 5. Ask for the window — SYNCHRONOUSLY, in this callback, on
606
+ // every API level.
653
607
  //
654
- // The platform builds the PiP window from the LAST
655
- // COMMITTED params. The source rect hint among them is the
656
- // region the PiP window ANIMATES FROM — a transition hint,
657
- // not a crop.
658
- commitPipParams(activity)
659
- // 6. Ask for the window — on API 31+ only AFTER the window has
660
- // DRAWN the chrome-less frame.
608
+ // The entry path READS the state ONCE and COMMITS NOTHING:
609
+ // `enterPictureInPictureMode(params)` carries the params, so
610
+ // the separate `setPictureInPictureParams` round trip before
611
+ // it is gone.
612
+ //
613
+ // That is a measured fix, not a tidy-up. Measured on
614
+ // emulator-5554 (API 37) on 2026-11-06, the HOME entry was
615
+ // FLAKY: 2 of 5 HOME presses opened a PiP window and the
616
+ // other 3 were REFUSED by the OS. The log splits the entry
617
+ // path's latency:
661
618
  //
662
- // This is an EXPLICIT entry on every API level. On API 31+
663
- // it is legal here because the platform's auto-enter
664
- // switch is not armed: ATMS refuses a client entry only
665
- // while auto-enter is enabled ("auto-enter should take in
666
- // place instead").
619
+ // ```
620
+ // 16:43:59.432 hideConsumerChrome
621
+ // 16:43:59.805 commitPipParams: committed the PiP params (373 ms)
622
+ // 16:44:01.003 the OS refused the PiP entry (1.2 s later)
623
+ // ```
667
624
  //
668
- // On API 31+ the entry runs from
669
- // `Activity.onPictureInPictureRequested()`, while the
670
- // Activity is still RESUMED — so there is time for one
671
- // frame, and the window is asked for only once the
672
- // chrome-less frame is on the screen. Measured on
673
- // emulator-5554 (API 37) on 2026-11-05: asking for the
674
- // window in the SAME tick as the hide put the app's
675
- // PRE-HIDE frame in the PiP window, and that window carried
676
- // the app's FULL expanded chrome, scaled down.
625
+ // The path built the params TWICE — each build reads several
626
+ // mpv properties over JNI, and `mpv_get_property` blocks
627
+ // until mpv's core thread answers — and made two binder
628
+ // round trips. It cost 0.1-2.3 s. The Activity is only
629
+ // RESUMED for a few hundred milliseconds after the
630
+ // platform's request, so the entry succeeded only when the
631
+ // path happened to be fast.
632
+ //
633
+ // The ask MUST NOT be deferred either. Measured on
634
+ // emulator-5554 (API 37) on 2026-11-05 and again on
635
+ // 2026-11-06: a deferred ask (waiting for the window's next
636
+ // draw) landed 1.8 s after the platform's request, the
637
+ // Activity had been paused by then, and the OS REFUSED the
638
+ // entry — so NO PiP window opened at all.
639
+ // `enterPictureInPictureMode` needs a RESUMED Activity, and
640
+ // this callback is the last moment the Activity is
641
+ // guaranteed to be one.
677
642
  //
678
643
  // On API 24-30 the entry runs from `onUserLeaveHint()`,
679
644
  // which AOSP calls "right before the activity's onPause
680
- // callback": there is no frame between it and the pause,
681
- // and `enterPictureInPictureMode` needs a RESUMED Activity.
682
- // The entry there is immediate.
683
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
684
- if (!enterPictureInPictureAfterDraw(activity)) {
685
- Log.w(TAG, "companion.notifyUserLeaveHint: the OS refused the PiP entry")
686
- return false
687
- }
688
- Log.i(
689
- TAG,
690
- "companion.notifyUserLeaveHint: the PiP entry is scheduled for the next draw",
691
- )
692
- return true
693
- }
694
- val entered = enterPictureInPicture(activity)
645
+ // callback": there is no frame between it and the pause.
646
+ // The entry there is immediate too.
647
+ val entered = enterPictureInPicture(activity, state = state)
695
648
  if (!entered) {
696
649
  Log.w(TAG, "companion.notifyUserLeaveHint: the OS refused the PiP entry")
697
650
  return false
@@ -773,6 +726,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
773
726
  activity: Activity,
774
727
  chapterTitle: String? = null,
775
728
  progressPct: String? = null,
729
+ state: String = PlaybackState.live(),
776
730
  ): android.app.PictureInPictureParams {
777
731
  // The rect of the window the VIDEO occupies. The PiP window
778
732
  // animates FROM it — a transition hint, not a crop.
@@ -790,33 +744,19 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
790
744
  // is read from mpv, which is the same fact.
791
745
  chapterTitle = chapterTitle ?: nativeTitle() ?: DEFAULT_PIP_TITLE,
792
746
  progressPercentage = progressPct ?: nativeProgressPct() ?: DEFAULT_PIP_PROGRESS,
793
- playing = isPlaying(),
747
+ // The SAME live read answers both questions. Reading it
748
+ // twice was two thirds of the entry path's mpv traffic.
749
+ playing = PlaybackState.isPlaying(state),
794
750
  // The CONSUMER'S POLICY: whether the Activity may enter PiP
795
751
  // when the user leaves the app. It is checked by the library
796
752
  // before it enters, and it is NOT a platform switch - the
797
753
  // platform's own auto-enter switch is never armed, because
798
754
  // the platform would transition the window before the app
799
755
  // could hide its chrome.
800
- autoEnter = isMediaLoaded(),
756
+ autoEnter = PlaybackState.hasMedia(state),
801
757
  )
802
758
  }
803
759
 
804
- /**
805
- * Whether the engine is playing right now.
806
- *
807
- * Read from mpv's REAL properties (`idle-active`, `eof-reached`,
808
- * `pause`) at the moment of use, and never from a JS flag: the PiP
809
- * action's icon has to be true even when the app is paused and no
810
- * JS is running.
811
- */
812
- private fun isPlaying(): Boolean {
813
- // ONE owner: `PlaybackState`, which reads mpv's REAL
814
- // properties. This used to read `playback-state`, a property
815
- // mpv does NOT have, so the answer was always "not playing"
816
- // and the PiP action's icon never changed.
817
- return PlaybackState.isPlayingNow()
818
- }
819
-
820
760
  /**
821
761
  * The ONE owner of "how a PiP window is built".
822
762
  *
@@ -838,6 +778,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
838
778
  activity: Activity,
839
779
  chapterTitle: String? = null,
840
780
  progressPct: String? = null,
781
+ state: String = PlaybackState.live(),
841
782
  ): Boolean {
842
783
  // `return try`, not a bare `try`. A `try`/`catch` in
843
784
  // STATEMENT position is not this function's return value in
@@ -850,7 +791,23 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
850
791
  // The ONE params builder: the same facts, the same
851
792
  // rect, the same action — as the ones the library
852
793
  // commits to the Activity outside the entry moment.
853
- val pipParams = buildCurrentPipParams(activity, chapterTitle, progressPct)
794
+ //
795
+ // The HOME path carries no title and no progress of its
796
+ // own, so it uses the params the library ALREADY built
797
+ // instead of building a second set. The build reads
798
+ // several mpv properties over JNI and blocks until
799
+ // mpv's core thread answers, and the entry path cannot
800
+ // afford that latency: measured on emulator-5554 (API
801
+ // 37) on 2026-11-06, building them twice made the
802
+ // entry cost 0.1-2.3 s, and the Activity is only RESUMED
803
+ // for a few hundred milliseconds — so 3 of 5 HOME
804
+ // presses were REFUSED by the OS.
805
+ val pipParams = buildCurrentPipParams(
806
+ activity,
807
+ chapterTitle,
808
+ progressPct,
809
+ state,
810
+ )
854
811
  // `enterPictureInPictureMode` ANSWERS whether the OS
855
812
  // accepted the request, and the answer is the only
856
813
  // authority on this.
@@ -891,25 +848,6 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
891
848
  }
892
849
  }
893
850
 
894
- /**
895
- * Whether mpv currently has media loaded.
896
- *
897
- * Read live through [PlaybackHost] — never from a JS flag, which
898
- * can be stale by the time the user presses HOME. The question is
899
- * answered by mpv's REAL properties (`idle-active`, `eof-reached`,
900
- * `pause`), and the vocabulary is the one the library has always
901
- * published: `idle` and `stopped` mean no media, and `paused` and
902
- * `playing` mean something is still loaded.
903
- */
904
- private fun isMediaLoaded(): Boolean {
905
- // ONE owner: `PlaybackState`, which reads mpv's REAL
906
- // properties. This used to read `playback-state`, a property
907
- // mpv does NOT have — the read failed, and "not idle, not
908
- // stopped" was therefore true for EVERY value, so the library
909
- // thought media was always loaded.
910
- return PlaybackState.hasMediaNow()
911
- }
912
-
913
851
  /**
914
852
  * The title for the PiP notification, read from mpv.
915
853
  *
@@ -1003,8 +941,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
1003
941
  * chrome, scaled down.
1004
942
  *
1005
943
  * The chrome is kept out by HIDING it before the entry, in native
1006
- * code, and by having that hide DRAWN before the platform takes the
1007
- * window — see `notifyUserLeaveHint(activity)`.
944
+ * code — see `notifyUserLeaveHint(activity)`.
1008
945
  *
1009
946
  * It is **not** the picture view's box. The picture fills its lane
1010
947
  * (`StyleSheet.absoluteFill`), so the view's rect is the whole window
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "3.3.3",
3
+ "version": "3.3.5",
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",