@simba-dev/react-native-media-player 1.13.0 → 1.13.2

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.
@@ -1282,27 +1282,85 @@ class PlayerActivity : ReactActivity() {
1282
1282
  }
1283
1283
 
1284
1284
  /**
1285
- * Phase 10.7: back-button behaviour. V11's contract was: pressing
1286
- * back while in PiP finishes the activity (exits PiP, returns to
1287
- * the parent app) — back from fullscreen does default behaviour.
1288
- * V12 keeps the same contract.
1285
+ * Back-button behaviour.
1289
1286
  *
1290
- * NOTE: `onBackPressed` is deprecated in API 33+; the platform
1291
- * wants apps to use `OnBackInvokedCallback` (predictive back
1292
- * gesture). For Phase 10 we keep the V11 approach and guard by
1293
- * `isInPictureInPictureMode`; a Phase 35 hardening pass will move
1294
- * to the new API for API 33+ targets.
1287
+ * ## Why this does the teardown itself
1288
+ *
1289
+ * This used to be:
1290
+ *
1291
+ * ```kotlin
1292
+ * if (isInPictureInPictureMode) { finish(); return }
1293
+ * super.onBackPressed()
1294
+ * ```
1295
+ *
1296
+ * which finishes the Activity **natively**. The event never reaches
1297
+ * React Native, so a JS `BackHandler` cannot run — which left two back
1298
+ * paths that disagreed:
1299
+ *
1300
+ * - the header chevron -> JS `exitPlayer()` -> stop the engine,
1301
+ * release the foreground service, then finish;
1302
+ * - the hardware key -> `super.onBackPressed()` -> finish only.
1303
+ *
1304
+ * Since the mpv engine is a process-global C++ handle that outlives any
1305
+ * Activity, the second path closed the window while the media carried
1306
+ * on: audio still audible, notification still posted, no window left to
1307
+ * stop it. Verified on device — after the hardware key the media
1308
+ * session was still `PLAYING` with zero `stopPlayback` / `nativeStop`
1309
+ * lines in logcat, even with a JS BackHandler registered.
1310
+ *
1311
+ * ## Why natively rather than delegating to JS
1312
+ *
1313
+ * Delegating would make the player un-closable whenever the bundle is
1314
+ * unhealthy — a JS crash would strand a playing session with no window
1315
+ * and no working dismiss. The Activity already owns the engine through
1316
+ * `PlaybackHost`, so the teardown here is both shorter and strictly
1317
+ * more robust.
1318
+ *
1319
+ * The JS `BackHandler` in `VideoPlayer.tsx` is KEPT: it covers whatever
1320
+ * does reach React (notably predictive back, where `onBackPressed` is
1321
+ * never called), and the two paths now agree.
1295
1322
  */
1296
1323
  override fun onBackPressed() {
1297
1324
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && isInPictureInPictureMode) {
1298
1325
  // Finish exits PiP and returns the user to MainActivity (the
1299
- // parent of PlayerActivity in the task stack). This matches the
1300
- // V11 behaviour bit-for-bit.
1326
+ // parent of PlayerActivity in the task stack).
1301
1327
  Log.i(TAG, "onBackPressed: in PiP, finishing PlayerActivity")
1302
1328
  finish()
1303
1329
  return
1304
1330
  }
1305
- super.onBackPressed()
1331
+
1332
+ // Not PiP: leaving means CLOSING. Stop the engine unconditionally —
1333
+ // if the service is already gone its own stop callback is not around
1334
+ // to do it, and leaving media running with no way to stop it is the
1335
+ // worse of the two failures.
1336
+ val ptr = PlaybackHost.handle()
1337
+ if (ptr != 0L) {
1338
+ try {
1339
+ MPVLib.nativeStop(ptr)
1340
+ Log.i(TAG, "onBackPressed: stopped mpv (ptr=$ptr)")
1341
+ } catch (e: Throwable) {
1342
+ Log.w(TAG, "onBackPressed: nativeStop threw ${e.message}")
1343
+ }
1344
+ } else {
1345
+ Log.w(TAG, "onBackPressed: no engine handle; nothing to stop")
1346
+ }
1347
+
1348
+ if (MediaPlaybackService.isRunning()) {
1349
+ try {
1350
+ // Plain startService: the service is already foreground, so there
1351
+ // is no startForeground deadline to meet.
1352
+ startService(
1353
+ android.content.Intent(this, MediaPlaybackService::class.java).apply {
1354
+ action = MediaPlaybackService.ACTION_STOP
1355
+ },
1356
+ )
1357
+ Log.i(TAG, "onBackPressed: delivered ACTION_STOP to MediaPlaybackService")
1358
+ } catch (e: Throwable) {
1359
+ Log.e(TAG, "onBackPressed: could not deliver ACTION_STOP", e)
1360
+ }
1361
+ }
1362
+
1363
+ finish()
1306
1364
  }
1307
1365
 
1308
1366
  // ── PiP helpers (Phase 9) ──────────────────────────────────────────
@@ -2064,27 +2064,43 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2064
2064
  promise.resolve(true)
2065
2065
  }
2066
2066
 
2067
+ /**
2068
+ * The one teardown, for both lanes.
2069
+ *
2070
+ * Renamed from `stopAudioPlayback` because the body never was
2071
+ * audio-specific: it stops the process-global engine and sends the
2072
+ * service `ACTION_STOP`, neither of which knows or cares which lane
2073
+ * loaded the media. The old name became a lie the moment video
2074
+ * needed it too — and video does, because `exitPipAndFinish()` only
2075
+ * finishes the Activity. Closing the window left the engine running
2076
+ * with the notification still posted, so "back" looked like it closed
2077
+ * the player while the audio carried on with no window to stop it.
2078
+ *
2079
+ * Idempotent: safe with nothing loaded, and safe when the service is
2080
+ * already gone (the engine is still stopped in that case, because
2081
+ * leaving media running with no way to stop it is the worse failure).
2082
+ */
2067
2083
  @ReactMethod
2068
2084
  @Override
2069
- override fun stopAudioPlayback() {
2085
+ override fun stopPlayback() {
2070
2086
  val context = reactApplicationContext
2071
2087
  val serviceUp = com.simba.player.MediaPlaybackService.isRunning()
2072
2088
  // Stop the engine unconditionally. If the service is already
2073
2089
  // gone, the session's own stop callback is not around to do it,
2074
- // and leaving audio running with no way to stop it is the worse
2090
+ // and leaving media running with no way to stop it is the worse
2075
2091
  // failure of the two.
2076
2092
  if (nativePtr != 0L) {
2077
2093
  try {
2078
2094
  MPVLib.nativeStop(nativePtr)
2079
2095
  } catch (e: Throwable) {
2080
- Log.w(TAG, "[PlaybackTrace][Bridge][stopAudioPlayback] nativeStop threw ${e.message}")
2096
+ Log.w(TAG, "[PlaybackTrace][Bridge][stopPlayback] nativeStop threw ${e.message}")
2081
2097
  }
2082
2098
  } else {
2083
- Log.w(TAG, "[PlaybackTrace][Bridge][stopAudioPlayback] no engine handle; nothing to stop")
2099
+ Log.w(TAG, "[PlaybackTrace][Bridge][stopPlayback] no engine handle; nothing to stop")
2084
2100
  }
2085
2101
 
2086
2102
  if (!serviceUp) {
2087
- Log.i(TAG, "[PlaybackTrace][Bridge][stopAudioPlayback] service not running; engine stopped, nothing to tear down")
2103
+ Log.i(TAG, "[PlaybackTrace][Bridge][stopPlayback] service not running; engine stopped, nothing to tear down")
2088
2104
  return
2089
2105
  }
2090
2106
  try {
@@ -2096,7 +2112,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2096
2112
  },
2097
2113
  )
2098
2114
  } catch (e: Throwable) {
2099
- Log.e(TAG, "[PlaybackTrace][Bridge][stopAudioPlayback] could not deliver ACTION_STOP", e)
2115
+ Log.e(TAG, "[PlaybackTrace][Bridge][stopPlayback] could not deliver ACTION_STOP", e)
2100
2116
  }
2101
2117
  }
2102
2118
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "1.13.0",
3
+ "version": "1.13.2",
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",
@@ -191,7 +191,7 @@ export interface CaptureFrameOptions {
191
191
  * - Filters + playlist
192
192
  * - State queries
193
193
  * - Activity launch (openPlayer/getLaunchParams)
194
- * - Audio without an Activity (startAudioPlayback/stopAudioPlayback)
194
+ * - Audio without an Activity (startAudioPlayback/stopPlayback)
195
195
  * - Configuration (setConfig)
196
196
  * - PiP + screen
197
197
  * - Orientation + immersive
@@ -413,8 +413,8 @@ export interface MpvPlayerModuleBridge {
413
413
  startPositionMs?: number;
414
414
  autoPlay?: boolean;
415
415
  }): Promise<boolean>;
416
- /** Stop playback and tear the foreground service down. */
417
- stopAudioPlayback(): void;
416
+ /** Stop playback and tear the foreground service down. Both lanes. */
417
+ stopPlayback(): void;
418
418
  /** Whether the foreground service currently holds a live session. */
419
419
  isAudioPlaybackServiceRunning(): boolean;
420
420
  /**
@@ -686,7 +686,7 @@ const NOOP_BRIDGE: MpvPlayerModuleBridge = {
686
686
  void _options;
687
687
  return Promise.resolve(false);
688
688
  },
689
- stopAudioPlayback: () => {},
689
+ stopPlayback: () => {},
690
690
  isAudioPlaybackServiceRunning: () => false,
691
691
  // V22.0.0 / 1.5.8 (D-035 fix #6): now matches the real
692
692
  // Promise<LaunchParams | null> runtime return type.
@@ -231,14 +231,22 @@ export interface Spec extends TurboModule {
231
231
  }): Promise<boolean>;
232
232
 
233
233
  /**
234
- * Stop playback and tear the foreground service down. This is the mini
235
- * player's close (X) verb — distinct from *minimize*, which is a React
236
- * state change and never reaches the native layer.
234
+ * Stop playback and tear the foreground service down.
235
+ *
236
+ * This is *the* teardown, for both lanes. It is what "close" means:
237
+ * the engine stops and the foreground service is released, so the
238
+ * notification goes away and the media keys stop driving a session.
239
+ * It is NOT what "minimize" means — minimize is a React state change
240
+ * and never reaches the native layer.
241
+ *
242
+ * Video needs this too. `exitPipAndFinish()` only finishes the
243
+ * Activity; the engine is process-global, so finishing the window
244
+ * left the media running with no window left to stop it.
237
245
  *
238
246
  * Synchronous and idempotent: the service's own `ACTION_STOP` handler
239
247
  * already tolerates being called when nothing is loaded.
240
248
  */
241
- stopAudioPlayback(): void;
249
+ stopPlayback(): void;
242
250
 
243
251
  /**
244
252
  * Whether the foreground service currently holds a live session.
@@ -173,6 +173,16 @@ describe('usePlayer (no provider)', () => {
173
173
  expect(typeof c.playlistRemove).toBe('function');
174
174
  expect(typeof c.shuffle).toBe('function');
175
175
  expect(typeof c.clear).toBe('function');
176
+ // `stopPlayback` is the FULL teardown (engine + foreground service).
177
+ // It must exist alongside `stop`, which only halts the engine and
178
+ // leaves the session and notification alive. Video's back button
179
+ // needs the full one: `exitPipAndFinish()` finishes the Activity, and
180
+ // the process-global engine would otherwise keep playing behind a
181
+ // closed window.
182
+ expect(typeof c.stopPlayback).toBe('function');
183
+ // The old lane-specific name must NOT survive — a consumer reaching
184
+ // for it would silently get undefined and skip the teardown.
185
+ expect((c as unknown as Record<string, unknown>).stopAudioPlayback).toBeUndefined();
176
186
  expect(typeof c.cycleTrack).toBe('function');
177
187
  expect(typeof c.setTrack).toBe('function');
178
188
  expect(typeof c.enterPip).toBe('function');
@@ -162,6 +162,11 @@ export interface PlayerCommands {
162
162
  togglePlayPause(): void;
163
163
  /** Stop playback entirely. */
164
164
  stop(): void;
165
+ /**
166
+ * Stop playback AND tear the foreground service down (notification
167
+ * dismissed, media session released). This is what "close" means.
168
+ */
169
+ stopPlayback(): void;
165
170
  /** Relative seek in milliseconds. Negative seeks backward. */
166
171
  seekBy(deltaMs: number): void;
167
172
  /** Seek to the chapter at `index` (via mpv `setProperty('chapter', index)`). */
@@ -511,6 +516,16 @@ function buildCommands(): PlayerCommands {
511
516
  dlog('commands.stop()');
512
517
  getMpvPlayerModule().stop();
513
518
  },
519
+ /**
520
+ * The full teardown: engine + foreground service. Distinct from
521
+ * `stop()`, which only halts the engine and leaves the session and
522
+ * notification alive — correct for "pause the media, keep the
523
+ * transport available", wrong for "close the player".
524
+ */
525
+ stopPlayback: () => {
526
+ dlog('commands.stopPlayback()');
527
+ getMpvPlayerModule().stopPlayback();
528
+ },
514
529
  seekBy: (deltaMs: number) => {
515
530
  dlog('commands.seekBy(', deltaMs, ')');
516
531
  const deltaSec = Math.abs(deltaMs / 1000);