@simba-dev/react-native-media-player 1.1.0 → 1.3.0
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/README.md +132 -39
- package/package.json +2 -2
- package/src/components/PlayerProvider.tsx +288 -7
- package/src/hooks/SimbaPlayer.tsx +144 -0
- package/src/hooks/SimbaPlayerRoot.tsx +73 -0
- package/src/hooks/useLaunchParams.tsx +64 -0
- package/src/hooks/useOpenFromUrl.tsx +174 -0
- package/src/hooks/useOpenWithResume.tsx +236 -0
- package/src/hooks/usePlayerActivity.ts +103 -0
- package/src/hooks/useSimbaPlayerLookup.tsx +58 -0
- package/src/index.ts +26 -0
- package/src/types/__tests__/player.test.tsx +494 -0
- package/src/types/player.ts +947 -72
- package/src/types/__tests__/player.test.ts +0 -166
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import React, {useMemo} from 'react';
|
|
2
|
+
import { PlayerProvider } from '../components/PlayerProvider';
|
|
3
|
+
import type { PlayerConfig } from '../types/config';
|
|
4
|
+
import { PlayerResumeProvider } from './useOpenWithResume';
|
|
5
|
+
import type { PlayerResumeLookup } from './useOpenWithResume';
|
|
6
|
+
import type { GetResumePosition } from './useSimbaPlayerLookup';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* V13+ DX: the single-entry-point wrapper for the SIMBA player
|
|
10
|
+
* module. This is the **only** component a consumer's app root
|
|
11
|
+
* needs to integrate the module. It composes the three layers a
|
|
12
|
+
* typical app needs:
|
|
13
|
+
*
|
|
14
|
+
* 1. `<PlayerProvider>` — the config + state context (Phase 51).
|
|
15
|
+
* 2. `<PlayerResumeProvider>` — the bookmark-aware resume
|
|
16
|
+
* lookup used by `useOpenWithResume` (Phase 51/52 + DX).
|
|
17
|
+
*
|
|
18
|
+
* Without `<SimbaPlayer>`, the consumer would have to nest both
|
|
19
|
+
* providers manually, which is verbose and error-prone for junior
|
|
20
|
+
* developers. With `<SimbaPlayer>`, the entire integration is
|
|
21
|
+
* **one import + one wrapper component**:
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```tsx
|
|
25
|
+
* // App.tsx (the consumer's root)
|
|
26
|
+
* import { SimbaPlayer } from '@simba-dev/react-native-media-player';
|
|
27
|
+
*
|
|
28
|
+
* export default function App() {
|
|
29
|
+
* return (
|
|
30
|
+
* <SimbaPlayer
|
|
31
|
+
* config={{ theme: { accent: '#FFD700' } }}
|
|
32
|
+
* getResumePosition={(uri) =>
|
|
33
|
+
* store.getState().bookmarks.byFileUri[uri]?.positionMs
|
|
34
|
+
* }
|
|
35
|
+
* >
|
|
36
|
+
* <RootNavigator />
|
|
37
|
+
* </SimbaPlayer>
|
|
38
|
+
* );
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* **V14 Phase 61:** the wrapper now accepts EITHER a
|
|
43
|
+
* `getResumePosition` function reference (recommended for new
|
|
44
|
+
* consumers) OR a `lookup` object (backward-compat for V13-era
|
|
45
|
+
* consumers). If both are passed, `getResumePosition` wins.
|
|
46
|
+
* The function-prop shape is one function reference instead of
|
|
47
|
+
* an object literal — slightly less typing, slightly less
|
|
48
|
+
* nesting.
|
|
49
|
+
*
|
|
50
|
+
* The `lookup` / `getResumePosition` props are optional — pass
|
|
51
|
+
* one to enable `useOpenWithResume`'s auto-resume behavior. If
|
|
52
|
+
* you only need `usePlayerActivity().openPlayer(...)` (no
|
|
53
|
+
* resume lookup), you can omit both and the inner
|
|
54
|
+
* `<PlayerResumeProvider>` is still mounted with a no-op lookup
|
|
55
|
+
* that always returns `undefined`.
|
|
56
|
+
*
|
|
57
|
+
* **Junior-dev rule of thumb:** if your screen calls
|
|
58
|
+
* `openPlayer({resumeId: item.id})`, you need a `lookup` or
|
|
59
|
+
* `getResumePosition` here. If you only call
|
|
60
|
+
* `openPlayer({uri, title, type})` without `resumeId`, the
|
|
61
|
+
* lookup is optional.
|
|
62
|
+
*/
|
|
63
|
+
export interface SimbaPlayerProps {
|
|
64
|
+
/**
|
|
65
|
+
* Partial PlayerConfig. Every field is optional; missing
|
|
66
|
+
* fields fall back to the defaults in `resolvePlayerConfig`.
|
|
67
|
+
*/
|
|
68
|
+
config?: PlayerConfig;
|
|
69
|
+
/**
|
|
70
|
+
* Bookmark-aware resume lookup (object shape). If provided,
|
|
71
|
+
* the module's `useOpenWithResume` hook will resolve
|
|
72
|
+
* `resumeId` arguments to a `startPositionMs` via this
|
|
73
|
+
* object's `getResumePosition` method.
|
|
74
|
+
*
|
|
75
|
+
* Backward-compat: V13-era consumers that pass a memoized
|
|
76
|
+
* `PlayerResumeLookup` object. New consumers should prefer
|
|
77
|
+
* the `getResumePosition` function-prop shape.
|
|
78
|
+
*
|
|
79
|
+
* If both `lookup` and `getResumePosition` are passed,
|
|
80
|
+
* `getResumePosition` wins.
|
|
81
|
+
*/
|
|
82
|
+
lookup?: PlayerResumeLookup;
|
|
83
|
+
/**
|
|
84
|
+
* Bookmark-aware resume lookup (function shape). Receives a
|
|
85
|
+
* `resumeId` (typically a URI or item id) and returns the
|
|
86
|
+
* saved position in ms, or `undefined` for no saved position.
|
|
87
|
+
*
|
|
88
|
+
* **Recommended for new consumers.** One function reference
|
|
89
|
+
* instead of an object literal:
|
|
90
|
+
*
|
|
91
|
+
* ```tsx
|
|
92
|
+
* <SimbaPlayer getResumePosition={(uri) => store.getState().bookmarks.byFileUri[uri]?.positionMs}>
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* If both `lookup` and `getResumePosition` are passed,
|
|
96
|
+
* `getResumePosition` wins.
|
|
97
|
+
*/
|
|
98
|
+
getResumePosition?: GetResumePosition;
|
|
99
|
+
children: React.ReactNode;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The one-line integration point. See `SimbaPlayerProps` for
|
|
104
|
+
* usage.
|
|
105
|
+
*/
|
|
106
|
+
export function SimbaPlayer({
|
|
107
|
+
config,
|
|
108
|
+
lookup,
|
|
109
|
+
getResumePosition,
|
|
110
|
+
children,
|
|
111
|
+
}: SimbaPlayerProps): React.ReactElement {
|
|
112
|
+
// `getResumePosition` wins over `lookup` if both are passed.
|
|
113
|
+
// Memoize the resulting lookup so the `PlayerResumeContext`
|
|
114
|
+
// value stays stable across renders — this is what keeps
|
|
115
|
+
// every `useOpenWithResume()` consumer from re-rendering on
|
|
116
|
+
// every root render.
|
|
117
|
+
const effectiveLookup = useMemo<PlayerResumeLookup>(() => {
|
|
118
|
+
if (getResumePosition) {
|
|
119
|
+
return {getResumePosition};
|
|
120
|
+
}
|
|
121
|
+
if (lookup) {
|
|
122
|
+
return lookup;
|
|
123
|
+
}
|
|
124
|
+
return noopLookup;
|
|
125
|
+
}, [getResumePosition, lookup]);
|
|
126
|
+
|
|
127
|
+
return (
|
|
128
|
+
<PlayerProvider config={config}>
|
|
129
|
+
<PlayerResumeProvider lookup={effectiveLookup}>
|
|
130
|
+
{children}
|
|
131
|
+
</PlayerResumeProvider>
|
|
132
|
+
</PlayerProvider>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* No-op lookup used when the consumer doesn't pass a `lookup`
|
|
138
|
+
* or `getResumePosition` prop. Always returns `undefined` so
|
|
139
|
+
* `useOpenWithResume` falls back to 0 (or the
|
|
140
|
+
* consumer-provided `startPositionMs`).
|
|
141
|
+
*/
|
|
142
|
+
const noopLookup: PlayerResumeLookup = {
|
|
143
|
+
getResumePosition: () => undefined,
|
|
144
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { PlayerRoot } from '../components/PlayerRoot';
|
|
3
|
+
import { useLaunchParams } from './useLaunchParams';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* V14 Phase 59: the activity-branch wrapper.
|
|
7
|
+
*
|
|
8
|
+
* The native `PlayerActivity` is launched with playback params
|
|
9
|
+
* (`{uri, title, type, startPositionMs}`) by any `openPlayer(...)` call.
|
|
10
|
+
* The activity's React tree should render the player surface +
|
|
11
|
+
* controls when those params are present, and the regular app
|
|
12
|
+
* navigator when they're not.
|
|
13
|
+
*
|
|
14
|
+
* Before V14, this branching lived in the consumer's `App.tsx`:
|
|
15
|
+
*
|
|
16
|
+
* ```tsx
|
|
17
|
+
* const launchParams = useLaunchParams();
|
|
18
|
+
* if (launchParams) {
|
|
19
|
+
* return <PlayerRoot />;
|
|
20
|
+
* }
|
|
21
|
+
* return <YourNavigator />;
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* `<SimbaPlayerRoot>` absorbs both the hook call and the switch.
|
|
25
|
+
* Consumers just nest their navigator as children:
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```tsx
|
|
29
|
+
* <SimbaPlayer getResumePosition={...}>
|
|
30
|
+
* <SimbaPlayerRoot>
|
|
31
|
+
* <YourNavigator />
|
|
32
|
+
* </SimbaPlayerRoot>
|
|
33
|
+
* </SimbaPlayer>
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* The activity branch is opaque: the consumer cannot customize what
|
|
37
|
+
* is rendered when `launchParams` is non-null. The module owns the
|
|
38
|
+
* player surface + controls. If a consumer needs a different
|
|
39
|
+
* activity surface (rare), they should use `PlayerRoot` directly
|
|
40
|
+
* with `useLaunchParams()`.
|
|
41
|
+
*
|
|
42
|
+
* **Why this exists:** V14's junior-dev-level integration goal is
|
|
43
|
+
* that any consumer's `App.tsx` looks the same. The launch-params
|
|
44
|
+
* branch is the second-largest source of glue after the resume
|
|
45
|
+
* lookup and the deep-link handler — Phase 59 deletes it from the
|
|
46
|
+
* consumer's `App.tsx`.
|
|
47
|
+
*/
|
|
48
|
+
export interface SimbaPlayerRootProps {
|
|
49
|
+
/**
|
|
50
|
+
* The regular app content (typically a navigator). Rendered when
|
|
51
|
+
* the activity was launched WITHOUT playback params — i.e., the
|
|
52
|
+
* user just opened the app from the launcher or the home screen
|
|
53
|
+
* icon, with no recent `openPlayer(...)` handoff queued.
|
|
54
|
+
*/
|
|
55
|
+
children: React.ReactNode;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The activity-branch wrapper. Mounts `<PlayerRoot />` when the
|
|
60
|
+
* activity was launched with playback params, otherwise renders
|
|
61
|
+
* `children`. See `SimbaPlayerRootProps` for usage.
|
|
62
|
+
*/
|
|
63
|
+
export function SimbaPlayerRoot({
|
|
64
|
+
children,
|
|
65
|
+
}: SimbaPlayerRootProps): React.ReactElement {
|
|
66
|
+
const launchParams = useLaunchParams();
|
|
67
|
+
|
|
68
|
+
if (launchParams) {
|
|
69
|
+
return <PlayerRoot />;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return <>{children}</>;
|
|
73
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { getMpvPlayerModule, type LaunchParams } from '../bridge/MpvPlayerModule';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* V13 Phase 54: read the activity's launch params (one-shot).
|
|
6
|
+
*
|
|
7
|
+
* The native `PlayerActivity` is launched with a payload
|
|
8
|
+
* `{uri, title, type, startPositionMs}` from the most recent
|
|
9
|
+
* `openPlayer(...)` call (see `LaunchParams` in
|
|
10
|
+
* `MpvPlayerModule.ts`). The Kotlin side keeps the params as a
|
|
11
|
+
* single-shot queue — the first reader consumes them, subsequent
|
|
12
|
+
* calls return `null`.
|
|
13
|
+
*
|
|
14
|
+
* In the JS layer, this hook is what `PlayerActivity`'s React
|
|
15
|
+
* tree calls to fetch the params (typically in the activity's
|
|
16
|
+
* top-level component, e.g. `AppContent`). After reading, the
|
|
17
|
+
* hook exposes the params as React state so the consumer can:
|
|
18
|
+
*
|
|
19
|
+
* 1. Conditionally render `<PlayerRoot />` (the activity's
|
|
20
|
+
* player surface + controls) when params are present.
|
|
21
|
+
* 2. Render the regular navigator when no params (e.g. when
|
|
22
|
+
* launched from the home screen icon, no recent playback
|
|
23
|
+
* was queued).
|
|
24
|
+
*
|
|
25
|
+
* The hook is **idempotent at the React level** — calling it
|
|
26
|
+
* from multiple components reads the same native one-shot
|
|
27
|
+
* queue. The first mount wins; subsequent mounts see `null`.
|
|
28
|
+
* (This matches the Kotlin-side behavior; the alternative is a
|
|
29
|
+
* shared React state, but that requires hoisting the hook to
|
|
30
|
+
* a common ancestor.)
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* function AppContent() {
|
|
35
|
+
* const launchParams = useLaunchParams();
|
|
36
|
+
* if (launchParams) {
|
|
37
|
+
* return <PlayerRoot />;
|
|
38
|
+
* }
|
|
39
|
+
* return <RootNavigator />;
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* For consumers that just want the data without the React
|
|
44
|
+
* subscription (e.g. for analytics), use
|
|
45
|
+
* `getMpvPlayerModule().getLaunchParams()` directly. Note that
|
|
46
|
+
* a direct call consumes the one-shot queue the same way the
|
|
47
|
+
* hook does — so don't call it twice.
|
|
48
|
+
*/
|
|
49
|
+
export function useLaunchParams(): LaunchParams | null {
|
|
50
|
+
const [params, setParams] = useState<LaunchParams | null>(null);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
const bridge = getMpvPlayerModule();
|
|
54
|
+
try {
|
|
55
|
+
const next = bridge.getLaunchParams();
|
|
56
|
+
setParams(next);
|
|
57
|
+
} catch {
|
|
58
|
+
// Bridge threw — assume no launch params.
|
|
59
|
+
setParams(null);
|
|
60
|
+
}
|
|
61
|
+
}, []);
|
|
62
|
+
|
|
63
|
+
return params;
|
|
64
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import {useCallback} from 'react';
|
|
2
|
+
import {useOpenWithResume} from './useOpenWithResume';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* V14 Phase 60: the deep-link helper hook.
|
|
6
|
+
*
|
|
7
|
+
* Apps that handle "open with" / Android Share Sheet / cold-start
|
|
8
|
+
* deep links all converge on the same boilerplate:
|
|
9
|
+
*
|
|
10
|
+
* 1. Listen for `Linking.addEventListener('url', ...)`.
|
|
11
|
+
* 2. On cold start, also call `Linking.getInitialURL()`.
|
|
12
|
+
* 3. Filter for the URI schemes the app handles (typically
|
|
13
|
+
* `content://` and `file://` for shared media files).
|
|
14
|
+
* 4. Derive a display title from the URI basename.
|
|
15
|
+
* 5. Classify the file as audio or video (typically by extension).
|
|
16
|
+
* 6. Forward to the player via `openPlayer({uri, title, type, ...})`.
|
|
17
|
+
*
|
|
18
|
+
* Before V14, every consumer wrote this glue themselves. The shape
|
|
19
|
+
* was always the same; only the title-derivation heuristic and
|
|
20
|
+
* the extension lists varied.
|
|
21
|
+
*
|
|
22
|
+
* `useOpenFromUrl` absorbs steps 3–6. The consumer still owns step
|
|
23
|
+
* 1 + 2 (the `Linking` plumbing), but those are 4 lines of React
|
|
24
|
+
* boilerplate that any RN dev can write. The type-classification
|
|
25
|
+
* + title-derivation + openPlayer forwarding is the actual glue.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```tsx
|
|
29
|
+
* const openFromUrl = useOpenFromUrl();
|
|
30
|
+
*
|
|
31
|
+
* useEffect(() => {
|
|
32
|
+
* Linking.getInitialURL().then(url => url && openFromUrl(url));
|
|
33
|
+
* const sub = Linking.addEventListener('url', ({url}) => openFromUrl(url));
|
|
34
|
+
* return () => sub.remove();
|
|
35
|
+
* }, [openFromUrl]);
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Audio file extensions (lowercase, no dot). Conservative list —
|
|
41
|
+
* any consumer with a custom format can pass a `classifyType` prop
|
|
42
|
+
* (TODO if needed; not in V14 spec).
|
|
43
|
+
*/
|
|
44
|
+
const AUDIO_EXTENSIONS = new Set([
|
|
45
|
+
'mp3',
|
|
46
|
+
'm4a',
|
|
47
|
+
'm4b',
|
|
48
|
+
'aac',
|
|
49
|
+
'flac',
|
|
50
|
+
'ogg',
|
|
51
|
+
'opus',
|
|
52
|
+
'wav',
|
|
53
|
+
'wma',
|
|
54
|
+
'aiff',
|
|
55
|
+
'alac',
|
|
56
|
+
'mp2',
|
|
57
|
+
'amr',
|
|
58
|
+
'ac3',
|
|
59
|
+
'oga',
|
|
60
|
+
]);
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Video file extensions (lowercase, no dot). Same conservatism
|
|
64
|
+
* as `AUDIO_EXTENSIONS`.
|
|
65
|
+
*/
|
|
66
|
+
const VIDEO_EXTENSIONS = new Set([
|
|
67
|
+
'mp4',
|
|
68
|
+
'm4v',
|
|
69
|
+
'mkv',
|
|
70
|
+
'webm',
|
|
71
|
+
'avi',
|
|
72
|
+
'mov',
|
|
73
|
+
'wmv',
|
|
74
|
+
'flv',
|
|
75
|
+
'ts',
|
|
76
|
+
'm2ts',
|
|
77
|
+
'mpg',
|
|
78
|
+
'mpeg',
|
|
79
|
+
'vob',
|
|
80
|
+
'ogv',
|
|
81
|
+
'3gp',
|
|
82
|
+
'3g2',
|
|
83
|
+
'f4v',
|
|
84
|
+
'mp2p',
|
|
85
|
+
]);
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Pull the extension off a URI basename. Strips any query string
|
|
89
|
+
* or fragment first (so `foo.MP3?token=abc` returns `mp3`). Returns
|
|
90
|
+
* an empty string if the basename has no extension.
|
|
91
|
+
*/
|
|
92
|
+
function getExtension(uri: string): string {
|
|
93
|
+
const clean = uri.split('?')[0].split('#')[0];
|
|
94
|
+
// Take the last path segment. Use both / and \\ separators so
|
|
95
|
+
// file:// URIs from Windows-style paths still work.
|
|
96
|
+
const basename = clean.split(/[/\\]/).pop() || '';
|
|
97
|
+
const dot = basename.lastIndexOf('.');
|
|
98
|
+
return dot >= 0 ? basename.slice(dot + 1).toLowerCase() : '';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Classify a media URI as 'audio' or 'video' by file extension.
|
|
103
|
+
* Unknown extensions default to 'video' — matches the SIMBA
|
|
104
|
+
* consumer's pre-V14 convention. (The video default is permissive:
|
|
105
|
+
* if we guess wrong, the player handles both via mpv. Defaulting
|
|
106
|
+
* to audio would silently hide the surface for a video file.)
|
|
107
|
+
*/
|
|
108
|
+
function classifyMediaType(uri: string): 'audio' | 'video' {
|
|
109
|
+
const ext = getExtension(uri);
|
|
110
|
+
if (VIDEO_EXTENSIONS.has(ext)) return 'video';
|
|
111
|
+
if (AUDIO_EXTENSIONS.has(ext)) return 'audio';
|
|
112
|
+
return 'video';
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Derive a human-readable display name from a URI. Strips the
|
|
117
|
+
* file extension, URL-decodes the basename, and falls back to
|
|
118
|
+
* 'Shared File' if the URI has no usable path component.
|
|
119
|
+
*/
|
|
120
|
+
function deriveDisplayName(uri: string): string {
|
|
121
|
+
const clean = uri.split('?')[0].split('#')[0];
|
|
122
|
+
const fileName = clean.split('/').pop() || 'Shared File';
|
|
123
|
+
const noExt = fileName.replace(/\.[^.]+$/, '');
|
|
124
|
+
try {
|
|
125
|
+
return decodeURIComponent(noExt) || 'Shared File';
|
|
126
|
+
} catch {
|
|
127
|
+
// Malformed URI — fall back to the raw basename.
|
|
128
|
+
return noExt || 'Shared File';
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* V14 Phase 60: the deep-link helper hook.
|
|
134
|
+
*
|
|
135
|
+
* Returns a stable callback that takes a URI string and, if the
|
|
136
|
+
* URI looks like a sharable media file (`content://` or `file://`),
|
|
137
|
+
* opens it in the player with a derived title + classified type.
|
|
138
|
+
* Returns `false` for URIs the hook ignores (empty / wrong scheme).
|
|
139
|
+
*
|
|
140
|
+
* The returned callback composes with the nearest
|
|
141
|
+
* `<PlayerResumeProvider>` (set up by `<SimbaPlayer>` in the
|
|
142
|
+
* consumer's App root): the URI is passed as `resumeId` so the
|
|
143
|
+
* consumer's bookmark lookup runs. If the URI has a saved
|
|
144
|
+
* bookmark, playback resumes from that position; otherwise the
|
|
145
|
+
* file plays from the start.
|
|
146
|
+
*/
|
|
147
|
+
export function useOpenFromUrl(): (uri: string) => Promise<boolean> {
|
|
148
|
+
const openPlayer = useOpenWithResume();
|
|
149
|
+
return useCallback(
|
|
150
|
+
async (uri: string): Promise<boolean> => {
|
|
151
|
+
// Defensive: empty / non-sharable URIs are no-ops.
|
|
152
|
+
if (!uri) return false;
|
|
153
|
+
if (!uri.startsWith('content://') && !uri.startsWith('file://')) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const title = deriveDisplayName(uri);
|
|
158
|
+
const type = classifyMediaType(uri);
|
|
159
|
+
|
|
160
|
+
// Pass the URI as `resumeId` so the consumer's bookmark
|
|
161
|
+
// lookup runs. Shared content:// URIs are not in the
|
|
162
|
+
// bookmark slice, so the lookup typically returns 0 (start
|
|
163
|
+
// from the beginning). But if the user previously saved a
|
|
164
|
+
// bookmark for the same URI, playback resumes from there.
|
|
165
|
+
return openPlayer({
|
|
166
|
+
uri,
|
|
167
|
+
title,
|
|
168
|
+
type,
|
|
169
|
+
resumeId: uri,
|
|
170
|
+
});
|
|
171
|
+
},
|
|
172
|
+
[openPlayer],
|
|
173
|
+
);
|
|
174
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import React, { createContext, useCallback, useContext } from 'react';
|
|
2
|
+
import { usePlayerActivity } from './usePlayerActivity';
|
|
3
|
+
import type { OpenPlayerOptions } from './usePlayerActivity';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* V13+ DX: a lookup function the module calls to resolve a saved
|
|
7
|
+
* resume position for an item. The consumer provides this once at
|
|
8
|
+
* the root of the app; every call site that wants resume-aware
|
|
9
|
+
* openPlayer just passes `resumeId: item.id` to the hook's returned
|
|
10
|
+
* function.
|
|
11
|
+
*
|
|
12
|
+
* Why the consumer provides the lookup (not the module): every
|
|
13
|
+
* project persists bookmarks/history differently — Redux, SQLite,
|
|
14
|
+
* MMKV, AsyncStorage, server-side. The module can't know the
|
|
15
|
+
* shape of the consumer's persistence layer, so the consumer
|
|
16
|
+
* provides a small adapter.
|
|
17
|
+
*
|
|
18
|
+
* The lookup must be **pure and synchronous**. The module calls it
|
|
19
|
+
* inline at `openPlayer(...)` time. If you need async resolution,
|
|
20
|
+
* hydrate the resume position into the consumer's state first and
|
|
21
|
+
* have the lookup return the cached value.
|
|
22
|
+
*/
|
|
23
|
+
export interface PlayerResumeLookup {
|
|
24
|
+
/**
|
|
25
|
+
* Returns the saved resume position in milliseconds for the given
|
|
26
|
+
* item id. Return 0 (or undefined) when no saved position exists
|
|
27
|
+
* (file will play from the start).
|
|
28
|
+
*/
|
|
29
|
+
getResumePosition(itemId: string): number | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const PlayerResumeContext = createContext<PlayerResumeLookup | null>(null);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Wrap the consumer app (or any subtree) with a resume lookup.
|
|
36
|
+
* Place this ABOVE every call site of `useOpenWithResume()`. In
|
|
37
|
+
* practice, wrap the same subtree as `<PlayerProvider>` (Phase 54
|
|
38
|
+
* will compose them in PlayerActivity, but for now the consumer
|
|
39
|
+
* can wrap at App.tsx).
|
|
40
|
+
*
|
|
41
|
+
* The actual JSX component lives in `PlayerResumeProvider.tsx` (a
|
|
42
|
+
* `.tsx` file) so the legacy JSX transform doesn't reject the
|
|
43
|
+
* `React.ReactElement` return type. This module re-exports it
|
|
44
|
+
* through the index.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```tsx
|
|
48
|
+
* function App() {
|
|
49
|
+
* const lookup = useMemo<PlayerResumeLookup>(() => ({
|
|
50
|
+
* getResumePosition: (id) => store.getState().bookmarks.byId[id]?.position ?? 0,
|
|
51
|
+
* }), []);
|
|
52
|
+
* return (
|
|
53
|
+
* <PlayerResumeProvider lookup={lookup}>
|
|
54
|
+
* <Navigator />
|
|
55
|
+
* </PlayerResumeProvider>
|
|
56
|
+
* );
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export interface PlayerResumeProviderProps {
|
|
61
|
+
lookup: PlayerResumeLookup;
|
|
62
|
+
children: React.ReactNode;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* JSX component that wraps a subtree with the player resume context.
|
|
67
|
+
* Returns the same React tree with the context provider wrapping
|
|
68
|
+
* `children`. Place this above every call site of `useOpenWithResume`.
|
|
69
|
+
*/
|
|
70
|
+
export function PlayerResumeProvider({
|
|
71
|
+
lookup,
|
|
72
|
+
children,
|
|
73
|
+
}: PlayerResumeProviderProps): React.ReactElement {
|
|
74
|
+
return (
|
|
75
|
+
<PlayerResumeContext.Provider value={lookup}>
|
|
76
|
+
{children}
|
|
77
|
+
</PlayerResumeContext.Provider>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* DX-friendly alternative to `usePlayerActivity().openPlayer`. The
|
|
83
|
+
* returned function accepts the same `OpenPlayerOptions` as the
|
|
84
|
+
* V13 `usePlayerActivity` hook, plus an optional `resumeId`. When
|
|
85
|
+
* `resumeId` is provided, the module:
|
|
86
|
+
*
|
|
87
|
+
* 1. Reads the saved resume position from the nearest
|
|
88
|
+
* `<PlayerResumeProvider>`'s lookup.
|
|
89
|
+
* 2. Substitutes it for `startPositionMs` (if the consumer didn't
|
|
90
|
+
* already pass one).
|
|
91
|
+
* 3. Calls the underlying `openPlayer` with the resolved options.
|
|
92
|
+
*
|
|
93
|
+
* If no `PlayerResumeProvider` is in scope, the hook behaves
|
|
94
|
+
* identically to `usePlayerActivity().openPlayer` (no resume
|
|
95
|
+
* lookup, no error).
|
|
96
|
+
*
|
|
97
|
+
* This is the pattern the V13 migration prefers: every screen that
|
|
98
|
+
* opens a media item passes `resumeId: item.id` and lets the
|
|
99
|
+
* module handle the resume-position plumbing.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```tsx
|
|
103
|
+
* function TrackRow({track}) {
|
|
104
|
+
* const openPlayer = useOpenWithResume();
|
|
105
|
+
* return (
|
|
106
|
+
* <Pressable onPress={() => openPlayer({
|
|
107
|
+
* uri: track.uri,
|
|
108
|
+
* title: track.title,
|
|
109
|
+
* type: 'audio',
|
|
110
|
+
* resumeId: track.id,
|
|
111
|
+
* })}>
|
|
112
|
+
* <Text>{track.title}</Text>
|
|
113
|
+
* </Pressable>
|
|
114
|
+
* );
|
|
115
|
+
* }
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export function useOpenWithResume(): (opts: OpenPlayerOptions & {resumeId?: string}) => Promise<boolean> {
|
|
119
|
+
const lookup = useContext(PlayerResumeContext);
|
|
120
|
+
const { openPlayer } = usePlayerActivity();
|
|
121
|
+
return useCallback(
|
|
122
|
+
(opts) => {
|
|
123
|
+
const { resumeId, startPositionMs: providedStartMs, ...rest } = opts;
|
|
124
|
+
// If the consumer passed an explicit startPositionMs, respect it.
|
|
125
|
+
// Otherwise, if a resumeId + lookup are available, ask the lookup.
|
|
126
|
+
// Otherwise, fall back to 0.
|
|
127
|
+
let startPositionMs = providedStartMs ?? 0;
|
|
128
|
+
if (resumeId != null && lookup != null && providedStartMs == null) {
|
|
129
|
+
try {
|
|
130
|
+
const saved = lookup.getResumePosition(resumeId);
|
|
131
|
+
if (typeof saved === 'number' && Number.isFinite(saved) && saved > 0) {
|
|
132
|
+
startPositionMs = saved;
|
|
133
|
+
}
|
|
134
|
+
} catch {
|
|
135
|
+
// Lookup threw — fall back to 0. The consumer's persistence
|
|
136
|
+
// layer is buggy, but the player should still launch.
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return openPlayer({ ...rest, startPositionMs });
|
|
140
|
+
},
|
|
141
|
+
[lookup, openPlayer],
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* DX hook for the "press a row to play" pattern. Returns a stable
|
|
147
|
+
* `playItem` function that takes a minimal item shape and a few
|
|
148
|
+
* optional fields, and does the openPlayer wiring. The hook reads
|
|
149
|
+
* the resume position from the nearest `PlayerResumeProvider`
|
|
150
|
+
* automatically.
|
|
151
|
+
*
|
|
152
|
+
* This is sugar on top of `useOpenWithResume` — if the consumer
|
|
153
|
+
* doesn't need this level of abstraction, use `useOpenWithResume`
|
|
154
|
+
* directly.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```tsx
|
|
158
|
+
* function TrackRow({track}) {
|
|
159
|
+
* const play = usePlayItem();
|
|
160
|
+
* return <Pressable onPress={() => play(track, {type: 'audio'})}>...</Pressable>;
|
|
161
|
+
* }
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export function usePlayItem(): (item: {id: string; uri: string; title: string}, opts: {type: 'video' | 'audio'}) => Promise<boolean> {
|
|
165
|
+
const openPlayer = useOpenWithResume();
|
|
166
|
+
return useCallback(
|
|
167
|
+
(item, opts) =>
|
|
168
|
+
openPlayer({
|
|
169
|
+
uri: item.uri,
|
|
170
|
+
title: item.title,
|
|
171
|
+
type: opts.type,
|
|
172
|
+
resumeId: item.id,
|
|
173
|
+
}),
|
|
174
|
+
[openPlayer],
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Re-export the resume context for advanced consumers (testing, etc.). */
|
|
179
|
+
export { PlayerResumeContext };
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* V13+ DX: map a content/media-kind string to a V13 stream type
|
|
183
|
+
* (`'video' | 'audio'`). Useful for consumers that historically
|
|
184
|
+
* passed a content type (`'music'`, `'movie'`, etc.) where V13
|
|
185
|
+
* expects a stream type.
|
|
186
|
+
*
|
|
187
|
+
* The mapping is conservative — anything not in the explicit
|
|
188
|
+
* "video" / "audio" lists falls back to `'audio'` (the safer
|
|
189
|
+
* default; if you need a different default for your app, copy
|
|
190
|
+
* the function and adjust).
|
|
191
|
+
*
|
|
192
|
+
* The map covers the common SIMBA content kinds. Consumers with
|
|
193
|
+
* custom kinds should add their own cases or pre-map before
|
|
194
|
+
* calling.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* import { resolveStreamType } from '@simba-dev/react-native-media-player';
|
|
199
|
+
*
|
|
200
|
+
* openPlayer({
|
|
201
|
+
* uri: item.uri,
|
|
202
|
+
* title: item.title,
|
|
203
|
+
* type: resolveStreamType(item.kind), // 'music' -> 'audio', 'movie' -> 'video', etc.
|
|
204
|
+
* });
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
export type ContentKind =
|
|
208
|
+
| 'video' | 'audio' // stream types (passthrough)
|
|
209
|
+
| 'music' | 'audiobook' | 'podcast' | 'radio' | 'archive-audio' // audio content
|
|
210
|
+
| 'movie' | 'episode' | 'live-tv' | 'video-file' // video content
|
|
211
|
+
| string; // unknown — falls back to 'audio'
|
|
212
|
+
|
|
213
|
+
export function resolveStreamType(input: ContentKind): 'video' | 'audio' {
|
|
214
|
+
// Already a stream type — passthrough.
|
|
215
|
+
if (input === 'video' || input === 'audio') return input;
|
|
216
|
+
// Audio content kinds.
|
|
217
|
+
switch (input) {
|
|
218
|
+
case 'music':
|
|
219
|
+
case 'audiobook':
|
|
220
|
+
case 'podcast':
|
|
221
|
+
case 'radio':
|
|
222
|
+
case 'archive-audio':
|
|
223
|
+
return 'audio';
|
|
224
|
+
// Video content kinds.
|
|
225
|
+
case 'movie':
|
|
226
|
+
case 'episode':
|
|
227
|
+
case 'live-tv':
|
|
228
|
+
case 'video-file':
|
|
229
|
+
return 'video';
|
|
230
|
+
default:
|
|
231
|
+
// Unknown kind — conservative default. Better to launch an
|
|
232
|
+
// audio player on an unknown file than to crash on a video
|
|
233
|
+
// mismatch.
|
|
234
|
+
return 'audio';
|
|
235
|
+
}
|
|
236
|
+
}
|