@simba-dev/react-native-media-player 1.1.0 → 1.2.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/package.json +2 -2
- package/src/components/PlayerProvider.tsx +288 -7
- package/src/hooks/PlayerResumeProvider.tsx +21 -0
- package/src/hooks/SimbaPlayer.tsx +93 -0
- package/src/hooks/useLaunchParams.tsx +64 -0
- package/src/hooks/useOpenWithResume.tsx +236 -0
- package/src/hooks/usePlayerActivity.ts +103 -0
- package/src/index.ts +20 -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,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
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
getMpvPlayerModule,
|
|
4
|
+
type LaunchParams,
|
|
5
|
+
} from '../bridge/MpvPlayerModule';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options accepted by `usePlayerActivity().openPlayer(opts)`. This is
|
|
9
|
+
* the V13 module-level signature — the consumer's V11 `openPlayer`
|
|
10
|
+
* took `{uri, title, duration, source, type, mediaType}` (the
|
|
11
|
+
* extra fields were Redux-dispatch concerns that V13 drops because
|
|
12
|
+
* there's no Redux dispatch path anymore).
|
|
13
|
+
*/
|
|
14
|
+
export interface OpenPlayerOptions {
|
|
15
|
+
/** Media URI (`file://`, `content://`, or `https://`). */
|
|
16
|
+
uri: string;
|
|
17
|
+
/** Display title for the notification / top bar. */
|
|
18
|
+
title: string;
|
|
19
|
+
/** `'video'` launches the full-screen activity; `'audio'` plays in the background. */
|
|
20
|
+
type: 'video' | 'audio';
|
|
21
|
+
/** Resume position in milliseconds. 0 (or omitted) starts from the beginning. */
|
|
22
|
+
startPositionMs?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* V13 Phase 52: thin wrapper around `bridge.openPlayer` +
|
|
27
|
+
* `bridge.getLaunchParams`. Replaces the consumer's V11
|
|
28
|
+
* `usePlaybackCommands()` hook for the 33+ call sites that
|
|
29
|
+
* previously launched `PlayerActivity` from screens like
|
|
30
|
+
* `NowPlaying`, `AllVideos`, `Bookmarks`, etc.
|
|
31
|
+
*
|
|
32
|
+
* Why a separate hook (not just `usePlayer().commands.openPlayer`):
|
|
33
|
+
* - `usePlayer()` requires a `<PlayerProvider>` ancestor for
|
|
34
|
+
* live state; the activity-launch calls happen from screen
|
|
35
|
+
* components (lists, grids) that are NOT inside the player
|
|
36
|
+
* activity yet.
|
|
37
|
+
* - Returning a smaller surface keeps the screen-side code
|
|
38
|
+
* self-documenting: "this screen launches the player" vs
|
|
39
|
+
* "this screen is inside the player".
|
|
40
|
+
* - Easier to mock in consumer-side tests (jest.mock the
|
|
41
|
+
* whole `usePlayerActivity` module).
|
|
42
|
+
*
|
|
43
|
+
* The hook is non-throwing: the bridge resolves to a no-op
|
|
44
|
+
* fallback in jest / web previews, so `openPlayer` resolves with
|
|
45
|
+
* `false` and `getLaunchParams` returns `null` — both clearly
|
|
46
|
+
* falsy, easy to assert against.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```tsx
|
|
50
|
+
* function NowPlaying() {
|
|
51
|
+
* const { openPlayer } = usePlayerActivity();
|
|
52
|
+
* const onPressSong = (song: Song) => {
|
|
53
|
+
* openPlayer({
|
|
54
|
+
* uri: song.uri,
|
|
55
|
+
* title: song.title,
|
|
56
|
+
* type: song.hasVideo ? 'video' : 'audio',
|
|
57
|
+
* startPositionMs: song.resumePositionMs,
|
|
58
|
+
* });
|
|
59
|
+
* };
|
|
60
|
+
* // ...
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export interface UsePlayerActivityResult {
|
|
65
|
+
/**
|
|
66
|
+
* Launch the dedicated `PlayerActivity` with the given media.
|
|
67
|
+
* Resolves with `true` on a successful `startActivity` and
|
|
68
|
+
* `false` otherwise (e.g. when the activity is unavailable on
|
|
69
|
+
* the device, the URI is unplayable, or the bridge is in
|
|
70
|
+
* no-op mode).
|
|
71
|
+
*/
|
|
72
|
+
openPlayer(opts: OpenPlayerOptions): Promise<boolean>;
|
|
73
|
+
/**
|
|
74
|
+
* One-shot accessor for the launch params the most recent
|
|
75
|
+
* `openPlayer` call handed to `PlayerActivity`. Returns
|
|
76
|
+
* `null` when called from MainActivity or after the first
|
|
77
|
+
* read has already consumed the value. The Kotlin side
|
|
78
|
+
* keeps a single-shot queue, so call this exactly once
|
|
79
|
+
* per `PlayerActivity.onCreate`.
|
|
80
|
+
*/
|
|
81
|
+
getLaunchParams(): LaunchParams | null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function usePlayerActivity(): UsePlayerActivityResult {
|
|
85
|
+
return useMemo<UsePlayerActivityResult>(
|
|
86
|
+
() => ({
|
|
87
|
+
openPlayer: (opts) => {
|
|
88
|
+
const bridge = getMpvPlayerModule();
|
|
89
|
+
return bridge.openPlayer(
|
|
90
|
+
opts.uri,
|
|
91
|
+
opts.title,
|
|
92
|
+
opts.type,
|
|
93
|
+
opts.startPositionMs ?? 0,
|
|
94
|
+
);
|
|
95
|
+
},
|
|
96
|
+
getLaunchParams: () => {
|
|
97
|
+
const bridge = getMpvPlayerModule();
|
|
98
|
+
return bridge.getLaunchParams();
|
|
99
|
+
},
|
|
100
|
+
}),
|
|
101
|
+
[],
|
|
102
|
+
);
|
|
103
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -54,9 +54,29 @@ export {
|
|
|
54
54
|
type PlayerCommands,
|
|
55
55
|
type PlayerProgress,
|
|
56
56
|
type PlayerState,
|
|
57
|
+
type PlaylistEntry,
|
|
57
58
|
type UsePlayerResult,
|
|
58
59
|
} from './types/player';
|
|
59
60
|
|
|
61
|
+
export {
|
|
62
|
+
usePlayerActivity,
|
|
63
|
+
type OpenPlayerOptions,
|
|
64
|
+
type UsePlayerActivityResult,
|
|
65
|
+
} from './hooks/usePlayerActivity';
|
|
66
|
+
|
|
67
|
+
export {
|
|
68
|
+
PlayerResumeProvider,
|
|
69
|
+
useOpenWithResume,
|
|
70
|
+
usePlayItem,
|
|
71
|
+
resolveStreamType,
|
|
72
|
+
type ContentKind,
|
|
73
|
+
type PlayerResumeLookup,
|
|
74
|
+
type PlayerResumeProviderProps,
|
|
75
|
+
} from './hooks/useOpenWithResume';
|
|
76
|
+
|
|
77
|
+
export {SimbaPlayer, type SimbaPlayerProps} from './hooks/SimbaPlayer';
|
|
78
|
+
export {useLaunchParams} from './hooks/useLaunchParams';
|
|
79
|
+
|
|
60
80
|
export {
|
|
61
81
|
getMpvPlayerModule,
|
|
62
82
|
subscribePlayerEvent,
|