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

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,52 @@
1
1
 
2
+ ## [3.3.1] - 2026-11-04
3
+
4
+ ### Fixed
5
+
6
+ - **The Picture-in-Picture play/pause button did nothing.** The action reached the library's
7
+ receiver, the receiver logged "play/pause: played", and the picture kept moving: the button
8
+ looked alive and did nothing. Measured on emulator-5554 (API 37) on 2026-11-04.
9
+
10
+ The cause was that the receiver read mpv's `playback-state` — **a property mpv does not
11
+ have**. It is absent from mpv's own manuals (`DOCS/man/options.rst`, `DOCS/man/mpv.rst`) and
12
+ from mpv's source (`player/command.c`: `"playback-state"` appears ZERO times, while
13
+ `"idle-active"`, `"eof-reached"`, `"pause"` and `"playback-time"` all appear). The read
14
+ failed, the native helper returned the JSON literal `null`, and "null" is neither "play" nor
15
+ "idle" — so every answer was wrong in the same direction: the receiver always believed the
16
+ video was stopped and called PLAY on an already-playing video.
17
+
18
+ The same broken read was in three other places — the PiP action's icon, the library's own
19
+ media check, and the consumer app's media check — so "media is loaded" was true for every
20
+ value. All four now go through **one** owner, `PlaybackState`, which reads mpv's real
21
+ properties and derives the same vocabulary the library has always published:
22
+ `idle` / `stopped` / `paused` / `playing`.
23
+
24
+ - **The Picture-in-Picture window showed the app's full chrome on the HOME path.** The platform
25
+ transitions the window itself when the auto-enter switch is armed, and it does that through
26
+ the Shell transition: the app's hooks are never consulted. `Activity.onUserLeaveHint()` did
27
+ **not fire at all** on a HOME press (measured on emulator-5554, API 37, 2026-11-04 — zero log
28
+ lines) and the PiP window showed the app's full expanded chrome, scaled down.
29
+
30
+ The entry now runs from `Activity.onPictureInPictureRequested()` — AOSP's own PiP entry hook —
31
+ which is called while the Activity is still **RESUMED**. That matters twice over:
32
+ `Activity.enterPictureInPictureMode()` throws `IllegalStateException` unless the Activity is
33
+ resumed, and the app is still drawing, so the chrome-less window is presented before the
34
+ platform transitions it. The chrome is now hidden **in the entry, before the window is asked
35
+ for** — hiding it afterwards, from `onPictureInPictureModeChanged(true)`, was too late.
36
+
37
+ The platform's auto-enter switch is **not armed**. With it armed there is no pre-transition
38
+ hook to use, and ATMS refuses an explicit entry while it is enabled ("auto-enter should take
39
+ in place instead").
40
+
41
+ ### Retracted
42
+
43
+ - **"The platform crops your window to the rect in `setSourceRectHint(...)`."** This was
44
+ published in the 3.2.0 and 3.3.0 READMEs and CHANGELOGs, and it is **false**.
45
+ `setSourceRectHint` is a **transition hint**: it tells the platform which region of your window
46
+ the PiP window should animate FROM. It does not crop anything. Measured on emulator-5554 (API
47
+ 37) on 2026-11-04: with the hint committed, the PiP window showed the app's **full** chrome,
48
+ scaled down. The chrome is kept out by hiding it before the entry, not by a crop.
49
+
2
50
  ## [3.3.0] - 2026-11-04
3
51
 
4
52
  ### Fixed
@@ -16,6 +64,12 @@
16
64
  from the window's last rendered buffer, and that buffer still held the chrome. The chrome was
17
65
  hidden and stayed visible.
18
66
 
67
+ **3.3.0's own claim is retracted by 3.3.1.** The crop mechanism described below is NOT a
68
+ mechanism: `setSourceRectHint` is a transition hint, not a crop, and the device disproved it.
69
+ 3.3.1 replaces this whole mechanism with hiding the chrome before the entry, from
70
+ `Activity.onPictureInPictureRequested()`. The text below is kept as the historical record of
71
+ what 3.3.0 believed, and it is WRONG.
72
+
19
73
  Two mechanisms keep the app's chrome out of the PiP window now, and they are layered:
20
74
 
21
75
  1. **The crop (primary).** The platform crops the app's window to the rect in
package/README.md CHANGED
@@ -369,6 +369,23 @@ override fun onUserLeaveHint() {
369
369
  }
370
370
  ```
371
371
 
372
+ On Android 12+ (API 31+) there is a **better** hook, and it is the one that matters:
373
+
374
+ ```kotlin
375
+ override fun onPictureInPictureRequested(): Boolean {
376
+ com.simba.player.mpv.MpvBridgeModule.notifyUserLeaveHint(this)
377
+ return true
378
+ }
379
+ ```
380
+
381
+ Return `true`. AOSP's contract for that return value is "true if the activity received this
382
+ callback regardless of if it acts on it or not", and `false` sends the framework down the legacy
383
+ `onUserLeaveHint()` path instead.
384
+
385
+ **Keep both.** `onPictureInPictureRequested()` is API 31+; `onUserLeaveHint()` is the API 24-30
386
+ path. The same library call serves both, so there is one entry and one owner. Why the API 31+
387
+ hook matters is in [Why it is native](#why-it-is-native-and-why-your-activity-hands-over-the-activity).
388
+
372
389
  The library then asks the OS for the window, hides your chrome, and brings your chrome back when
373
390
  Picture-in-Picture ends. It returns whether the OS accepted the entry, so you can log it:
374
391
 
@@ -378,36 +395,53 @@ val entered = com.simba.player.mpv.MpvBridgeModule.notifyUserLeaveHint(this)
378
395
 
379
396
  ### Why it is native, and why your Activity hands over the Activity
380
397
 
381
- `Activity.onUserLeaveHint()` is the only hook Android gives you BEFORE the Activity is paused.
398
+ Two reasons, and they are different.
382
399
 
383
- And by then, React Native has ALREADY frozen, in two ways: `FabricUIManager.onHostPause()`
384
- removes the DISPATCH_UI frame callback, so mount items are never applied to native views, and
400
+ **React Native has frozen by then.** `FabricUIManager.onHostPause()` removes the DISPATCH_UI
401
+ frame callback, so mount items are never applied to native views, and
385
402
  `JavaTimerManager.onHostPause()` freezes the timer registry, so `requestAnimationFrame` never
386
403
  fires. The JS runtime itself keeps running. Measured on emulator-5554 (API 37) on 2026-11-03: the
387
404
  JS handler ran immediately, the effect that would have driven the entry ran **5.7 s late**, and
388
405
  the `requestAnimationFrame` **never fired**. So the entry has to happen in native code.
389
406
 
390
- While the Activity is paused, the app window's **UI layer** stops producing frames, while a
391
- `SurfaceView` keeps compositing. That is why the chrome is hidden from
392
- `onPictureInPictureModeChanged(true)` and not from `onUserLeaveHint()`: `onUserLeaveHint()` is
393
- called as part of the Activity's PAUSE transaction, and there is **no frame between the hide and
394
- the pause** — a hide applied there never reaches the pixels. (The 3.2.0 release claimed otherwise;
395
- the 3.3.0 release retracted it.)
407
+ **And on API 31+ the platform's own hooks are the wrong ones.** `Activity.onUserLeaveHint()`
408
+ used to be described as "the only hook Android gives you BEFORE the Activity is paused", and on
409
+ API 31+ that is **false**. With the platform's auto-enter switch armed, the platform
410
+ transitions your window itself, through the Shell transition, and your Activity is not
411
+ consulted: `onUserLeaveHint()` did **not fire at all** on a HOME press. Measured on
412
+ emulator-5554 (API 37) on 2026-11-04 — zero log lines, while the PiP window opened with the
413
+ app's full chrome in it.
414
+
415
+ `Activity.onPictureInPictureRequested()` is AOSP's own PiP entry hook, and it is called while
416
+ your Activity is still **RESUMED**. That matters twice over:
417
+
418
+ - `Activity.enterPictureInPictureMode()` throws `IllegalStateException` unless the Activity is
419
+ 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
+
423
+ That is why the library does **not** arm the platform's auto-enter switch: with it armed there
424
+ is no pre-transition hook to use.
396
425
 
397
426
  ### How your chrome is kept out of the PiP window
398
427
 
399
- Two mechanisms, layered:
428
+ The PiP window **is** your Activity's own window, so whatever your chrome draws is what the PiP
429
+ window shows. The library hides your chrome root **before** it asks the OS for the window.
430
+
431
+ Two things have to be right for that to work:
432
+
433
+ 1. **The hide happens in the entry, not after it.** Hiding from
434
+ `onPictureInPictureModeChanged(true)` — after the platform has already transitioned the
435
+ window — is too late. Measured on emulator-5554 (API 37) on 2026-11-04: the PiP window showed
436
+ the app's FULL expanded chrome, scaled down.
437
+ 2. **The entry happens while your Activity is resumed.** On API 31+ that means
438
+ `onPictureInPictureRequested()`; see
439
+ [Why it is native](#why-it-is-native-and-why-your-activity-hands-over-the-activity).
400
440
 
401
- 1. **The crop (primary).** The platform crops your window to the rect in
402
- `PictureInPictureParams.setSourceRectHint(...)` and scales that crop into the PiP window. The
403
- library **commits** the params to your Activity with
404
- `Activity.setPictureInPictureParams(...)` — early, and again whenever the picture's geometry,
405
- the video's shape or the playback state changes — because the platform builds the PiP window
406
- from the LAST COMMITTED params. A hint handed over only at the entry moment never crops
407
- anything, and the PiP window then shows your whole window, scaled down.
408
- 2. **The hide (second line of defence).** The chrome you draw *over* the video is inside the
409
- crop, so the crop cannot remove it. The library hides your chrome root when Picture-in-
410
- Picture starts.
441
+ `PictureInPictureParams.setSourceRectHint(...)` is **not** part of this. It is a TRANSITION
442
+ hint: it tells the platform which region of your window the PiP window should animate FROM. It
443
+ does **not** crop your window. (The 3.2.0 and 3.3.0 releases said otherwise; 3.3.1 retracts it,
444
+ with the device measurement that disproved it.)
411
445
 
412
446
  ### Telling the library which view is your chrome
413
447
 
@@ -563,14 +597,17 @@ before launching, and check `commands.isSurfaceMounted()`.
563
597
  **Picture-in-Picture does not start.** The Activity is missing
564
598
  `android:supportsPictureInPicture="true"` or `android:resizeableActivity="true"`. If auto-enter
565
599
  on leave does nothing, the Activity is not calling `MpvBridgeModule.notifyUserLeaveHint(this)`
566
- from `onUserLeaveHint()` - see [Picture-in-Picture](#picture-in-picture).
567
-
568
- **The Picture-in-Picture window shows your app's chrome.** The platform crops your window to the
569
- source rect hint in the COMMITTED PiP params, and the library commits them when the picture's
570
- geometry, the video's shape or the playback state changes. If the crop is not happening, the
571
- params are not being committed - the log says so (`companion.commitPipParams`). A `nativeID` in
572
- your config that no view carries is NOT a refusal: the chrome is already hidden, and the library
573
- says so in the log.
600
+ from `onPictureInPictureRequested()` (API 31+) or from `onUserLeaveHint()` (API 24-30) - see
601
+ [Picture-in-Picture](#picture-in-picture).
602
+
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
605
+ 31+ that means `onPictureInPictureRequested()` must return `true` and call the library:
606
+ `onUserLeaveHint()` alone is **never called** there, because the platform's auto-enter switch
607
+ transitions the window itself (measured: zero log lines on a HOME press). The library logs the
608
+ hide (`companion.hideConsumerChrome`) and the entry (`companion.notifyUserLeaveHint`). A
609
+ `nativeID` in your config that no view carries is NOT a refusal: the chrome is already hidden, and
610
+ the library says so in the log.
574
611
 
575
612
  **The Picture-in-Picture action row shows three buttons and none of them work.** That was the
576
613
  explicit-broadcast bug, fixed in 3.1.0: the action's PendingIntent named the receiver's
@@ -88,10 +88,13 @@ object PipManager {
88
88
  * @param aspect Video aspect ratio as a float (e.g., 16f / 9f for 16:9).
89
89
  * Clamped to the Android PiP-allowed range [0.42, 2.38]
90
90
  * before being encoded as an integer Rational.
91
- * @param sourceRectHint The region of the window the VIDEO occupies. The
92
- * platform crops the app's window to this rect, so
93
- * it is what keeps the app's chrome out of the PiP
94
- * window.
91
+ * @param sourceRectHint The region of the window the VIDEO occupies. It is
92
+ * a TRANSITION hint: the platform animates the PiP
93
+ * window FROM that region. It does NOT crop the
94
+ * app's window — measured on emulator-5554 (API 37)
95
+ * on 2026-11-04, with the hint committed the PiP
96
+ * window showed the app's FULL chrome, scaled
97
+ * down.
95
98
  * @param chapterTitle Current chapter title for notification text (Android 12+).
96
99
  * @param progressPercentage Percentage string like "45%" for notification text (Android 12+).
97
100
  * @param playing Whether the video is playing right now — it picks
@@ -163,23 +166,32 @@ object PipManager {
163
166
  builder.setSeamlessResizeEnabled(true)
164
167
  }
165
168
 
166
- // Auto-enter (Android 12+).
169
+ // Auto-enter (Android 12+). DELIBERATELY NOT ARMED.
167
170
  //
168
171
  // The platform's auto-enter switch makes the SYSTEM transition the
169
- // activity into PiP when the user leaves the app, and it does that
170
- // from the LAST COMMITTED params — which is why the library commits
171
- // them early and keeps them current.
172
+ // activity into PiP when the user leaves the app. It does that
173
+ // through the Shell transition, and the app's own hooks are never
174
+ // consulted: `Activity.onUserLeaveHint()` did not fire at all on a
175
+ // HOME press (measured on emulator-5554, API 37, 2026-11-04 — zero
176
+ // log lines), and ATMS refuses an explicit entry while the switch
177
+ // is armed ("auto-enter should take in place instead").
172
178
  //
173
- // It is armed ONLY while media is loaded (`autoEnter`). Arming it
174
- // with nothing playing would throw the user into a PiP window of a
175
- // stopped session the moment they browsed the Home screen.
179
+ // So with the switch armed the library had NO pre-transition hook
180
+ // at all: the platform froze the window with the consumer's chrome
181
+ // still mounted, and the PiP window showed the whole chrome scaled
182
+ // down.
176
183
  //
177
- // While it is armed, ATMS REFUSES an explicit
178
- // `enterPictureInPictureMode()` — "auto-enter should take in place
179
- // instead" — so the library's own entry path is a logged no-op on
180
- // API 31+ and the explicit entry is kept for API 24-30.
184
+ // AOSP's own hook for this case is
185
+ // `Activity.onPictureInPictureRequested()` (API 31+): the system
186
+ // asks the app, and the app enters PiP itself. Returning `true`
187
+ // from it also stops the framework from falling back to the legacy
188
+ // `onUserLeaveHint()` path.
189
+ //
190
+ // `autoEnter` therefore stays a CONSUMER POLICY, checked by the
191
+ // library before it enters, and never a platform switch.
181
192
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && autoEnter) {
182
- builder.setAutoEnterEnabled(true)
193
+ // Intentionally NOT `setAutoEnterEnabled(true)`.
194
+ builder.setAutoEnterEnabled(false)
183
195
  }
184
196
 
185
197
  // Source rect hint (Android 8.0+, API 26)
@@ -190,10 +202,10 @@ object PipManager {
190
202
  // API 26, so gating it on API 31 threw away the entry animation on
191
203
  // every device below Android 12 for no reason.
192
204
  //
193
- // This is also the rect the platform CROPS the app's window to, so
194
- // it is what keeps the app's chrome out of the PiP window. That is
195
- // why the library commits the params early instead of handing them
196
- // over only at the entry moment.
205
+ // This is the rect the PiP window ANIMATES FROM — a transition
206
+ // hint, nothing more. It does not crop the app's window, so it is
207
+ // 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.
197
209
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && sourceRectHint != null) {
198
210
  builder.setSourceRectHint(sourceRectHint)
199
211
  }
@@ -373,14 +385,17 @@ class PipActionReceiver : BroadcastReceiver() {
373
385
  android.util.Log.w(TAG, "cannot toggle playback: no engine handle")
374
386
  return
375
387
  }
376
- val state = try {
377
- com.simba.player.mpv.MPVLib.nativeGetProperty(ptr, "playback-state")
378
- } catch (t: Throwable) {
379
- android.util.Log.w(TAG, "cannot read playback-state", t)
380
- null
381
- }
388
+ // The state is read through the ONE owner, from mpv's REAL
389
+ // properties (`idle-active`, `eof-reached`, `pause`).
390
+ //
391
+ // It used to read `playback-state` — a property mpv does NOT
392
+ // have. The read failed, the answer was "not playing", and the
393
+ // button called PLAY on an already-playing video: a button that
394
+ // looked alive and did nothing. Measured on emulator-5554
395
+ // (API 37) on 2026-11-04.
396
+ val state = com.simba.player.mpv.PlaybackState.live()
382
397
  try {
383
- if (PipNativeEntry.isPlaying(state)) {
398
+ if (com.simba.player.mpv.PlaybackState.isPlaying(state)) {
384
399
  com.simba.player.mpv.MPVLib.nativePause(ptr)
385
400
  android.util.Log.i(TAG, "play/pause: paused")
386
401
  } else {
@@ -87,22 +87,4 @@ internal object PipNativeEntry {
87
87
  return "${kotlin.math.floor(ratio + 0.5).toInt()} %"
88
88
  }
89
89
 
90
- /**
91
- * Whether mpv is PLAYING right now, from its `playback-state`.
92
- *
93
- * mpv's `playback-state` is one of `idle`, `loading`, `play`, `pause`,
94
- * `stopped`, `eof-reached`. Only `play` means the picture is moving.
95
- *
96
- * `eof-reached` is NOT playing: the file ended, and the engine is
97
- * sitting on its last frame. A play/pause button that shows "play"
98
- * for an ended file is right, so the state has to be read, not
99
- * guessed.
100
- *
101
- * The property arrives JSON-encoded, so it is trimmed of its quotes
102
- * before it is compared.
103
- */
104
- fun isPlaying(playbackState: String?): Boolean {
105
- val state = playbackState?.trim()?.trim('"')?.trim() ?: return false
106
- return state.equals("play", ignoreCase = true)
107
- }
108
90
  }
@@ -424,7 +424,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
424
424
  Log.i(
425
425
  TAG,
426
426
  "companion.hideConsumerChrome: hid the chrome view with nativeID " +
427
- "'$chromeNativeId'",
427
+ "'$chromeNativeId' visibility=" + chromeView.visibility,
428
428
  )
429
429
  }
430
430
 
@@ -448,11 +448,34 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
448
448
  * `requestAnimationFrame` never fired — so the chrome was still
449
449
  * mounted when the OS took the window.
450
450
  *
451
- * So the ENTRY happens HERE, in native code. The chrome is NOT
452
- * hidden here: there is no frame between this hook and the pause,
453
- * so a hide applied here never reaches the pixels. The chrome is
454
- * hidden from `onPictureInPictureModeChanged(true)`, after the
455
- * platform has entered PiP.
451
+ * So the ENTRY happens HERE, in native code.
452
+ *
453
+ * ## The hook this runs from changed in v3.3.1
454
+ *
455
+ * On API 31+ this function is called from
456
+ * `Activity.onPictureInPictureRequested()` — AOSP's own PiP
457
+ * entry hook — and NOT from `onUserLeaveHint()`.
458
+ *
459
+ * The reason is measured: with the platform's auto-enter switch
460
+ * armed, the platform transitions the window itself, through the
461
+ * Shell transition, and the app's hooks are never consulted. On a
462
+ * HOME press on emulator-5554 (API 37) on 2026-11-04,
463
+ * `onUserLeaveHint()` did not fire at all — zero log lines — and
464
+ * the PiP window showed the app's full chrome, scaled down.
465
+ *
466
+ * `Activity.onPictureInPictureRequested()` is called while the
467
+ * Activity is still RESUMED, which is the only moment
468
+ * `enterPictureInPictureMode()` is allowed to run at all
469
+ * (`Activity.enterPictureInPictureMode` throws
470
+ * `IllegalStateException` unless the Activity is resumed).
471
+ *
472
+ * ## The chrome is hidden BEFORE the window is asked for
473
+ *
474
+ * The PiP window IS the Activity's own window, so whatever the
475
+ * chrome draws is what the PiP window shows. The hide therefore
476
+ * happens here, before the entry — not in
477
+ * `onPictureInPictureModeChanged(true)`, which is AFTER the
478
+ * platform has already frozen the window.
456
479
  *
457
480
  * ## What it refuses, and why
458
481
  *
@@ -467,10 +490,8 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
467
490
  *
468
491
  * @param activity the Activity that is leaving. It is the window
469
492
  * the PiP entry applies to.
470
- * @return `true` when the Activity entered Picture-in-Picture (or,
471
- * on API 31+, when the platform's own auto-enter switch is
472
- * armed and will enter it as soon as this returns), `false`
473
- * when it did not.
493
+ * @return `true` when the Activity entered Picture-in-Picture,
494
+ * `false` when it did not.
474
495
  */
475
496
  fun notifyUserLeaveHint(activity: Activity): Boolean {
476
497
  Log.i(
@@ -503,45 +524,42 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
503
524
  Log.i(TAG, "companion.notifyUserLeaveHint: no media loaded; not entering PiP")
504
525
  return false
505
526
  }
506
- // 4. The chrome, when the consumer has one. The literal is
507
- // the consumer's — the library never hardcodes one. The
508
- // lookup here is a DIAGNOSTIC: the chrome is hidden from
509
- // `onPictureInPictureModeChanged(true)`, because a hide
510
- // applied now would never reach the pixels.
511
- val chromeNativeId = pipChromeNativeIdForCompanion
512
- if (chromeNativeId != null) {
513
- val chromeView = findConsumerChromeView(activity.window?.decorView)
514
- Log.i(
515
- TAG,
516
- "companion.notifyUserLeaveHint: the chrome view with nativeID " +
517
- "'$chromeNativeId' is " +
518
- if (chromeView == null) "not mounted (already hidden)" else "mounted",
519
- )
520
- }
527
+ // 4. Hide the consumer's chrome NOW, before the window is
528
+ // asked for.
529
+ //
530
+ // The PiP window IS the Activity's own window, so whatever
531
+ // the chrome draws is what the PiP window shows. It has to
532
+ // be gone BEFORE the platform transitions the window.
533
+ //
534
+ // On API 31+ this runs from
535
+ // `Activity.onPictureInPictureRequested()`, where the
536
+ // Activity is still RESUMED, so the app draws the
537
+ // chrome-less window before the transition. On API 24-30
538
+ // it runs from `onUserLeaveHint()`, right before the
539
+ // Activity's pause — that path has not been measured on a
540
+ // device.
541
+ //
542
+ // It used to happen in `onPictureInPictureModeChanged(true)`
543
+ // — AFTER the transition — which is too late: the platform
544
+ // had already frozen the window. Measured on emulator-5554
545
+ // (API 37) on 2026-11-04: the PiP window showed the app's
546
+ // FULL chrome, scaled down.
547
+ hideConsumerChrome()
521
548
  rememberPipActivity(activity)
522
549
  // 5. Commit the params BEFORE the window is asked for.
523
550
  //
524
551
  // The platform builds the PiP window from the LAST
525
- // COMMITTED params, and the source rect hint in them is
526
- // the rect it CROPS the app's window to. Params handed
527
- // over only at the entry moment never crop anything, so
528
- // the PiP window shows the whole window — the app's chrome
529
- // included — scaled down.
552
+ // COMMITTED params. The source rect hint among them is the
553
+ // region the PiP window ANIMATES FROM — a transition hint,
554
+ // not a crop.
530
555
  commitPipParams(activity)
531
- // 6. On API 31+ the PLATFORM enters PiP by itself, from the
532
- // auto-enter switch in the committed params. ATMS refuses
533
- // an explicit `enterPictureInPictureMode()` while
534
- // auto-enter is enabled ("auto-enter should take in place
535
- // instead"), so asking for the window here would be
536
- // refused — and a refusal read as "PiP failed".
537
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
538
- Log.i(
539
- TAG,
540
- "companion.notifyUserLeaveHint: auto-enter is armed; the platform enters PiP",
541
- )
542
- return true
543
- }
544
- // 7. API 24-30: the explicit entry is the only way in.
556
+ // 6. Ask for the window.
557
+ //
558
+ // This is an EXPLICIT entry on every API level. On API 31+
559
+ // it is legal here because the platform's auto-enter
560
+ // switch is not armed: ATMS refuses a client entry only
561
+ // while auto-enter is enabled ("auto-enter should take in
562
+ // place instead").
545
563
  val entered = enterPictureInPicture(activity)
546
564
  if (!entered) {
547
565
  Log.w(TAG, "companion.notifyUserLeaveHint: the OS refused the PiP entry")
@@ -557,13 +575,13 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
557
575
  * ## Why the params are committed EARLY
558
576
  *
559
577
  * The platform builds the PiP window from the LAST COMMITTED
560
- * params — `Activity.setPictureInPictureParams(...)` — and the
561
- * source rect hint among them is the rect it CROPS the app's
562
- * window to. A hint handed over only at the entry moment never
563
- * crops anything: the PiP window then shows the whole window,
564
- * scaled down, with the app's chrome in it. Measured on
565
- * emulator-5554 (API 37) on 2026-11-03: the PiP window showed the
566
- * app's full expanded chrome, scaled down, with dead buttons.
578
+ * params — `Activity.setPictureInPictureParams(...)` — so the
579
+ * params have to be current when the entry happens.
580
+ *
581
+ * The source rect hint among them is the region the PiP window
582
+ * ANIMATES FROM. It is NOT a crop: measured on emulator-5554
583
+ * (API 37) on 2026-11-04, with the hint committed, the PiP window
584
+ * showed the app's FULL chrome, scaled down.
567
585
  *
568
586
  * Google's own `androidx.core-pip` does exactly this — it tracks
569
587
  * the player view's bounds and commits the params as they change.
@@ -625,9 +643,8 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
625
643
  chapterTitle: String? = null,
626
644
  progressPct: String? = null,
627
645
  ): android.app.PictureInPictureParams {
628
- // The rect of the window the VIDEO occupies. The platform crops
629
- // the app's window to it, so it is what keeps the app's chrome
630
- // out of the PiP window.
646
+ // The rect of the window the VIDEO occupies. The PiP window
647
+ // animates FROM it — a transition hint, not a crop.
631
648
  val frame = pipSourceRectInWindow()
632
649
  // The PiP window's shape is the loaded video's shape, and it is
633
650
  // taken from the SAME frame as the hint, so the two agree by
@@ -658,14 +675,11 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
658
675
  * the app is paused and no JS is running.
659
676
  */
660
677
  private fun isPlaying(): Boolean {
661
- val ptr = PlaybackHost.handle()
662
- if (ptr == 0L) return false
663
- return try {
664
- PipNativeEntry.isPlaying(MPVLib.nativeGetProperty(ptr, "playback-state"))
665
- } catch (e: Exception) {
666
- Log.w(TAG, "companion.isPlaying: reading playback-state failed", e)
667
- false
668
- }
678
+ // ONE owner: `PlaybackState`, which reads mpv's REAL
679
+ // properties. This used to read `playback-state`, a property
680
+ // mpv does NOT have, so the answer was always "not playing"
681
+ // and the PiP action's icon never changed.
682
+ return PlaybackState.isPlayingNow()
669
683
  }
670
684
 
671
685
  /**
@@ -752,18 +766,12 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
752
766
  * `pause`, `eof-reached`) means something is still loaded.
753
767
  */
754
768
  private fun isMediaLoaded(): Boolean {
755
- val ptr = PlaybackHost.handle()
756
- if (ptr == 0L) return false
757
- return try {
758
- val state = MPVLib.nativeGetProperty(ptr, "playback-state").trim().trim('"')
759
- state.isNotEmpty() && state != "idle" && state != "stopped"
760
- } catch (e: Exception) {
761
- // The native lease guard refuses calls against a
762
- // destroyed engine; that is a normal outcome, not a
763
- // crash.
764
- Log.w(TAG, "companion.notifyUserLeaveHint: reading playback-state failed", e)
765
- false
766
- }
769
+ // ONE owner: `PlaybackState`, which reads mpv's REAL
770
+ // properties. This used to read `playback-state`, a property
771
+ // mpv does NOT have — the read failed, and "not idle, not
772
+ // stopped" was therefore true for EVERY value, so the library
773
+ // thought media was always loaded.
774
+ return PlaybackState.hasMediaNow()
767
775
  }
768
776
 
769
777
  /**
@@ -2344,25 +2352,13 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2344
2352
  Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=0 state=idle")
2345
2353
  return "idle"
2346
2354
  }
2355
+ // ONE owner: `PlaybackState`. The derivation below used to be
2356
+ // written out HERE, while the PiP paths read a DIFFERENT (and
2357
+ // non-existent) property. Two owners for one fact is how the PiP
2358
+ // paths came to answer "not playing" for a video that was playing.
2347
2359
  return try {
2348
- val idle = MPVLib.nativeGetProperty(nativePtr, "idle-active")
2349
- .trim('"').toBoolean()
2350
- if (idle) {
2351
- Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=$nativePtr state=idle idle=true")
2352
- return "idle"
2353
- }
2354
-
2355
- val ended = MPVLib.nativeGetProperty(nativePtr, "eof-reached")
2356
- .trim('"').toBoolean()
2357
- if (ended) {
2358
- Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=$nativePtr state=stopped eof=true")
2359
- return "stopped"
2360
- }
2361
-
2362
- val paused = MPVLib.nativeGetProperty(nativePtr, "pause")
2363
- .trim('"').toBoolean()
2364
- val state = if (paused) "paused" else "playing"
2365
- Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=$nativePtr state=$state idle=$idle eof=$ended pause=$paused")
2360
+ val state = PlaybackState.live()
2361
+ Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=$nativePtr state=$state")
2366
2362
  state
2367
2363
  } catch (e: Exception) {
2368
2364
  Log.e(TAG, "[PlaybackTrace][Bridge][getPlaybackState] failed: ${e.message}", e)
@@ -0,0 +1,174 @@
1
+ package com.simba.player.mpv
2
+
3
+ import com.simba.player.PlaybackHost
4
+
5
+ /**
6
+ * The ONE owner of "what is the engine doing right now".
7
+ *
8
+ * ## Why this file exists
9
+ *
10
+ * Until v3.3.1 this library had TWO owners of the same fact:
11
+ *
12
+ * - `MpvBridgeModule.getPlaybackState()` read mpv's REAL properties —
13
+ * `idle-active`, `eof-reached`, `pause` — and derived one of
14
+ * `idle` / `stopped` / `paused` / `playing`.
15
+ *
16
+ * - the PiP paths read a property named **`playback-state`**, which is
17
+ * NOT an mpv property. It is absent from mpv's own option and
18
+ * property manuals (`DOCS/man/options.rst`, `DOCS/man/mpv.rst`) and
19
+ * from mpv's source (`player/command.c`: `"playback-state"` appears
20
+ * ZERO times, while `"idle-active"`, `"eof-reached"`, `"pause"` and
21
+ * `"playback-time"` all appear).
22
+ *
23
+ * The consequence was measured on emulator-5554 (API 37) on
24
+ * 2026-11-04: `mpv_get_property` on a property that does not exist
25
+ * fails, the bridge's native helper returns the JSON literal `null`,
26
+ * and every caller of that read answered "not playing" — forever.
27
+ *
28
+ * So the PiP play/pause button called `play` on an already-playing
29
+ * video and logged "play/pause: played" while the picture kept moving.
30
+ * The button looked alive and did nothing.
31
+ *
32
+ * Two owners for one fact is two chances to get it wrong, and this one
33
+ * was wrong in a way no test could see: the string it compared against
34
+ * was a plausible-looking vocabulary that mpv never emits.
35
+ *
36
+ * ## The vocabulary
37
+ *
38
+ * Four words, and they are the ones this library has always published
39
+ * to JS through `getPlaybackState()`:
40
+ *
41
+ * - `idle` — no media, the engine has nothing loaded.
42
+ * - `stopped` — the file reached its end.
43
+ * - `paused` — the file is loaded and the clock is not running.
44
+ * - `playing` — the picture is moving.
45
+ *
46
+ * ## What is deliberately NOT here
47
+ *
48
+ * The derivation is PURE Kotlin — no Android, no mpv — so it can be
49
+ * exercised by a plain JUnit test with no device, no Robolectric and no
50
+ * network download. The live read is [live], and it is the only
51
+ * function here that touches the engine.
52
+ */
53
+ object PlaybackState {
54
+
55
+ // ── The vocabulary ────────────────────────────────────────────────────
56
+
57
+ const val IDLE = "idle"
58
+ const val STOPPED = "stopped"
59
+ const val PAUSED = "paused"
60
+ const val PLAYING = "playing"
61
+
62
+ // ── mpv's REAL property names ─────────────────────────────────────────
63
+
64
+ /**
65
+ * mpv's real properties. These three are the ones that answer the
66
+ * question; `playback-state` is NOT among them and never was.
67
+ */
68
+ const val PROP_IDLE_ACTIVE = "idle-active"
69
+ const val PROP_EOF_REACHED = "eof-reached"
70
+ const val PROP_PAUSE = "pause"
71
+
72
+ // ── The pure derivation ───────────────────────────────────────────────
73
+
74
+ /**
75
+ * The state, derived from mpv's three real properties.
76
+ *
77
+ * The order matters and it is the order the platform's own session
78
+ * uses: a file that ended is `stopped` even though the engine is
79
+ * still loaded, and an engine with nothing loaded is `idle` whatever
80
+ * the pause flag says.
81
+ */
82
+ fun derive(idleActive: Boolean, eofReached: Boolean, paused: Boolean): String {
83
+ return when {
84
+ idleActive -> IDLE
85
+ eofReached -> STOPPED
86
+ paused -> PAUSED
87
+ else -> PLAYING
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Whether [state] means the picture is moving.
93
+ *
94
+ * `stopped` is NOT playing: the file ended and the engine is sitting
95
+ * on its last frame. A play/pause button that shows "play" for an
96
+ * ended file is right, so the state has to be read, not guessed.
97
+ */
98
+ fun isPlaying(state: String?): Boolean {
99
+ return normalise(state).equals(PLAYING, ignoreCase = true)
100
+ }
101
+
102
+ /**
103
+ * Whether [state] means something is still loaded.
104
+ *
105
+ * `idle` and `stopped` are the two "no media" answers; `paused` and
106
+ * `playing` both mean a file is loaded.
107
+ */
108
+ fun hasMedia(state: String?): Boolean {
109
+ val value = normalise(state)
110
+ if (value.isEmpty()) return false
111
+ return !value.equals(IDLE, ignoreCase = true) &&
112
+ !value.equals(STOPPED, ignoreCase = true)
113
+ }
114
+
115
+ /**
116
+ * A boolean property, read from the JSON the native helper returns.
117
+ *
118
+ * mpv's boolean properties arrive as `true` / `false`; a property
119
+ * that is not set arrives as the JSON literal `null`, which is NOT
120
+ * a boolean and must not be read as `false` by accident — but for
121
+ * these three properties "not set" and "false" mean the same thing,
122
+ * so `null` is false here.
123
+ */
124
+ fun parseBoolean(raw: String?): Boolean {
125
+ return normalise(raw).equals("true", ignoreCase = true)
126
+ }
127
+
128
+ /**
129
+ * A property value with its JSON quoting and surrounding space
130
+ * removed, and the JSON literal `null` turned into an empty string.
131
+ */
132
+ private fun normalise(raw: String?): String {
133
+ val value = raw?.trim()?.trim('"')?.trim() ?: return ""
134
+ if (value.equals("null", ignoreCase = true)) return ""
135
+ return value
136
+ }
137
+
138
+ // ── The live read ─────────────────────────────────────────────────────
139
+
140
+ /**
141
+ * The engine's state RIGHT NOW, read through [PlaybackHost].
142
+ *
143
+ * The pointer is the engine's own handle — the same one every other
144
+ * engine call in this library uses — and it is read at the moment of
145
+ * use, never from a cached flag. A JS flag can be stale by the time
146
+ * the user presses HOME, and the PiP action's icon has to be true
147
+ * even while the app is paused and no JS is running.
148
+ */
149
+ fun live(): String {
150
+ val ptr = PlaybackHost.handle()
151
+ if (ptr == 0L) return IDLE
152
+ return try {
153
+ derive(
154
+ idleActive = parseBoolean(MPVLib.nativeGetProperty(ptr, PROP_IDLE_ACTIVE)),
155
+ eofReached = parseBoolean(MPVLib.nativeGetProperty(ptr, PROP_EOF_REACHED)),
156
+ paused = parseBoolean(MPVLib.nativeGetProperty(ptr, PROP_PAUSE)),
157
+ )
158
+ } catch (e: Throwable) {
159
+ // The native lease guard refuses calls against a destroyed
160
+ // engine; that is a normal outcome, not a crash.
161
+ IDLE
162
+ }
163
+ }
164
+
165
+ /**
166
+ * Whether the engine is playing right now.
167
+ */
168
+ fun isPlayingNow(): Boolean = isPlaying(live())
169
+
170
+ /**
171
+ * Whether the engine still has a file loaded.
172
+ */
173
+ fun hasMediaNow(): Boolean = hasMedia(live())
174
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "3.3.0",
3
+ "version": "3.3.1",
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",