@simba-dev/react-native-media-player 3.0.4 → 3.0.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,54 @@
1
1
 
2
+ ## [3.0.5] - 2026-11-03
3
+
4
+ ### Changed
5
+
6
+ - **The CONSUMER performs the PiP auto-enter; the platform's own switch is
7
+ gone.** `pip.autoEnterOnLeave` used to be honoured by arming
8
+ `PictureInPictureParams.Builder.setAutoEnterEnabled` (API 31+), which makes
9
+ the SYSTEM transition the activity into PiP when the user leaves the app,
10
+ with the consumer unable to do anything first. That is the wrong order for a
11
+ consumer that draws its own chrome over the picture, because **an Activity in
12
+ PiP does not redraw**: whatever its window held at the transition is what
13
+ the PiP window keeps showing. Measured on device on 2026-11-03, the
14
+ consumer's full expanded chrome was painted into the PiP window, its
15
+ buttons were dead, and its clock was frozen three minutes behind the real
16
+ position.
17
+
18
+ The library now says **WHEN** — through the new `onUserLeaveHint` event —
19
+ and the consumer removes its chrome, lets the frame that drew it go by, and
20
+ asks for the window itself. `pip.autoEnterOnLeave: true` keeps its public
21
+ meaning; only the performer changed. The consumer's side is two lines of
22
+ Kotlin (forward `onUserLeaveHint()` from the Activity) plus a subscription
23
+ to the `onUserLeaveHint` event in JS; both are in the README's
24
+ Picture-in-Picture section.
25
+
26
+ A side effect worth knowing: auto-enter now works on **every** API level.
27
+ The platform's switch never existed below Android 12; `onUserLeaveHint()`
28
+ does.
29
+
30
+ - **`onUserLeaveHint` is the 27th player event.** It is emitted with an empty
31
+ payload, and only when the auto-enter policy says yes — a consumer that
32
+ turned auto-enter off is never asked to enter PiP.
33
+
34
+ - **The bridge no longer stashes PiP params on the Activity.**
35
+ `Activity.setPictureInPictureParams` and the helper that pushed them
36
+ (`applyActivityPipParams`) are gone: they existed only to give the
37
+ platform's own auto-enter a shape to use, and that path no longer exists.
38
+ The params are now built at the moment the consumer asks for the window, so
39
+ the window's shape is the CURRENT video's shape.
40
+
41
+ ### Fixed
42
+
43
+ - **The PiP window could show the consumer's own chrome, frozen and dead.**
44
+ This is the library's half of that fix: it no longer arms the platform's
45
+ switch, so a consumer can remove its chrome before the window is asked for.
46
+ The consumer's half — hide the chrome, let one frame go by, then enter — is
47
+ the app's.
48
+
49
+ No public TypeScript API was removed. The config type, the command surface and
50
+ the event union are unchanged apart from the added event.
51
+
2
52
  ## [3.0.4] - 2026-11-03
3
53
 
4
54
  ### Changed
package/README.md CHANGED
@@ -139,6 +139,10 @@ Your launcher Activity has to opt in:
139
139
  `resizeableActivity="true"` is not optional. An Activity that is not resizeable cannot enter
140
140
  Picture-in-Picture.
141
141
 
142
+ If `config.pip.autoEnterOnLeave` is on, the Activity also has to forward the platform’s leave
143
+ hint to the library — see [Picture-in-Picture](#picture-in-picture) for the two lines of Kotlin
144
+ and the JS that goes with them.
145
+
142
146
  ### Background audio
143
147
 
144
148
  ```xml
@@ -68,7 +68,6 @@ object PipManager {
68
68
  sourceRectHint: Rect? = null,
69
69
  chapterTitle: String? = null,
70
70
  progressPercentage: String? = null,
71
- autoEnter: Boolean = false,
72
71
  ): PictureInPictureParams {
73
72
  val actions = mutableListOf<RemoteAction>()
74
73
 
@@ -133,17 +132,33 @@ object PipManager {
133
132
  builder.setSeamlessResizeEnabled(true)
134
133
  }
135
134
 
136
- // Auto-enter PiP when the user leaves the app (Android 12+).
135
+ // NO `setAutoEnterEnabled` HERE, and that is deliberate.
137
136
  //
138
- // This is the platform's OWN switch: with it set, the system
139
- // transitions the activity into PiP when the user swipes home,
140
- // instead of the app having to request it. Below API 31 the
141
- // platform has no equivalent - an app would have to override
142
- // `onUserLeaveHint()` in its own Activity, and this library ships
143
- // no Activity - so PiP entry stays manual there.
144
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
145
- builder.setAutoEnterEnabled(autoEnter)
146
- }
137
+ // The platform's auto-enter switch makes the SYSTEM transition the
138
+ // activity into PiP when the user leaves the app, without the app
139
+ // being able to do anything first. That is the wrong order for a
140
+ // consumer that draws its own chrome over the picture: an Activity
141
+ // in PiP does not redraw, so whatever the window held at the
142
+ // moment of the transition is what the PiP window keeps showing -
143
+ // frozen, dead and scaled down.
144
+ //
145
+ // Measured on device on 2026-11-03: the consumer's full expanded
146
+ // chrome (header band, scrub row, +-10 buttons, the big pause
147
+ // button) was painted into the PiP window, its buttons were DEAD
148
+ // (tapping pause did not pause) and its clock was FROZEN at
149
+ // 51:09 while the real position was 54:15 - while the consumer's
150
+ // own state already said `pipActive: true` and `isExpanded:
151
+ // false`.
152
+ //
153
+ // So auto-enter on leave is the CONSUMER's to perform, in the only
154
+ // order that works: remove the chrome, let the frame that drew it
155
+ // go by, and only then ask for the window. The library's part is
156
+ // to tell the consumer WHEN the user is leaving - the
157
+ // `onUserLeaveHint` event - and to keep the params it hands the
158
+ // platform free of the platform's own auto-enter switch.
159
+ //
160
+ // The consumer's Activity overrides `onUserLeaveHint()`, which
161
+ // works on every API level, unlike the platform switch (API 31+).
147
162
 
148
163
  // Source rect hint for smooth entry animation (Android 12+)
149
164
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && sourceRectHint != null) {
@@ -129,6 +129,20 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
129
129
  */
130
130
  private var pipAutoEnter: Boolean = false
131
131
 
132
+ /**
133
+ * The same policy, mirrored where the COMPANION can read it.
134
+ *
135
+ * `notifyUserLeaveHint()` is a companion function: it is called
136
+ * from the consumer's Activity, which has no reference to this
137
+ * module instance, so it cannot read the instance field above.
138
+ * The instance writes this mirror when it applies the policy.
139
+ *
140
+ * The same pattern `debugLoggingEnabled` uses.
141
+ */
142
+ @Volatile
143
+ var pipAutoEnterForCompanion: Boolean = false
144
+ private set
145
+
132
146
  /**
133
147
  * Phase 21: recursive JSONObject → Kotlin Map converter. Used
134
148
  * by [setConfig] so the stored config can be consumed
@@ -198,6 +212,51 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
198
212
  Log.w(TAG, "onPictureInPictureModeChanged: emit threw", e)
199
213
  }
200
214
  }
215
+
216
+ /**
217
+ * Called from the consumer's `Activity.onUserLeaveHint()` - the
218
+ * user is leaving the app (home, recents, app switcher).
219
+ *
220
+ * The consumer needs this because the PiP ENTRY has to be in the
221
+ * consumer's hands, not the platform's: an Activity in PiP does
222
+ * not redraw, so a consumer that draws its own chrome over the
223
+ * picture has to remove that chrome BEFORE the window is frozen.
224
+ * The platform's own auto-enter switch cannot give it that
225
+ * chance - see `PipManager.buildPipParams` for the measurement.
226
+ *
227
+ * So the library's job is to say WHEN, and the consumer decides
228
+ * whether to act: it removes its chrome, lets the frame that drew
229
+ * it go by, and then asks the OS for the window itself.
230
+ *
231
+ * The auto-enter policy is the consumer's own `pip.autoEnterOnLeave`
232
+ * config, so this is a no-op when the consumer does not want
233
+ * auto-enter.
234
+ */
235
+ fun notifyUserLeaveHint() {
236
+ Log.i(
237
+ TAG,
238
+ "companion.notifyUserLeaveHint: autoEnter=$pipAutoEnterForCompanion " +
239
+ "instance=${instance != null}",
240
+ )
241
+ if (!pipAutoEnterForCompanion) {
242
+ Log.i(
243
+ TAG,
244
+ "companion.notifyUserLeaveHint: auto-enter is off; not telling the consumer",
245
+ )
246
+ return
247
+ }
248
+ val ctx = instance ?: run {
249
+ Log.w(TAG, "notifyUserLeaveHint: module instance not initialized yet")
250
+ return
251
+ }
252
+ try {
253
+ ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
254
+ .emit("onUserLeaveHint", Arguments.createMap())
255
+ Log.i(TAG, "companion.notifyUserLeaveHint: emit succeeded")
256
+ } catch (e: Exception) {
257
+ Log.w(TAG, "notifyUserLeaveHint: emit threw", e)
258
+ }
259
+ }
201
260
  }
202
261
 
203
262
  init {
@@ -301,13 +360,13 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
301
360
  "surfaceAttached" -> "onSurfaceAttached"
302
361
  else -> event
303
362
  }
304
- // The video's dimensions just changed. The params the SYSTEM
305
- // reads for an auto-enter carry the aspect ratio, so they
306
- // have to be refreshed with the new one - otherwise the
307
- // auto-entered window has the shape of the previous video.
308
- if (event == "videoReconfig") {
309
- applyActivityPipParams()
310
- }
363
+ // The video's dimensions just changed. Nothing has to be
364
+ // refreshed here any more: the params the platform reads are
365
+ // the ones the CONSUMER's entry carries, and the consumer
366
+ // builds them at the moment it asks for the window - by then
367
+ // it knows the video's current shape. (The platform's own
368
+ // auto-enter switch, which used to read params stashed on the
369
+ // activity, is gone: see `PipManager.buildPipParams`.)
311
370
  try {
312
371
  val payload = JsonUtil.jsonStringToReactMap(jsonPayload)
313
372
  eventEmitter.emit(jsEvent, payload)
@@ -2294,20 +2353,30 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2294
2353
  }
2295
2354
 
2296
2355
  /**
2297
- * Apply `pip.autoEnterOnLeave` to the Activity.
2298
- *
2299
- * `Activity.setAutoEnterEnabled(true)` (API 31+ / Android 12) is the
2300
- * platform's own switch for "enter PiP when the user leaves the app".
2301
- *
2302
- * Below API 31 there is NO platform equivalent: an app has to
2303
- * override `onUserLeaveHint()` in its own Activity, and this library
2304
- * ships NO Activity - the consumer's launcher Activity hosts
2305
- * playback (the one-Activity architecture). So below Android 12 this
2306
- * is a documented no-op and PiP entry stays manual.
2307
- *
2308
- * The consumer's Activity must also declare
2309
- * `android:supportsPictureInPicture="true"` for the OS to honour it.
2310
- * That is the consumer's manifest, not this library's.
2356
+ * Apply `pip.autoEnterOnLeave` — remember the policy, and tell the
2357
+ * CONSUMER when the user leaves.
2358
+ *
2359
+ * What this does NOT do any more: arm the platform's own switch
2360
+ * (`PictureInPictureParams.Builder.setAutoEnterEnabled`, API 31+).
2361
+ * The platform's switch makes the SYSTEM transition the activity into
2362
+ * PiP without the consumer being able to do anything first, and an
2363
+ * Activity in PiP does not redraw — so a consumer that draws its own
2364
+ * chrome over the picture had that chrome frozen into the PiP
2365
+ * window, dead and scaled down. Measured on device on 2026-11-03.
2366
+ * See `PipManager.buildPipParams` for the full account.
2367
+ *
2368
+ * What it does now: the policy is remembered (so
2369
+ * `notifyUserLeaveHint()` can decide whether to speak at all), and
2370
+ * when the consumer's Activity reports that the user is leaving, the
2371
+ * consumer is told through the `onUserLeaveHint` event. The consumer
2372
+ * then removes its chrome, lets the frame that drew it go by, and
2373
+ * asks the OS for the window itself.
2374
+ *
2375
+ * The consumer's Activity must override `onUserLeaveHint()` and
2376
+ * declare `android:supportsPictureInPicture="true"`. Both are the
2377
+ * consumer's, not this library's — it ships no Activity (the
2378
+ * one-Activity architecture). `onUserLeaveHint()` works on EVERY API
2379
+ * level, unlike the platform switch, which needed API 31+.
2311
2380
  *
2312
2381
  * @param config the RESOLVED config map, as parsed by `setConfig`.
2313
2382
  */
@@ -2315,7 +2384,15 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2315
2384
  try {
2316
2385
  val desired = PipAutoEnterPolicy.desiredAutoEnter(config) ?: return
2317
2386
  pipAutoEnter = desired
2318
- applyActivityPipParams()
2387
+ // The policy is remembered so `notifyUserLeaveHint()` knows
2388
+ // whether to tell the consumer that it should enter PiP. The
2389
+ // platform's OWN auto-enter switch is NOT armed: the system
2390
+ // would transition the activity without the consumer being
2391
+ // able to remove its chrome first, and an Activity in PiP
2392
+ // does not redraw - so the consumer's chrome would be frozen
2393
+ // into the PiP window as a dead layer. See
2394
+ // `PipManager.buildPipParams` for the measurement.
2395
+ pipAutoEnterForCompanion = desired
2319
2396
  } catch (t: Throwable) {
2320
2397
  // Never let this reject the `setConfig` promise: the config
2321
2398
  // itself was stored fine, and a rejected promise would tell
@@ -2379,77 +2456,6 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2379
2456
  )
2380
2457
  }
2381
2458
 
2382
- /**
2383
- * Push the PiP params the SYSTEM will use onto the Activity.
2384
- *
2385
- * `Activity.setPictureInPictureParams` is what the platform reads
2386
- * when it transitions the activity into PiP BY ITSELF - which is
2387
- * exactly the swipe-home path that `pip.autoEnterOnLeave` asks for.
2388
- * The params `enterPip` passes to `enterPictureInPictureMode` only
2389
- * describe a MANUAL entry.
2390
- *
2391
- * So the auto-enter flag has to live on the activity's params, and
2392
- * it has to carry the video's CURRENT aspect ratio - a stale aspect
2393
- * means the auto-entered window has the shape of the previous video.
2394
- * That is why this runs again on every `videoReconfig`: that is the
2395
- * moment mpv reports new dimensions.
2396
- *
2397
- * `Activity.setAutoEnterEnabled` does NOT exist - the platform's
2398
- * switch is `PictureInPictureParams.Builder.setAutoEnterEnabled`,
2399
- * which is API 31+. Below that the OS cannot auto-enter at all.
2400
- */
2401
- private fun applyActivityPipParams() {
2402
- try {
2403
- if (!pipAutoEnter) return
2404
- val activity = reactApplicationContext.currentActivity
2405
- if (activity == null) {
2406
- Log.w(
2407
- TAG,
2408
- "[PlaybackTrace][Bridge][pip] no activity yet; auto-enter PiP not applied",
2409
- )
2410
- return
2411
- }
2412
- if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S) {
2413
- Log.w(
2414
- TAG,
2415
- "[PlaybackTrace][Bridge][pip] auto-enter PiP needs Android 12 (API 31)+; " +
2416
- "below that the OS cannot auto-enter, so PiP entry stays manual",
2417
- )
2418
- return
2419
- }
2420
- // The window's shape and the source rect hint are taken from the
2421
- // SAME frame, so they can never disagree. The system scales the
2422
- // hinted region to the window non-proportionally when the two
2423
- // shapes differ, and the video visibly arrives stretched.
2424
- //
2425
- // The frame's shape follows the picture's mode: with `contain`
2426
- // (the app's default) the frame inside the view IS the video,
2427
- // letterboxed into the view's own shape; with `cover` or
2428
- // `stretch` the picture fills the view, so the frame IS the
2429
- // view's rect and the window takes the view's shape. Either way
2430
- // the window's shape does not change here — only the hint does,
2431
- // from `null` (a black rectangle) to a real rect that points at
2432
- // the picture.
2433
- val frame = pipSourceRectInWindow()
2434
- val aspect = frame?.aspect ?: (currentVideoAspect() ?: (16f / 9f))
2435
- val params = com.simba.player.PipManager.buildPipParams(
2436
- context = activity,
2437
- aspect = aspect,
2438
- sourceRectHint = frame?.let { Rect(it.left, it.top, it.right, it.bottom) },
2439
- autoEnter = true,
2440
- )
2441
- activity.runOnUiThread {
2442
- activity.setPictureInPictureParams(params)
2443
- Log.i(
2444
- TAG,
2445
- "[PlaybackTrace][Bridge][pip] activity PiP params set, autoEnter=true",
2446
- )
2447
- }
2448
- } catch (t: Throwable) {
2449
- Log.w(TAG, "[PlaybackTrace][Bridge][pip] applying activity params failed", t)
2450
- }
2451
- }
2452
-
2453
2459
  // ── Picture-in-Picture ─────────────────────────────────────────────────
2454
2460
 
2455
2461
  /**
@@ -2554,7 +2560,6 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2554
2560
  sourceRectHint = sourceRect,
2555
2561
  chapterTitle = title,
2556
2562
  progressPercentage = pct,
2557
- autoEnter = pipAutoEnter,
2558
2563
  )
2559
2564
  // `enterPictureInPictureMode` ANSWERS whether the OS
2560
2565
  // accepted the request, and the answer was being
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "3.0.4",
3
+ "version": "3.0.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",
@@ -112,7 +112,16 @@ export interface LaunchParams {
112
112
  readonly requestId?: string;
113
113
  }
114
114
 
115
- /** All 26 events emitted by the Kotlin bridge. */
115
+ /**
116
+ * All 27 events emitted by the Kotlin bridge.
117
+ *
118
+ * `onUserLeaveHint` is the 27th: the native side telling the consumer
119
+ * that the user is leaving the app. It exists because a consumer that
120
+ * draws its own chrome over the picture has to be able to remove that
121
+ * chrome BEFORE the Activity enters Picture-in-Picture. See
122
+ * `PipManager`'s note on why the OS-initiated transition cannot be used
123
+ * for that.
124
+ */
116
125
  export type PlayerEventName =
117
126
  | 'onFileLoaded'
118
127
  | 'onLoadRequested'
@@ -139,7 +148,8 @@ export type PlayerEventName =
139
148
  | 'onPipModeChanged'
140
149
  | 'onPipPlayPause'
141
150
  | 'onPipExpand'
142
- | 'onPipClose';
151
+ | 'onPipClose'
152
+ | 'onUserLeaveHint';
143
153
 
144
154
  /** Typed payload map for each event. */
145
155
  export interface PlayerEventPayloads {
@@ -198,6 +208,17 @@ export interface PlayerEventPayloads {
198
208
  onPipPlayPause: Record<string, never>;
199
209
  onPipExpand: Record<string, never>;
200
210
  onPipClose: Record<string, never>;
211
+ /**
212
+ * The user is leaving the app (home, recents, app switcher).
213
+ *
214
+ * A consumer that owns its own chrome over the picture uses this to
215
+ * remove that chrome and THEN enter Picture-in-Picture itself. The
216
+ * order is not negotiable: an Activity in PiP does not redraw, so
217
+ * whatever its window contained at the moment of the transition is
218
+ * what the PiP window keeps showing — frozen, dead and scaled down.
219
+ * See `PipManager`'s note on the same thing.
220
+ */
221
+ onUserLeaveHint: Record<string, never>;
201
222
  }
202
223
 
203
224
  // ═══════════════════════════════════════════════════════════════════════════
@@ -1054,7 +1075,7 @@ export function removeAllListeners(event?: PlayerEventName): void {
1054
1075
  }
1055
1076
 
1056
1077
  /**
1057
- * All 26 player event names — the ONE owner of this list.
1078
+ * All 27 player event names — the ONE owner of this list.
1058
1079
  *
1059
1080
  * `<PlayerProvider>` imports it to subscribe to every event on mount,
1060
1081
  * `removeAllListeners()` iterates it when no event is named, and the
@@ -1088,4 +1109,5 @@ export const ALL_PLAYER_EVENTS: readonly PlayerEventName[] = [
1088
1109
  'onPipPlayPause',
1089
1110
  'onPipExpand',
1090
1111
  'onPipClose',
1112
+ 'onUserLeaveHint',
1091
1113
  ];
@@ -460,7 +460,7 @@ export interface PlayerProviderProps {
460
460
  * - **Initial hydration** (mount): synchronous bridge getters fill in
461
461
  * position / duration / playback state / volume / mute / speed /
462
462
  * loop / playlist / tracks / chapters / video params.
463
- * - **Event subscriptions** (mount): all 26 mpv events are subscribed
463
+ * - **Event subscriptions** (mount): all 27 mpv events are subscribed
464
464
  * via `subscribePlayerEvent` and dispatched to `applyPlayerEvent`.
465
465
  * - **Mount-time level seeds** (mount): `seekable` and `mute` are read once
466
466
  * via the sync `getProperty` getter, because mpv emits those two as
@@ -601,7 +601,7 @@ export function PlayerProvider({
601
601
  console.warn('[simba-player] hydratePlayerState failed:', e);
602
602
  }
603
603
 
604
- // ── 2. Subscribe to all 26 mpv events ───────────────────────────────
604
+ // ── 2. Subscribe to all 27 mpv events ───────────────────────────────
605
605
  // Each event handler reads the current state + progress from the
606
606
  // refs, applies the patch via the pure `applyPlayerEvent`
607
607
  // reducer, writes back to the refs, and schedules a re-render
@@ -51,7 +51,18 @@ export const DEFAULT_THEME: PlayerTheme = {
51
51
  export interface PipConfig {
52
52
  /** Master switch — when false, all PiP entry requests are ignored. */
53
53
  enabled: boolean;
54
- /** When true, pressing home / recents / app-switcher auto-enters PiP. */
54
+ /**
55
+ * When true, the library reports that the user is leaving the app, through
56
+ * the `onUserLeaveHint` event, and the CONSUMER enters PiP itself.
57
+ *
58
+ * The library deliberately does NOT arm Android's own auto-enter switch
59
+ * (`PictureInPictureParams.Builder.setAutoEnterEnabled`): that makes the OS
60
+ * transition the Activity without giving the app a chance to remove its own
61
+ * chrome, and an Activity in Picture-in-Picture does not redraw — so the
62
+ * chrome is frozen into the PiP window as a dead layer. See the README's
63
+ * Picture-in-Picture section for the two lines of Kotlin and the JS that
64
+ * go with it.
65
+ */
55
66
  autoEnterOnLeave: boolean;
56
67
  }
57
68
 
@@ -1479,6 +1479,7 @@ export function applyPlayerEvent(
1479
1479
  case 'onPipPlayPause':
1480
1480
  case 'onPipExpand':
1481
1481
  case 'onPipClose':
1482
+ case 'onUserLeaveHint':
1482
1483
  // No state-field mapping for these in the V13 surface (PiP state
1483
1484
  // is owned by the activity; reconfig signals a video-parameter
1484
1485
  // change that's already covered by onVideoParamsChanged).