@simba-dev/react-native-media-player 1.13.5 → 1.13.7
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/android/src/main/java/com/simba/player/PipManager.kt +9 -0
- package/android/src/main/java/com/simba/player/mpv/MpvBridgeModule.kt +58 -0
- package/android/src/main/java/com/simba/player/mpv/PlayerSurface.kt +41 -0
- package/package.json +1 -1
- package/src/bridge/MpvPlayerModule.ts +17 -0
- package/src/hooks/__tests__/useLaunchPlayback.test.tsx +35 -0
- package/src/hooks/useLaunchPlayback.ts +11 -1
|
@@ -123,6 +123,15 @@ object PipManager {
|
|
|
123
123
|
.setAspectRatio(aspectRational)
|
|
124
124
|
.setActions(actions)
|
|
125
125
|
|
|
126
|
+
// Seamless resize (Android 12+). Without it the PiP window animates
|
|
127
|
+
// between two discrete sizes on rotation / return, which reads as a
|
|
128
|
+
// stutter rather than a resize. This is a rendering-quality flag only:
|
|
129
|
+
// it changes how the transition is drawn, never whether PiP is
|
|
130
|
+
// entered, so it carries no behavioural risk.
|
|
131
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
132
|
+
builder.setSeamlessResizeEnabled(true)
|
|
133
|
+
}
|
|
134
|
+
|
|
126
135
|
// Source rect hint for smooth entry animation (Android 12+)
|
|
127
136
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && sourceRectHint != null) {
|
|
128
137
|
builder.setSourceRectHint(sourceRectHint)
|
|
@@ -2373,10 +2373,45 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2373
2373
|
// rather than blank.
|
|
2374
2374
|
val title = chapterTitle?.takeIf { it.isNotBlank() } ?: "Simba Player"
|
|
2375
2375
|
val pct = progressPct?.takeIf { it.isNotBlank() } ?: "0 %"
|
|
2376
|
+
// The PiP window's shape is the loaded video's shape. This used to
|
|
2377
|
+
// take the `16f / 9f` default from buildPipParams, so every PiP
|
|
2378
|
+
// window was 16:9 no matter what was playing -- a 4:3 archive or a
|
|
2379
|
+
// 2.35:1 scope film was letterboxed into the wrong window, and the
|
|
2380
|
+
// mismatch was baked in at entry so the system animated to a shape
|
|
2381
|
+
// that did not match the picture inside it.
|
|
2382
|
+
//
|
|
2383
|
+
// Read from the same `width` / `height` properties `getVideoParams`
|
|
2384
|
+
// already reads, rather than adding a second, parallel way to ask
|
|
2385
|
+
// mpv about dimensions.
|
|
2386
|
+
val aspect = currentVideoAspect()
|
|
2387
|
+
if (aspect == null) {
|
|
2388
|
+
Log.w(
|
|
2389
|
+
TAG,
|
|
2390
|
+
"[PlaybackTrace][Bridge][enterPip] no video dimensions loaded " +
|
|
2391
|
+
"(no file, or audio-only); PiP window will use 16:9",
|
|
2392
|
+
)
|
|
2393
|
+
}
|
|
2394
|
+
// Source rect hint: where the video actually is right now, in window
|
|
2395
|
+
// coordinates. This is what makes PiP entry animate the real frame into
|
|
2396
|
+
// the window instead of growing out of a black rectangle.
|
|
2397
|
+
//
|
|
2398
|
+
// The hint's shape has to match the aspect ratio above, or the system
|
|
2399
|
+
// scales the frame non-proportionally to fit and the video visibly
|
|
2400
|
+
// arrives stretched. Both come from the same place -- the surface is
|
|
2401
|
+
// sized with the video's own shape by `setVideoBounds` / `fillBounds`,
|
|
2402
|
+
// and `boundsInWindow()` reports that same surface -- so they agree by
|
|
2403
|
+
// construction instead of being kept in sync by hand.
|
|
2404
|
+
//
|
|
2405
|
+
// Null when the surface is hidden or zero-sized: no video rect to
|
|
2406
|
+
// animate from. That degrades to the no-hint behaviour, which costs a
|
|
2407
|
+
// black transition but never a distorted one.
|
|
2408
|
+
val sourceRect = PlayerSurface.boundsInWindow()
|
|
2376
2409
|
try {
|
|
2377
2410
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
|
2378
2411
|
val pipParams = com.simba.player.PipManager.buildPipParams(
|
|
2379
2412
|
context = activity,
|
|
2413
|
+
aspect = aspect ?: (16f / 9f),
|
|
2414
|
+
sourceRectHint = sourceRect,
|
|
2380
2415
|
chapterTitle = title,
|
|
2381
2416
|
progressPercentage = pct,
|
|
2382
2417
|
)
|
|
@@ -2400,6 +2435,29 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2400
2435
|
* Exit PiP mode by bringing the activity to the front.
|
|
2401
2436
|
* Called from JS when user taps "Expand" in PiP RemoteActions.
|
|
2402
2437
|
*/
|
|
2438
|
+
/**
|
|
2439
|
+
* Display aspect (width / height) of the loaded video, or `null` when it
|
|
2440
|
+
* is not known.
|
|
2441
|
+
*
|
|
2442
|
+
* Reads the same `width` / `height` properties as [getVideoParams]. Null
|
|
2443
|
+
* is the honest answer for "no file loaded" and for "audio only" -- both
|
|
2444
|
+
* are real, expected states rather than failures -- and callers decide
|
|
2445
|
+
* what a shape-less PiP window means for them.
|
|
2446
|
+
*/
|
|
2447
|
+
private fun currentVideoAspect(): Float? {
|
|
2448
|
+
val ptr = nativePtr
|
|
2449
|
+
if (ptr == 0L) return null
|
|
2450
|
+
return try {
|
|
2451
|
+
val w = MPVLib.nativeGetProperty(ptr, "width").trim().trim('"').toDoubleOrNull()
|
|
2452
|
+
val h = MPVLib.nativeGetProperty(ptr, "height").trim().trim('"').toDoubleOrNull()
|
|
2453
|
+
if (w != null && h != null && w > 0.0 && h > 0.0) (w / h).toFloat() else null
|
|
2454
|
+
} catch (e: Exception) {
|
|
2455
|
+
// The native lease guard refuses reads against a destroyed engine.
|
|
2456
|
+
Log.w(TAG, "[PlaybackTrace][Bridge][currentVideoAspect] ${e.message}")
|
|
2457
|
+
null
|
|
2458
|
+
}
|
|
2459
|
+
}
|
|
2460
|
+
|
|
2403
2461
|
@ReactMethod
|
|
2404
2462
|
@Override
|
|
2405
2463
|
override fun exitPip() {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
package com.simba.player.mpv
|
|
2
2
|
|
|
3
3
|
import android.app.Activity
|
|
4
|
+
import android.graphics.Rect
|
|
4
5
|
import android.os.Looper
|
|
5
6
|
import android.util.Log
|
|
6
7
|
import android.view.View
|
|
@@ -185,6 +186,46 @@ object PlayerSurface {
|
|
|
185
186
|
fun setVisible(visible: Boolean): Boolean =
|
|
186
187
|
withSurface("setVisible") { it.setSurfaceVisible(visible) }
|
|
187
188
|
|
|
189
|
+
/**
|
|
190
|
+
* The surface's current rect in window coordinates, or `null` when it has
|
|
191
|
+
* no on-screen area.
|
|
192
|
+
*
|
|
193
|
+
* This is the `setSourceRectHint` Android wants at PiP entry. With it the
|
|
194
|
+
* system animates the real video frame into the PiP window; without it the
|
|
195
|
+
* window grows out of a black rectangle.
|
|
196
|
+
*
|
|
197
|
+
* The hint must describe a rect of the *same shape* as the PiP aspect
|
|
198
|
+
* ratio, or the system scales the frame non-proportionally to fit and the
|
|
199
|
+
* video visibly arrives stretched or squashed. Because this rect is the
|
|
200
|
+
* surface itself and the surface is sized by [setBounds] / [fillBounds]
|
|
201
|
+
* with the video's own shape, the two agree by construction rather than by
|
|
202
|
+
* being kept in sync by hand.
|
|
203
|
+
*
|
|
204
|
+
* `null` is the honest answer while the surface is `GONE` or zero-sized --
|
|
205
|
+
* there is no video rect to animate from, and returning a zero rect would
|
|
206
|
+
* ask the system to animate from nothing. Null degrades to the no-hint
|
|
207
|
+
* behaviour, which costs a black transition but never a distorted one.
|
|
208
|
+
*/
|
|
209
|
+
@JvmStatic
|
|
210
|
+
fun boundsInWindow(): Rect? {
|
|
211
|
+
if (Looper.myLooper() != Looper.getMainLooper()) {
|
|
212
|
+
Log.e(TAG, "boundsInWindow refused: must be called from the main thread")
|
|
213
|
+
return null
|
|
214
|
+
}
|
|
215
|
+
val view = viewRef.get() ?: return null
|
|
216
|
+
if (view.visibility != View.VISIBLE || view.width <= 0 || view.height <= 0) {
|
|
217
|
+
return null
|
|
218
|
+
}
|
|
219
|
+
val location = IntArray(2)
|
|
220
|
+
view.getLocationInWindow(location)
|
|
221
|
+
return Rect(
|
|
222
|
+
location[0],
|
|
223
|
+
location[1],
|
|
224
|
+
location[0] + view.width,
|
|
225
|
+
location[1] + view.height,
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
|
|
188
229
|
/**
|
|
189
230
|
* Run [block] against the surface on the main thread.
|
|
190
231
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simba-dev/react-native-media-player",
|
|
3
|
-
"version": "1.13.
|
|
3
|
+
"version": "1.13.7",
|
|
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",
|
|
@@ -86,6 +86,23 @@ export interface LaunchParams {
|
|
|
86
86
|
readonly title: string;
|
|
87
87
|
readonly type: 'video' | 'audio';
|
|
88
88
|
readonly startPositionMs: number;
|
|
89
|
+
/**
|
|
90
|
+
* Monotonic identity of one launch request. Optional.
|
|
91
|
+
*
|
|
92
|
+
* `uri` + `startPositionMs` is not a unique identity for a launch, and
|
|
93
|
+
* `useLaunchPlayback` keys its load effect on exactly that pair. That was
|
|
94
|
+
* sound while every launch started a fresh `PlayerActivity` — a new React
|
|
95
|
+
* root, so the hook mounted fresh and always fired.
|
|
96
|
+
*
|
|
97
|
+
* It stops being sound the moment playback is loaded into an already-open
|
|
98
|
+
* window: the hook stays mounted across launches, so backing out of a film
|
|
99
|
+
* and tapping it again produces the same key and no second load happens.
|
|
100
|
+
* The user presses play and nothing occurs.
|
|
101
|
+
*
|
|
102
|
+
* A caller that loads in-window should therefore supply this. It matches
|
|
103
|
+
* `loadFileWithRequestId`, which already exists for the same reason.
|
|
104
|
+
*/
|
|
105
|
+
readonly requestId?: string;
|
|
89
106
|
}
|
|
90
107
|
|
|
91
108
|
/** All 22 events emitted by the Kotlin bridge. */
|
|
@@ -95,6 +95,41 @@ describe('useLaunchPlayback', () => {
|
|
|
95
95
|
);
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
+
// ── requestId: re-launching the SAME media must load again ───────────
|
|
99
|
+
//
|
|
100
|
+
// `uri` + `startPositionMs` is not a unique identity for a launch. It was
|
|
101
|
+
// sound only because every launch used to start a fresh `PlayerActivity` —
|
|
102
|
+
// a new React root, so this hook mounted fresh and always fired.
|
|
103
|
+
//
|
|
104
|
+
// The moment playback is loaded into an already-open window, this hook
|
|
105
|
+
// stays mounted across launches and that guarantee disappears. The
|
|
106
|
+
// everyday action that breaks is "back out of a film, tap it again": same
|
|
107
|
+
// URI, resume policy hands back the same position, same key, no load, and
|
|
108
|
+
// the user presses play and nothing happens.
|
|
109
|
+
|
|
110
|
+
it('loads again for the SAME uri and position when requestId differs', async () => {
|
|
111
|
+
const {rerender} = await render(
|
|
112
|
+
<Harness params={{...PARAMS, requestId: 'session-1'}} />,
|
|
113
|
+
);
|
|
114
|
+
await waitFor(() => expect(bridge().loadFile).toHaveBeenCalledTimes(1));
|
|
115
|
+
|
|
116
|
+
await rerender(<Harness params={{...PARAMS, requestId: 'session-2'}} />);
|
|
117
|
+
await waitFor(() => expect(bridge().loadFile).toHaveBeenCalledTimes(2));
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('does NOT re-fire when requestId is present but unchanged', async () => {
|
|
121
|
+
// The 1 Hz position poll still re-renders consumers constantly; a new
|
|
122
|
+
// object literal for the same launch must not reload.
|
|
123
|
+
const {rerender} = await render(
|
|
124
|
+
<Harness params={{...PARAMS, requestId: 'session-1'}} />,
|
|
125
|
+
);
|
|
126
|
+
await waitFor(() => expect(bridge().loadFile).toHaveBeenCalledTimes(1));
|
|
127
|
+
|
|
128
|
+
await rerender(<Harness params={{...PARAMS, requestId: 'session-1'}} />);
|
|
129
|
+
await rerender(<Harness params={{...PARAMS, requestId: 'session-1'}} />);
|
|
130
|
+
await waitFor(() => expect(bridge().loadFile).toHaveBeenCalledTimes(1));
|
|
131
|
+
});
|
|
132
|
+
|
|
98
133
|
it('converts startPositionMs from milliseconds to seconds', async () => {
|
|
99
134
|
render(<Harness params={{...PARAMS, startPositionMs: 90_000}} />);
|
|
100
135
|
await waitFor(() => expect(bridge().loadFile).toHaveBeenCalledTimes(1));
|
|
@@ -52,7 +52,17 @@ export function useLaunchPlayback(
|
|
|
52
52
|
const startPositionSec = launchParams?.startPositionMs
|
|
53
53
|
? launchParams.startPositionMs / 1000
|
|
54
54
|
: 0;
|
|
55
|
-
|
|
55
|
+
// A caller-supplied `requestId` ADDS launch identity to the key; it does
|
|
56
|
+
// not replace the uri|position part. Without it the key was `uri|position`,
|
|
57
|
+
// which is not unique across launches: that was only ever sound because
|
|
58
|
+
// each launch used to start a fresh Activity (fresh React root → fresh
|
|
59
|
+
// mount → always fires). A consumer that loads into an already-open window
|
|
60
|
+
// keeps this hook mounted, so re-launching the same file at the same
|
|
61
|
+
// position would produce the same key, skip the load, and the user would
|
|
62
|
+
// press play and see nothing.
|
|
63
|
+
const requestId = launchParams?.requestId;
|
|
64
|
+
const launchKey =
|
|
65
|
+
uri == null ? null : `${requestId ?? ''}|${uri}|${startPositionSec}`;
|
|
56
66
|
|
|
57
67
|
useEffect(() => {
|
|
58
68
|
if (launchKey == null || uri == null) {
|