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

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,119 @@
1
1
 
2
+ ## [3.3.3] - 2026-11-05
3
+
4
+ ### Fixed
5
+
6
+ - **The HOME path's PiP window could carry the app's full expanded chrome, scaled down.** The
7
+ 3.3.1 release shipped the entry ORDER — hide the chrome, then ask the OS for the window — and
8
+ that order is right, but it was not enough, and the release said it was. Measured on
9
+ emulator-5554 (API 37) on 2026-11-05, with an instrumented build that logged the chrome view's
10
+ identity, its visibility and the window's draw count at every stage:
11
+
12
+ - the native hide lands on the **right view**: the chrome root went `GONE` and **stayed** gone —
13
+ the same view identity before the hide and after `onPictureInPictureModeChanged(true)`;
14
+ - and yet the PiP window showed the app's **full** expanded chrome — the header band, the ±10
15
+ buttons, the big pause button, the scrub row — over the live video.
16
+
17
+ The cause is measured, not guessed. `View.setVisibility(GONE)` only **schedules** a traversal:
18
+ in that run the window's draw counter did **not move at all** between the hide and the entry, so
19
+ the hide never reached the pixels. The PiP window's UI layer is the app's **last drawn frame** —
20
+ the video keeps playing inside it because the video is a `SurfaceView`, a separate layer the
21
+ platform re-parents into the PiP window, while the app's UI layer is a snapshot. A frame that
22
+ was never drawn cannot be in it.
23
+
24
+ The defect is reproducible without the fix: **pause the video**, so the app has nothing to
25
+ animate and stops drawing, and then press HOME. With the video playing, the same press gives a
26
+ clean, video-only PiP window.
27
+
28
+ **The fix:** the entry now hides the chrome, **forces** the window to schedule a draw that
29
+ carries the hidden chrome (`invalidate()` + `requestLayout()`), and asks the OS for the window
30
+ **from that draw** — a one-shot `ViewTreeObserver.OnDrawListener`. If the window never draws,
31
+ the entry goes on anyway after a bounded wait (250 ms), because a PiP window that still carries
32
+ the chrome is better than no PiP window at all.
33
+
34
+ On API 24-30 the entry is unchanged and immediate: there it runs from `onUserLeaveHint()`, which
35
+ AOSP calls "right before the activity's onPause callback", so there is no frame to wait for and
36
+ `enterPictureInPictureMode()` needs a RESUMED Activity.
37
+
38
+ ### Retracted
39
+
40
+ - **"The chrome is kept out by HIDING it before the entry."** That was the 3.3.1/3.3.2 mechanism,
41
+ and it was **not** enough: the hide has to be **drawn** before the platform takes the window.
42
+ Five shipped places asserted the old mechanism — `MpvBridgeModule.kt` (two docblocks),
43
+ `PipManager.kt`, `src/types/config.ts` and the README — and all of them now say what the
44
+ measurement says. The 3.3.1 CHANGELOG entry below still quotes the old wording on purpose: a
45
+ retraction that deletes the wrong text is not a retraction.
46
+
47
+ - **"Your app is still drawing, so the chrome-less window is presented before the platform
48
+ transitions it."** (README) — the app is still **RESUMED** for a few hundred milliseconds after
49
+ `onPictureInPictureRequested()`, but *drawing* is a different thing, and a paused video leaves
50
+ the app with nothing to draw. The README now says what is true.
51
+
52
+ - **"It returns whether the OS accepted the entry."** (README) — on API 31+ the entry is
53
+ **scheduled** for the next draw, so the return value says the entry was *started*. The OS's
54
+ answer is logged, not returned.
55
+
56
+ ## [3.3.2] - 2026-11-04
57
+
58
+ ### Retracted
59
+
60
+ - **"The platform crops your window to the rect in `setSourceRectHint(...)`."** The 3.3.1
61
+ release retracted this claim in the README and in the CHANGELOG, but **four places in the
62
+ library's SOURCE still asserted it**, and one of them was in the TypeScript API docs every
63
+ consumer reads:
64
+
65
+ - `MpvBridgeModule.kt` — the docblock of `onPictureInPictureModeChanged` (*"Two mechanisms …
66
+ 1. **The crop (primary).** The platform CROPS the app's window …"*), the docblock of
67
+ `pipSourceRectInWindow()` (*"The platform CROPS the app's window to that rect … which is
68
+ what keeps the app's chrome out of the PiP window"*), and an inline comment.
69
+ - `MpvRenderView.kt` — an inline comment.
70
+ - `src/types/config.ts` — the `autoEnterOnLeave` docblock, which also claimed that the library
71
+ *"arms Android's own auto-enter switch (`PictureInPictureParams.Builder
72
+ .setAutoEnterEnabled`), but ONLY while media is loaded"*. **That is false too**: the switch
73
+ is **not armed at all**, because the platform's auto-enter transitions the window through the
74
+ Shell transition and the app's hooks are never consulted.
75
+
76
+ All five now say what is true: `setSourceRectHint` is a **TRANSITION hint** — the region the
77
+ PiP window animates **from** — and the app's chrome is kept out by **hiding it before the
78
+ entry**, in native code.
79
+
80
+ **A retraction that leaves the wrong claim standing in the source is not a retraction.** The
81
+ claim is now pinned by a test (`PipSourceRectClaimTest`): no shipped file may assert the crop,
82
+ and a block that quotes the old claim has to say that it is false.
83
+
84
+ - **Two more stale prose claims, found by the same sweep.** The 3.3.1 release's own guard read
85
+ the library's **code** with the comments stripped, so a docblock that contradicted the code was
86
+ invisible to it. Two families survived into the published package:
87
+
88
+ - `MpvBridgeModule.kt` — the docblock of `isPlaying()` said *"Read from mpv's `playback-state`
89
+ at the moment of use"*. `playback-state` is **not an mpv property** — that is the whole reason
90
+ the PiP play/pause button was inert — so the docblock described a read that does not exist and
91
+ never did.
92
+ - `MpvBridgeModule.kt` and `PipManager.kt` — six places still described the library as **arming
93
+ the platform's auto-enter switch** *"only while media is loaded"*. The switch is not armed at
94
+ all; `autoEnter` is a **consumer policy** the library checks before it enters.
95
+
96
+ Both families are corrected, and both are now pinned by a test (`PiPFactClaimTest`).
97
+
98
+ ### Added
99
+
100
+ - `PipSourceRectClaimTest` — a plain-JUnit guard (no device, no Robolectric, no network) that
101
+ fails if any source file, or the shipped README, asserts that the platform crops the app's
102
+ window. It also asserts that the scan **reaches** the files the claim lived in, because a
103
+ guard that silently covers nothing is not a guard.
104
+ - `PiPFactClaimTest` — the same shape for the other two PiP facts: no file may claim that the
105
+ library arms the platform's auto-enter switch, and no file may claim the playback state is read
106
+ from `playback-state`.
107
+ - `ClaimScan` — the shared scanner behind both, so a third guard does not have to re-implement
108
+ "what counts as a paragraph" and get it subtly different.
109
+
110
+ ### Changed
111
+
112
+ - **Nothing behavioural.** 3.3.2 is a documentation-and-guard release: the runtime behaviour is
113
+ exactly 3.3.1's. If you are on 3.3.1, you do not need 3.3.1's successor for behaviour — but
114
+ 3.3.1's README is the shipped documentation, and a false claim in it is a false claim in
115
+ every install.
116
+
2
117
  ## [3.3.1] - 2026-11-04
3
118
 
4
119
  ### Fixed
package/README.md CHANGED
@@ -386,8 +386,10 @@ callback regardless of if it acts on it or not", and `false` sends the framework
386
386
  path. The same library call serves both, so there is one entry and one owner. Why the API 31+
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
- The library then asks the OS for the window, hides your chrome, and brings your chrome back when
390
- Picture-in-Picture ends. It returns whether the OS accepted the entry, so you can log it:
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:
391
393
 
392
394
  ```kotlin
393
395
  val entered = com.simba.player.mpv.MpvBridgeModule.notifyUserLeaveHint(this)
@@ -417,8 +419,10 @@ your Activity is still **RESUMED**. That matters twice over:
417
419
 
418
420
  - `Activity.enterPictureInPictureMode()` throws `IllegalStateException` unless the Activity is
419
421
  resumed, so this is the only moment an explicit entry is legal at all.
420
- - your app is still drawing, so the chrome-less window is presented before the platform
421
- transitions it.
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.
422
426
 
423
427
  That is why the library does **not** arm the platform's auto-enter switch: with it armed there
424
428
  is no pre-transition hook to use.
@@ -428,7 +432,7 @@ is no pre-transition hook to use.
428
432
  The PiP window **is** your Activity's own window, so whatever your chrome draws is what the PiP
429
433
  window shows. The library hides your chrome root **before** it asks the OS for the window.
430
434
 
431
- Two things have to be right for that to work:
435
+ Three things have to be right for that to work:
432
436
 
433
437
  1. **The hide happens in the entry, not after it.** Hiding from
434
438
  `onPictureInPictureModeChanged(true)` — after the platform has already transitioned the
@@ -437,6 +441,23 @@ Two things have to be right for that to work:
437
441
  2. **The entry happens while your Activity is resumed.** On API 31+ that means
438
442
  `onPictureInPictureRequested()`; see
439
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.
440
461
 
441
462
  `PictureInPictureParams.setSourceRectHint(...)` is **not** part of this. It is a TRANSITION
442
463
  hint: it tells the platform which region of your window the PiP window should animate FROM. It
@@ -600,8 +621,9 @@ on leave does nothing, the Activity is not calling `MpvBridgeModule.notifyUserLe
600
621
  from `onPictureInPictureRequested()` (API 31+) or from `onUserLeaveHint()` (API 24-30) - see
601
622
  [Picture-in-Picture](#picture-in-picture).
602
623
 
603
- **The Picture-in-Picture window shows your app's chrome.** The chrome is hidden in the entry,
604
- before the window is asked for — so the entry has to run while your Activity is resumed. On API
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
605
627
  31+ that means `onPictureInPictureRequested()` must return `true` and call the library:
606
628
  `onUserLeaveHint()` alone is **never called** there, because the platform's auto-enter switch
607
629
  transitions the window itself (measured: zero log lines on a HOME press). The library logs the
@@ -17,12 +17,21 @@ import androidx.annotation.RequiresApi
17
17
  * Manages Android Picture-in-Picture (PiP) overlay actions and parameters
18
18
  * for the `PlayerActivity` in `@simba/react-native-media-player`.
19
19
  *
20
+ * v3.3.1:
21
+ * - ONE owner for the playback state: `PlaybackState`, which reads mpv's
22
+ * REAL properties (`idle-active`, `eof-reached`, `pause`)
23
+ * - The platform's own auto-enter switch is NOT armed - the app owns the
24
+ * entry, and the chrome is hidden IN THE ENTRY, before the window is
25
+ * asked for
26
+ * - The source-rect hint is a TRANSITION hint, not a crop - the crop
27
+ * claim is retracted
28
+ *
20
29
  * v3.3.0:
21
30
  * - ONE RemoteAction: [Play/Pause], with a state-driven icon
22
31
  * - Notification text with chapter title + progress percentage (Android 12+)
23
32
  * - No NEXT/PREV actions (removed for simplicity)
24
33
  * - The platform's own auto-enter switch, armed only while media is loaded
25
- * (Android 12+)
34
+ * (Android 12+) - RETRACTED in 3.3.1: the switch is not armed at all
26
35
  *
27
36
  * ## Who owns the action receiver
28
37
  *
@@ -99,9 +108,11 @@ object PipManager {
99
108
  * @param progressPercentage Percentage string like "45%" for notification text (Android 12+).
100
109
  * @param playing Whether the video is playing right now — it picks
101
110
  * the action's icon.
102
- * @param autoEnter Whether the platform may enter PiP by itself when
103
- * the user leaves the app (Android 12+). Armed only
104
- * while media is loaded.
111
+ * @param autoEnter Whether the Activity may enter PiP by itself when
112
+ * the user leaves the app. This is a CONSUMER
113
+ * POLICY, checked by the library before it enters -
114
+ * it is NOT a platform switch, and the platform's
115
+ * own auto-enter switch is never armed.
105
116
  */
106
117
  fun buildPipParams(
107
118
  context: Context,
@@ -205,7 +216,8 @@ object PipManager {
205
216
  // This is the rect the PiP window ANIMATES FROM — a transition
206
217
  // hint, nothing more. It does not crop the app's window, so it is
207
218
  // NOT what keeps the app's chrome out of the PiP window; the
208
- // consumer's chrome is hidden before the entry, in native code.
219
+ // consumer's chrome is hidden before the entry, in native code,
220
+ // and that hide is DRAWN before the platform takes the window.
209
221
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && sourceRectHint != null) {
210
222
  builder.setSourceRectHint(sourceRectHint)
211
223
  }
@@ -7,12 +7,14 @@ 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
10
11
  import java.io.File
11
12
  import java.io.FileOutputStream
12
13
  import java.lang.ref.WeakReference
13
14
  import java.util.concurrent.ExecutorService
14
15
  import java.util.concurrent.Executors
15
16
  import java.util.concurrent.RejectedExecutionException
17
+ import java.util.concurrent.atomic.AtomicBoolean
16
18
  import com.facebook.react.bridge.Arguments
17
19
  import com.facebook.react.bridge.Promise
18
20
  import com.facebook.react.bridge.ReadableArray
@@ -45,6 +47,18 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
45
47
  const val NAME = "MpvPlayerModule"
46
48
  private const val TAG = "MpvBridgeModule"
47
49
 
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
+
48
62
  // ── captureFrame: resume-rail thumbnails ──────────────────────────
49
63
  // `MediaMetadataRetriever` needs a real filesystem path for its
50
64
  // output. `filesDir` (NOT cacheDir) is deliberate: the platform
@@ -235,54 +249,42 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
235
249
  * Mirrors the companion sendEvent pattern from
236
250
  * https://yor-dev.com/react-native-picture-in-picture-native-in-android/
237
251
  *
238
- * ## The chrome is hidden HERE, and not where the window is asked for
239
- *
240
- * `Activity.onUserLeaveHint()` is called as part of the Activity's
241
- * PAUSE transaction. There is NO frame between the hide and the
242
- * pause, so a `visibility = View.GONE` applied there never reaches
243
- * the pixels: the PiP window is built from the window's LAST
244
- * RENDERED BUFFER, and that buffer still holds the chrome.
245
- * Measured on emulator-5554 (API 37) on 2026-11-03: the native
246
- * hide ran ~40 ms before the OS was asked for the window, and the
247
- * PiP window still showed the app's full expanded chrome, scaled
248
- * down, with dead buttons.
249
- *
250
- * By the time this callback runs, the platform HAS entered PiP and
251
- * the Activity's window IS the PiP window - so the hide is painted.
252
- *
253
- * ## Two mechanisms keep the app's chrome out of the PiP window,
254
- * and this callback is the SECOND of them
255
- *
256
- * 1. **The crop (primary).** The platform CROPS the app's window to
257
- * the rect in `PictureInPictureParams.setSourceRectHint(...)`
258
- * and scales that crop into the PiP window. The library
259
- * commits that hint EARLY — `Activity.setPictureInPictureParams
260
- * (...)` — and keeps it current, and the hint is the region of
261
- * the window the VIDEO occupies. So the chrome around the video
262
- * is cropped away by the platform itself. (Google's own
263
- * `androidx.core-pip` does exactly this: it tracks the player
264
- * view's bounds and feeds them as the source rect.)
265
- * 2. **The hide (second line of defence, and this callback).** The
266
- * chrome the app draws OVER the video — the big pause button,
267
- * the scrub row — is INSIDE the crop, so the crop cannot remove
268
- * it. It is removed here.
269
- *
270
- * ## Why the hide happens HERE and not where the window is asked for
271
- *
272
- * `Activity.onUserLeaveHint()` is called as part of the Activity's
273
- * PAUSE transaction. There is NO frame between the hide and the
274
- * pause, so a `visibility = View.GONE` applied there never reaches
275
- * the pixels: the PiP window is built from the window's LAST
276
- * RENDERED BUFFER, and that buffer still holds the chrome.
277
- * Measured on emulator-5554 (API 37) on 2026-11-03: the native
278
- * hide ran ~40 ms before the OS was asked for the window, and the
279
- * PiP window still showed the app's full expanded chrome, scaled
280
- * down, with dead buttons.
281
- *
282
- * By the time this callback runs, the platform HAS entered PiP and
283
- * the Activity's window IS the PiP window — and the platform's own
284
- * documentation says the activity is in a "paused, but rendering"
285
- * state there, so an invalidation made now IS drawn.
252
+ * ## The chrome is hidden in the ENTRY, and this callback only restores it
253
+ *
254
+ * The chrome is hidden in `notifyUserLeaveHint(activity)` — the
255
+ * entry — BEFORE the window is asked for. That is the only moment a
256
+ * hide can reach the pixels: the PiP window IS the Activity's own
257
+ * window, and it shows that window's LAST RENDERED FRAME.
258
+ *
259
+ * Measured on emulator-5554 (API 37) on 2026-11-04: with the hide
260
+ * in `onPictureInPictureModeChanged(true)` — AFTER the platform had
261
+ * already transitioned the window — the PiP window showed the app's
262
+ * FULL expanded chrome, scaled down, with a frozen clock.
263
+ *
264
+ * So the hide here is a SECOND line of defence, not the mechanism:
265
+ * by the time this callback runs, the platform has already entered
266
+ * PiP and the window is already frozen.
267
+ *
268
+ * ## There is NO crop
269
+ *
270
+ * The 3.2.0 and 3.3.0 releases of this library claimed that the
271
+ * platform "CROPS the app's window to the rect in
272
+ * `setSourceRectHint(...)`" and that this crop is what keeps the
273
+ * app's chrome out of the PiP window. **That is false.**
274
+ * `setSourceRectHint` is a TRANSITION hint: it tells the platform
275
+ * which region of the window the PiP window should animate FROM.
276
+ * It does not crop anything. Measured on emulator-5554 (API 37) on
277
+ * 2026-11-04: with the hint committed, the PiP window showed the
278
+ * app's full chrome, scaled down.
279
+ *
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.
282
+ *
283
+ * ## Why the restore still happens here
284
+ *
285
+ * The chrome comes back when PiP ends, and it comes back EVEN IF
286
+ * the React instance is gone: the view is a native one, and the app
287
+ * is showing it (or not showing it) either way.
286
288
  *
287
289
  * @param newConfig the platform's new configuration. It is logged
288
290
  * (it carries the PiP WINDOW's size in dp) and the work is
@@ -490,9 +492,110 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
490
492
  *
491
493
  * @param activity the Activity that is leaving. It is the window
492
494
  * the PiP entry applies to.
493
- * @return `true` when the Activity entered Picture-in-Picture,
494
- * `false` when it did not.
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.
500
+ */
501
+ /**
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.
538
+ */
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.
495
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
+
496
599
  fun notifyUserLeaveHint(activity: Activity): Boolean {
497
600
  Log.i(
498
601
  TAG,
@@ -553,13 +656,41 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
553
656
  // region the PiP window ANIMATES FROM — a transition hint,
554
657
  // not a crop.
555
658
  commitPipParams(activity)
556
- // 6. Ask for the window.
659
+ // 6. Ask for the window — on API 31+ only AFTER the window has
660
+ // DRAWN the chrome-less frame.
557
661
  //
558
662
  // This is an EXPLICIT entry on every API level. On API 31+
559
663
  // it is legal here because the platform's auto-enter
560
664
  // switch is not armed: ATMS refuses a client entry only
561
665
  // while auto-enter is enabled ("auto-enter should take in
562
666
  // place instead").
667
+ //
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.
677
+ //
678
+ // On API 24-30 the entry runs from `onUserLeaveHint()`,
679
+ // 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
+ }
563
694
  val entered = enterPictureInPicture(activity)
564
695
  if (!entered) {
565
696
  Log.w(TAG, "companion.notifyUserLeaveHint: the OS refused the PiP entry")
@@ -660,9 +791,12 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
660
791
  chapterTitle = chapterTitle ?: nativeTitle() ?: DEFAULT_PIP_TITLE,
661
792
  progressPercentage = progressPct ?: nativeProgressPct() ?: DEFAULT_PIP_PROGRESS,
662
793
  playing = isPlaying(),
663
- // Auto-enter is armed ONLY while media is loaded: the
664
- // platform would otherwise throw the user into a PiP
665
- // window of a stopped session.
794
+ // The CONSUMER'S POLICY: whether the Activity may enter PiP
795
+ // when the user leaves the app. It is checked by the library
796
+ // before it enters, and it is NOT a platform switch - the
797
+ // platform's own auto-enter switch is never armed, because
798
+ // the platform would transition the window before the app
799
+ // could hide its chrome.
666
800
  autoEnter = isMediaLoaded(),
667
801
  )
668
802
  }
@@ -670,9 +804,10 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
670
804
  /**
671
805
  * Whether the engine is playing right now.
672
806
  *
673
- * Read from mpv's `playback-state` at the moment of use, never
674
- * from a JS flag: the PiP action's icon has to be true even when
675
- * the app is paused and no JS is running.
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.
676
811
  */
677
812
  private fun isPlaying(): Boolean {
678
813
  // ONE owner: `PlaybackState`, which reads mpv's REAL
@@ -760,10 +895,11 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
760
895
  * Whether mpv currently has media loaded.
761
896
  *
762
897
  * Read live through [PlaybackHost] — never from a JS flag, which
763
- * can be stale by the time the user presses HOME. The question
764
- * is mpv's own `playback-state`: `idle` and `stopped` are its
765
- * "no media" states, and everything else (`loading`, `play`,
766
- * `pause`, `eof-reached`) means something is still loaded.
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.
767
903
  */
768
904
  private fun isMediaLoaded(): Boolean {
769
905
  // ONE owner: `PlaybackState`, which reads mpv's REAL
@@ -854,15 +990,26 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
854
990
  * ## What the rect is, and what the platform does with it
855
991
  *
856
992
  * Android's `setSourceRectHint` wants the region of the window where
857
- * the video is. The platform CROPS the app's window to that rect and
858
- * scales the crop into the PiP window — which is what keeps the
859
- * app's chrome out of the PiP window, because the chrome around the
860
- * video is outside the crop.
993
+ * the video is, so the PiP window can ANIMATE FROM the picture
994
+ * instead of from a black rectangle.
995
+ *
996
+ * **It does NOT crop the app's window.** The 3.2.0 and 3.3.0
997
+ * releases of this library claimed it did, and that the crop is
998
+ * what keeps the app's chrome out of the PiP window. That claim is
999
+ * false and is retracted in 3.3.2: `setSourceRectHint` is a
1000
+ * TRANSITION hint — the region the PiP window animates FROM — and
1001
+ * nothing more. Measured on emulator-5554 (API 37) on 2026-11-04:
1002
+ * with the hint committed, the PiP window showed the app's FULL
1003
+ * chrome, scaled down.
1004
+ *
1005
+ * 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)`.
861
1008
  *
862
1009
  * It is **not** the picture view's box. The picture fills its lane
863
- * (`StyleSheet.absoluteFill`), so the view's rect is the whole window,
864
- * and hinting that would crop the whole window into the PiP window —
865
- * the app's chrome included, scaled down. The rect has to be the
1010
+ * (`StyleSheet.absoluteFill`), so the view's rect is the whole window
1011
+ * — and hinting that would animate the PiP window out of the whole
1012
+ * screen rather than out of the picture. The rect has to be the
866
1013
  * video's FRAME inside the view: the largest rect of the video's own
867
1014
  * shape that fits inside it, or the whole view when the picture is
868
1015
  * filling it.
@@ -1477,9 +1624,10 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
1477
1624
  ensurePtr()
1478
1625
  Log.i(TAG, "[PlaybackTrace][Bridge][stop] ptr=$nativePtr")
1479
1626
  MPVLib.nativeStop(nativePtr)
1480
- // A stopped session has no media, so the auto-enter switch has to
1481
- // be disarmed: the platform must not throw the user into a PiP
1482
- // window of a stopped session.
1627
+ // A stopped session has no media, so the entry is refused: the
1628
+ // library must not throw the user into a PiP window of a stopped
1629
+ // session. The platform's own auto-enter switch is not armed at
1630
+ // all, so there is no platform path to a PiP window either.
1483
1631
  onPlaybackStateChanged()
1484
1632
  }
1485
1633
 
@@ -3040,12 +3188,11 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
3040
3188
  pipAutoEnter = desired
3041
3189
  // The policy is remembered so `notifyUserLeaveHint(activity)`
3042
3190
  // knows whether to enter PiP at all. The platform's OWN
3043
- // auto-enter switch is NOT armed: the system would transition
3044
- // the activity before the chrome could be hidden, and React
3045
- // cannot commit a frame while the Activity is paused - so the
3046
- // chrome would be still mounted in the PiP window, dead and
3047
- // scaled down. See `PipManager.buildPipParams` for the
3048
- // measurement.
3191
+ // auto-enter switch is NOT armed: the platform transitions the
3192
+ // window through the Shell transition and the app's hooks are
3193
+ // never consulted, so the chrome would still be mounted when
3194
+ // the window was captured - dead and scaled down. See
3195
+ // `PipManager.buildPipParams` for the measurement.
3049
3196
  pipAutoEnterForCompanion = desired
3050
3197
  } catch (t: Throwable) {
3051
3198
  // Never let this reject the `setConfig` promise: the config
@@ -3135,7 +3282,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
3135
3282
  // while the Activity is paused.
3136
3283
  rememberPipActivity(activity)
3137
3284
  // The params are committed BEFORE the window is requested, so the
3138
- // platform has the source rect hint it crops the app's window to.
3285
+ // platform has the source rect hint it ANIMATES FROM.
3139
3286
  commitPipParams(activity)
3140
3287
  // The params are built and the window is requested by the
3141
3288
  // companion's ONE builder - the same one the native HOME path
@@ -209,8 +209,8 @@ class MpvRenderView(context: Context) : SurfaceView(context),
209
209
  return
210
210
  }
211
211
  // The rect moved, so the PiP params — which carry this rect as the
212
- // source rect hint the platform crops the app's window to — are
213
- // stale. The bridge re-commits them.
212
+ // source rect hint the PiP window ANIMATES FROM — are stale. The
213
+ // bridge re-commits them.
214
214
  MpvBridgeModule.onPictureBoundsChanged()
215
215
  }
216
216
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "3.3.1",
3
+ "version": "3.3.3",
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",
@@ -65,15 +65,19 @@ export interface PipConfig {
65
65
  * immediately, the effect that would have driven the entry ran 5.7 s late,
66
66
  * and the `requestAnimationFrame` never fired.
67
67
  *
68
- * On Android 12+ the library also arms Android's own auto-enter switch
69
- * (`PictureInPictureParams.Builder.setAutoEnterEnabled`), but ONLY while
70
- * media is loaded — and it commits the PiP params to the Activity early,
71
- * because the platform builds the PiP window from the LAST COMMITTED params
72
- * and crops the app's window to their source rect hint. That crop is what
73
- * keeps the app's chrome out of the PiP window.
68
+ * On Android 12+ the library does **NOT** arm Android's own auto-enter
69
+ * switch (`PictureInPictureParams.Builder.setAutoEnterEnabled`): the
70
+ * platform's auto-enter transitions the window through the Shell
71
+ * transition, the app's hooks are never consulted, and the app's chrome
72
+ * ends up in the PiP window. The app owns the entry instead — see the
73
+ * README's Picture-in-Picture section.
74
74
  *
75
- * See the README's Picture-in-Picture section for how the two mechanisms
76
- * fit together.
75
+ * The library commits the PiP params to the Activity early, because the
76
+ * platform builds the PiP window from the LAST COMMITTED params. The
77
+ * source rect hint among them is a TRANSITION hint — the region the PiP
78
+ * window ANIMATES FROM — not a crop. The app's chrome is kept out of the
79
+ * PiP window by hiding it before the entry, in native code, and by having
80
+ * that hide DRAWN before the platform takes the window.
77
81
  */
78
82
  autoEnterOnLeave: boolean;
79
83
  /**