react-native-video-provider 0.2.2 → 0.2.4

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.
@@ -1 +1 @@
1
- {"version":3,"file":"VideoPlayer.d.ts","sourceRoot":"","sources":["../../../../src/components/VideoPlayer.tsx"],"names":[],"mappings":"AACA,OAAO,EAAoB,KAAK,SAAS,EAAE,MAAM,cAAc,CAAC;AAEhE,OAAO,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,mBAAgB,CAAC;AAI/E,MAAM,WAAW,gBAAiB,SAAQ,SAAS;IACjD,MAAM,EAAE,WAAW,CAAC;IACpB,oBAAoB;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,4CAA4C;IAC5C,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB;;;;OAIG;IACH,WAAW,CAAC,EAAE,eAAe,CAAC;IAC9B;;;;;OAKG;IACH,qBAAqB,CAAC,EAAE,eAAe,CAAC;CACzC;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,EAC1B,MAAM,EACN,QAAe,EACf,SAAS,EACT,QAAe,EACf,UAAU,EACV,WAAW,EACX,qBAAqB,EACrB,KAAK,EACL,GAAG,IAAI,EACR,EAAE,gBAAgB,+BAuClB"}
1
+ {"version":3,"file":"VideoPlayer.d.ts","sourceRoot":"","sources":["../../../../src/components/VideoPlayer.tsx"],"names":[],"mappings":"AACA,OAAO,EAA8B,KAAK,SAAS,EAAE,MAAM,cAAc,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAsB,CAAC;AAGpD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAiB,CAAC;AACrD,OAAO,KAAK,EACV,eAAe,EACf,UAAU,EACV,UAAU,EACV,WAAW,EACZ,MAAM,mBAAgB,CAAC;AAIxB,MAAM,WAAW,gBAAiB,SAAQ,SAAS;IACjD,MAAM,EAAE,WAAW,CAAC;IACpB,oBAAoB;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,4CAA4C;IAC5C,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,iDAAiD;IACjD,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,qBAAqB;IACrB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB;;;;OAIG;IACH,WAAW,CAAC,EAAE,eAAe,CAAC;IAC9B;;;;;OAKG;IACH,qBAAqB,CAAC,EAAE,eAAe,CAAC;IACxC;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,+DAA+D;IAC/D,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE,aAAa,CAAC,QAAQ,CAAC,KAAK,IAAI,CAAC;IACzD,gDAAgD;IAChD,WAAW,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,KAAK,IAAI,CAAC;IAC3C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,CAAC;CACvC;AAED;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,WAAW,2GAoGvB,CAAC"}
@@ -3,6 +3,7 @@ export { useVideoManager } from './provider/VideoContext.js';
3
3
  export { VideoManager, FULLSCREEN_SURFACE_ID, FLOATING_SURFACE_ID, } from './core/VideoManager.js';
4
4
  export { VideoSurface, type VideoSurfaceProps, } from './components/VideoSurface.js';
5
5
  export { VideoPlayer, type VideoPlayerProps } from './components/VideoPlayer.js';
6
+ export { VideoFeed, feedSurfaceId, type VideoFeedProps, type VideoFeedRenderInfo, } from './components/VideoFeed.js';
6
7
  export { FullscreenPlayer } from './components/FullscreenPlayer.js';
7
8
  export { FloatingPlayer, type FloatingPlayerProps, } from './components/FloatingPlayer.js';
8
9
  export { MiniPlayer, type MiniPlayerProps } from './components/MiniPlayer.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,KAAK,kBAAkB,GACxB,MAAM,6BAA0B,CAAC;AAClC,OAAO,EAAE,eAAe,EAAE,MAAM,4BAAyB,CAAC;AAC1D,OAAO,EACL,YAAY,EACZ,qBAAqB,EACrB,mBAAmB,GACpB,MAAM,wBAAqB,CAAC;AAE7B,OAAO,EACL,YAAY,EACZ,KAAK,iBAAiB,GACvB,MAAM,8BAA2B,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,KAAK,gBAAgB,EAAE,MAAM,6BAA0B,CAAC;AAC9E,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAA+B,CAAC;AACjE,OAAO,EACL,cAAc,EACd,KAAK,mBAAmB,GACzB,MAAM,gCAA6B,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,KAAK,eAAe,EAAE,MAAM,4BAAyB,CAAC;AAC3E,OAAO,EACL,aAAa,EACb,KAAK,kBAAkB,GACxB,MAAM,+BAA4B,CAAC;AACpC,OAAO,EACL,cAAc,EACd,KAAK,mBAAmB,GACzB,MAAM,gCAA6B,CAAC;AAErC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAkB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAqB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAAuB,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAgB,CAAC;AACxC,OAAO,EACL,cAAc,EACd,KAAK,kBAAkB,GACxB,MAAM,2BAAwB,CAAC;AAEhC,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAoB,CAAC;AAEhD,YAAY,EACV,WAAW,EACX,UAAU,EACV,eAAe,EACf,UAAU,EACV,cAAc,EACd,UAAU,EACV,UAAU,EACV,gBAAgB,EAChB,mBAAmB,EACnB,aAAa,EACb,cAAc,GACf,MAAM,kBAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,KAAK,kBAAkB,GACxB,MAAM,6BAA0B,CAAC;AAClC,OAAO,EAAE,eAAe,EAAE,MAAM,4BAAyB,CAAC;AAC1D,OAAO,EACL,YAAY,EACZ,qBAAqB,EACrB,mBAAmB,GACpB,MAAM,wBAAqB,CAAC;AAE7B,OAAO,EACL,YAAY,EACZ,KAAK,iBAAiB,GACvB,MAAM,8BAA2B,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,KAAK,gBAAgB,EAAE,MAAM,6BAA0B,CAAC;AAC9E,OAAO,EACL,SAAS,EACT,aAAa,EACb,KAAK,cAAc,EACnB,KAAK,mBAAmB,GACzB,MAAM,2BAAwB,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAA+B,CAAC;AACjE,OAAO,EACL,cAAc,EACd,KAAK,mBAAmB,GACzB,MAAM,gCAA6B,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,KAAK,eAAe,EAAE,MAAM,4BAAyB,CAAC;AAC3E,OAAO,EACL,aAAa,EACb,KAAK,kBAAkB,GACxB,MAAM,+BAA4B,CAAC;AACpC,OAAO,EACL,cAAc,EACd,KAAK,mBAAmB,GACzB,MAAM,gCAA6B,CAAC;AAErC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAkB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAqB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAAuB,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAgB,CAAC;AACxC,OAAO,EACL,cAAc,EACd,KAAK,kBAAkB,GACxB,MAAM,2BAAwB,CAAC;AAEhC,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAoB,CAAC;AAEhD,YAAY,EACV,WAAW,EACX,UAAU,EACV,eAAe,EACf,UAAU,EACV,cAAc,EACd,UAAU,EACV,UAAU,EACV,gBAAgB,EAChB,mBAAmB,EACnB,aAAa,EACb,cAAc,GACf,MAAM,kBAAS,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-video-provider",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Singleton-engine video library for React Native (one native player, many surfaces)",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -18,6 +18,7 @@
18
18
  "android",
19
19
  "ios",
20
20
  "cpp",
21
+ "scripts/postinstall.js",
21
22
  "*.podspec",
22
23
  "react-native.config.js",
23
24
  "!ios/build",
@@ -35,6 +36,7 @@
35
36
  "example": "yarn workspace react-native-video-provider-example",
36
37
  "clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
37
38
  "prepare": "bob build",
39
+ "postinstall": "node scripts/postinstall.js",
38
40
  "typecheck": "tsc",
39
41
  "test": "jest",
40
42
  "lint": "eslint \"**/*.{js,ts,tsx}\""
@@ -46,14 +48,14 @@
46
48
  ],
47
49
  "repository": {
48
50
  "type": "git",
49
- "url": "git+https://github.com/grdp99/react-native-au-video.git"
51
+ "url": "git+https://github.com/gurdeep99/react-native-au-video.git"
50
52
  },
51
53
  "author": "Gurdeep Singh <grdp99@gmail.com> (https://gurdeep.net)",
52
54
  "license": "MIT",
53
55
  "bugs": {
54
- "url": "https://github.com/grdp99/react-native-au-video/issues"
56
+ "url": "https://github.com/gurdeep99/react-native-au-video/issues"
55
57
  },
56
- "homepage": "https://github.com/grdp99/react-native-au-video#readme",
58
+ "homepage": "https://github.com/gurdeep99/react-native-au-video#readme",
57
59
  "publishConfig": {
58
60
  "registry": "https://registry.npmjs.org/"
59
61
  },
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Prints a banner after install. Wrapped so it can NEVER fail a consumer's
5
+ * `npm install` — any error is swallowed and the process always exits 0.
6
+ * Skipped in CI and non-interactive shells to avoid polluting build logs.
7
+ */
8
+
9
+ const BANNER = `
10
+ ++
11
+ +++ +++ +++++++++++ ++
12
+ ++++++++++++++ ++++++++++ ++++++++++++++ +++ +++++++ ++ +++
13
+ ++ +++ +++ ++++++++++++ +++ +++++ ++++ +++++++ ++++ +++++++ ++ +++
14
+ ++ +++ +++ ++++ +++ +++++++++++++ ++++ +++++ ++++ +++++ +++++++ ++ +++++++++++
15
+ +++ +++ +++ ++++ ++++ +++ +++++++ +++++++ +++++++++ +++++++++ ++++ ++++
16
+ ++++ +++ +++ ++++++++++ ++++++ +++ ++++++ +++++ +++ ++ +++ +++
17
+ +++ ++ +++ ++++ +++ +++ ++++++++ ++++ ++++ +++ ++++ +++ +++ +++
18
+ ++++ ++++ ++++ +++ ++ +++++ ++ +++ ++++
19
+ +++++++ ++++ ++ ++++++ ++++++++++
20
+ ++++ ++++ + +++ ++++ + +++++++++++
21
+ +++ +++ ++++ +++++ ++++ +++ +++ ++++ ++++ +++
22
+ ++++++++ ++ ++ ++ +++ + ++
23
+ `;
24
+
25
+ try {
26
+ const quiet = process.env.CI || process.env.npm_config_loglevel === 'silent';
27
+ if (!quiet && process.stdout && process.stdout.isTTY) {
28
+ const cyan = '\x1b[36m';
29
+ const dim = '\x1b[2m';
30
+ const reset = '\x1b[0m';
31
+ process.stdout.write(cyan + BANNER + reset + '\n');
32
+ process.stdout.write(
33
+ dim +
34
+ ' react-native-video-provider — one native player, many surfaces.\n' +
35
+ ' Docs: https://github.com/gurdeep99/react-native-video-provider#readme\n' +
36
+ reset +
37
+ '\n'
38
+ );
39
+ }
40
+ } catch {
41
+ // Never break an install over a banner.
42
+ }
43
+
44
+ process.exit(0);
@@ -37,13 +37,19 @@ export function VideoControls({
37
37
  onClose,
38
38
  }: VideoControlsProps) {
39
39
  const manager = useVideoManager();
40
+ const status = usePlayback((s) => s.status);
40
41
  const playing = usePlayback((s) => s.playing);
41
42
  const buffering = usePlayback((s) => s.buffering);
43
+ const loading = usePlayback((s) => s.loading);
42
44
  const position = usePlayback((s) => s.position);
43
45
  const duration = usePlayback((s) => s.duration);
44
46
  const muted = usePlayback((s) => s.muted);
45
47
  const fullscreen = usePlayback((s) => s.fullscreen);
46
48
 
49
+ // No known duration once loaded — a live stream or a video with no fixed
50
+ // end. There's nothing to seek, so hide the seek bar entirely.
51
+ const live = status !== 'idle' && !loading && duration <= 0;
52
+
47
53
  const [visible, setVisible] = useState(true);
48
54
  const hideTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
49
55
  const trackWidth = useRef(0);
@@ -85,6 +91,34 @@ export function VideoControls({
85
91
 
86
92
  const progress = duration > 0 ? Math.min(position / duration, 1) : 0;
87
93
 
94
+ const muteButton = (
95
+ <Pressable
96
+ style={styles.button}
97
+ onPress={() => (muted ? manager.unmute() : manager.mute())}
98
+ hitSlop={8}
99
+ >
100
+ {muted ? (
101
+ <SvgIcons icon="muteUnmute" size={18} fill="#fff" />
102
+ ) : (
103
+ <SvgIcons icon="muteUnmute" type="mute" size={18} fill="#fff" />
104
+ )}
105
+ </Pressable>
106
+ );
107
+
108
+ const fullscreenButton = showFullscreenButton ? (
109
+ <Pressable
110
+ style={styles.button}
111
+ onPress={() => manager.toggleFullscreen()}
112
+ hitSlop={8}
113
+ >
114
+ {fullscreen ? (
115
+ <SvgIcons icon="fullScreen" size={18} fill="#fff" />
116
+ ) : (
117
+ <SvgIcons icon="fullScreen" type="full" size={18} fill="#fff" />
118
+ )}
119
+ </Pressable>
120
+ ) : null;
121
+
88
122
  return (
89
123
  <View style={StyleSheet.absoluteFill} pointerEvents="box-none">
90
124
  <GestureOverlay
@@ -102,22 +136,7 @@ export function VideoControls({
102
136
  ) : (
103
137
  <View />
104
138
  )}
105
- <Pressable
106
- style={styles.button}
107
- onPress={() => (muted ? manager.unmute() : manager.mute())}
108
- hitSlop={8}
109
- >
110
- {muted ? (
111
- <SvgIcons icon="muteUnmute" type="mute" size={18} fill="#fff" />
112
- ) : (
113
- <SvgIcons
114
- icon="muteUnmute"
115
- type="unmute"
116
- size={18}
117
- fill="#fff"
118
- />
119
- )}
120
- </Pressable>
139
+ {!live ? muteButton : <View />}
121
140
  </View>
122
141
 
123
142
  <Pressable
@@ -138,36 +157,29 @@ export function VideoControls({
138
157
  </Pressable>
139
158
 
140
159
  <View style={styles.bottomRow}>
141
- <Text style={styles.time}>{formatTime(position)}</Text>
142
- <Pressable
143
- style={styles.track}
144
- onLayout={onTrackLayout}
145
- onPress={onTrackPress}
146
- >
147
- <View style={styles.trackBg} />
148
- <View
149
- style={[styles.trackFill, { width: `${progress * 100}%` }]}
150
- />
151
- </Pressable>
152
- <Text style={styles.time}>{formatTime(duration)}</Text>
153
- {showFullscreenButton ? (
154
- <Pressable
155
- style={styles.button}
156
- onPress={() => manager.toggleFullscreen()}
157
- hitSlop={8}
158
- >
159
- {fullscreen ? (
160
- <SvgIcons icon="fullScreen" size={18} fill="#fff" />
161
- ) : (
162
- <SvgIcons
163
- icon="fullScreen"
164
- type="full"
165
- size={18}
166
- fill="#fff"
160
+ {live ? (
161
+ <>
162
+ {muteButton}
163
+ <View style={styles.spacer} />
164
+ {fullscreenButton}
165
+ </>
166
+ ) : (
167
+ <>
168
+ <Text style={styles.time}>{formatTime(position)}</Text>
169
+ <Pressable
170
+ style={styles.track}
171
+ onLayout={onTrackLayout}
172
+ onPress={onTrackPress}
173
+ >
174
+ <View style={styles.trackBg} />
175
+ <View
176
+ style={[styles.trackFill, { width: `${progress * 100}%` }]}
167
177
  />
168
- )}
169
- </Pressable>
170
- ) : null}
178
+ </Pressable>
179
+ <Text style={styles.time}>{formatTime(duration)}</Text>
180
+ {fullscreenButton}
181
+ </>
182
+ )}
171
183
  </View>
172
184
  </View>
173
185
  ) : null}
@@ -214,6 +226,9 @@ const styles = StyleSheet.create({
214
226
  height: 24,
215
227
  justifyContent: 'center',
216
228
  },
229
+ spacer: {
230
+ flex: 1,
231
+ },
217
232
  trackBg: {
218
233
  position: 'absolute',
219
234
  left: 0,
@@ -0,0 +1,201 @@
1
+ import {
2
+ useCallback,
3
+ useEffect,
4
+ useRef,
5
+ useState,
6
+ type ReactNode,
7
+ } from 'react';
8
+ import {
9
+ FlatList,
10
+ StyleSheet,
11
+ useWindowDimensions,
12
+ View,
13
+ type FlatListProps,
14
+ } from 'react-native';
15
+ import { useVideoManager } from '../provider/VideoContext';
16
+ import type { ResizeMode, VideoSource } from '../types/video';
17
+ import { VideoSurface } from './VideoSurface';
18
+
19
+ /** Surface id a feed item registers under. */
20
+ export const feedSurfaceId = (id: string) => `feed:${id}`;
21
+
22
+ /** Minimal shape of a FlatList viewability token (avoids RN type-name drift). */
23
+ interface FeedViewToken {
24
+ item: unknown;
25
+ index: number | null | undefined;
26
+ isViewable: boolean;
27
+ }
28
+
29
+ export interface VideoFeedRenderInfo<T> {
30
+ item: T;
31
+ index: number;
32
+ /** True when this item is the one currently playing. */
33
+ focused: boolean;
34
+ }
35
+
36
+ export interface VideoFeedProps<T extends VideoSource> extends Omit<
37
+ FlatListProps<T>,
38
+ | 'data'
39
+ | 'renderItem'
40
+ | 'keyExtractor'
41
+ | 'onViewableItemsChanged'
42
+ | 'viewabilityConfig'
43
+ | 'getItemLayout'
44
+ > {
45
+ /** The videos. Each needs a stable `id` (used for the surface + handoff). */
46
+ data: T[];
47
+ /** Height of each item. Default: the window height (a full-screen reels feed). */
48
+ itemHeight?: number;
49
+ resizeMode?: ResizeMode;
50
+ /** Start muted. Default `true` (autoplay policies + typical feed UX). */
51
+ muted?: boolean;
52
+ /** Loop the focused video. Default `true`. */
53
+ repeat?: boolean;
54
+ /** Percent of an item that must be visible to become focused. Default 80. */
55
+ visibilityThreshold?: number;
56
+ /** Overlay above each item — captions, like button, a tap-to-pause layer, … */
57
+ renderOverlay?: (info: VideoFeedRenderInfo<T>) => ReactNode;
58
+ /** Called when the focused item changes (null when none is visible). */
59
+ onFocusChange?: (item: T | null, index: number) => void;
60
+ /** Pause playback when the feed unmounts. Default `true`. */
61
+ pauseOnUnmount?: boolean;
62
+ }
63
+
64
+ /**
65
+ * A single-engine video feed: render many videos in a scrollable list, but
66
+ * only the one scrolled into focus plays — the rest are stopped. There is
67
+ * still exactly ONE native player; scrolling hands it off to the focused
68
+ * item's surface, so memory and CPU stay flat no matter how long the feed.
69
+ *
70
+ * Each item is a plain `VideoSurface` (never a `VideoPlayer`, which would
71
+ * fight for the engine on mount). Focus is driven centrally from FlatList
72
+ * viewability.
73
+ *
74
+ * ```tsx
75
+ * <VideoFeed
76
+ * data={videos} // [{ id, uri, title? }, …]
77
+ * renderOverlay={({ item, focused }) => <Caption title={item.title} />}
78
+ * />
79
+ * ```
80
+ */
81
+ export function VideoFeed<T extends VideoSource>({
82
+ data,
83
+ itemHeight,
84
+ resizeMode,
85
+ muted = true,
86
+ repeat = true,
87
+ visibilityThreshold = 80,
88
+ renderOverlay,
89
+ onFocusChange,
90
+ pauseOnUnmount = true,
91
+ ...rest
92
+ }: VideoFeedProps<T>) {
93
+ const manager = useVideoManager();
94
+ const { height: windowHeight } = useWindowDimensions();
95
+ const height = itemHeight ?? windowHeight;
96
+ const [focusedId, setFocusedId] = useState<string | null>(null);
97
+
98
+ // Global engine settings — apply to whatever is currently playing.
99
+ useEffect(() => {
100
+ if (muted) {
101
+ manager.mute();
102
+ } else {
103
+ manager.unmute();
104
+ }
105
+ }, [manager, muted]);
106
+
107
+ useEffect(() => {
108
+ manager.setRepeat(repeat);
109
+ }, [manager, repeat]);
110
+
111
+ useEffect(() => {
112
+ if (resizeMode) {
113
+ manager.setResizeMode(resizeMode);
114
+ }
115
+ }, [manager, resizeMode]);
116
+
117
+ useEffect(() => {
118
+ return () => {
119
+ if (pauseOnUnmount) {
120
+ manager.pause();
121
+ }
122
+ };
123
+ }, [manager, pauseOnUnmount]);
124
+
125
+ const focus = useCallback(
126
+ (item: T | null, index: number) => {
127
+ setFocusedId(item?.id ?? null);
128
+ if (item) {
129
+ // Same-id handoff aware: re-focusing the same video never reloads.
130
+ // A different id replaces the source, which stops the previous one —
131
+ // so only the focused video ever plays.
132
+ manager.setSource(item, {
133
+ autoplay: true,
134
+ surfaceId: feedSurfaceId(item.id),
135
+ });
136
+ }
137
+ onFocusChange?.(item, index);
138
+ },
139
+ [manager, onFocusChange]
140
+ );
141
+
142
+ // FlatList forbids changing these on the fly, so keep them stable and read
143
+ // the latest `focus`/`data` through a ref.
144
+ const latest = useRef({ focus, data });
145
+ latest.current = { focus, data };
146
+
147
+ const viewabilityConfig = useRef({
148
+ itemVisiblePercentThreshold: visibilityThreshold,
149
+ minimumViewTime: 100,
150
+ }).current;
151
+
152
+ const onViewableItemsChanged = useRef(
153
+ ({ viewableItems }: { viewableItems: FeedViewToken[] }) => {
154
+ const focusedToken = viewableItems.find((v) => v.isViewable);
155
+ if (focusedToken) {
156
+ latest.current.focus(focusedToken.item as T, focusedToken.index ?? 0);
157
+ }
158
+ }
159
+ ).current;
160
+
161
+ const renderItem = useCallback(
162
+ ({ item, index }: { item: T; index: number }) => (
163
+ <View style={[styles.item, { height }]}>
164
+ <VideoSurface
165
+ surfaceId={feedSurfaceId(item.id)}
166
+ style={StyleSheet.absoluteFill}
167
+ />
168
+ {renderOverlay?.({ item, index, focused: item.id === focusedId })}
169
+ </View>
170
+ ),
171
+ [height, renderOverlay, focusedId]
172
+ );
173
+
174
+ return (
175
+ <FlatList
176
+ data={data}
177
+ keyExtractor={(item) => item.id}
178
+ renderItem={renderItem}
179
+ onViewableItemsChanged={onViewableItemsChanged}
180
+ viewabilityConfig={viewabilityConfig}
181
+ getItemLayout={(_, index) => ({
182
+ length: height,
183
+ offset: height * index,
184
+ index,
185
+ })}
186
+ showsVerticalScrollIndicator={false}
187
+ snapToInterval={height}
188
+ snapToAlignment="start"
189
+ decelerationRate="fast"
190
+ {...rest}
191
+ />
192
+ );
193
+ }
194
+
195
+ const styles = StyleSheet.create({
196
+ item: {
197
+ width: '100%',
198
+ backgroundColor: '#000',
199
+ overflow: 'hidden',
200
+ },
201
+ });
@@ -1,7 +1,15 @@
1
- import { useEffect } from 'react';
2
- import { StyleSheet, View, type ViewProps } from 'react-native';
1
+ import { forwardRef, useEffect, useImperativeHandle } from 'react';
2
+ import { AppState, StyleSheet, View, type ViewProps } from 'react-native';
3
+ import { VideoManager } from '../core/VideoManager';
3
4
  import { useVideoManager } from '../provider/VideoContext';
4
- import type { OrientationLock, ResizeMode, VideoSource } from '../types/video';
5
+ import { useVideoEvents } from '../hooks/useVideoEvents';
6
+ import type { VideoEventMap } from '../types/events';
7
+ import type {
8
+ OrientationLock,
9
+ ResizeMode,
10
+ VideoError,
11
+ VideoSource,
12
+ } from '../types/video';
5
13
  import { VideoControls } from './VideoControls';
6
14
  import { VideoSurface } from './VideoSurface';
7
15
 
@@ -18,6 +26,10 @@ export interface VideoPlayerProps extends ViewProps {
18
26
  /** Show built-in controls. Default true. */
19
27
  controls?: boolean;
20
28
  resizeMode?: ResizeMode;
29
+ /** Loop playback when it ends. Default false. */
30
+ repeat?: boolean;
31
+ /** Default false. */
32
+ muted?: boolean;
21
33
  /**
22
34
  * Force the screen into this orientation while the player is mounted:
23
35
  * `'landscape'`, `'inverted-landscape'`, `'portrait'` or
@@ -31,6 +43,18 @@ export interface VideoPlayerProps extends ViewProps {
31
43
  * when it closes, so the rest of the app is unaffected.
32
44
  */
33
45
  fullscreenOrientation?: OrientationLock;
46
+ /**
47
+ * Pause this player when it loses focus — i.e. the app goes to the
48
+ * background while this player's surface is the one currently playing.
49
+ * Default `true`. Set `false` to keep playing (e.g. background audio).
50
+ * Only the focused player acts, so a video attached elsewhere is untouched.
51
+ */
52
+ pauseOnFocusLost?: boolean;
53
+ /** Fires once metadata (duration, dimensions) is available. */
54
+ onLoadComplete?: (info: VideoEventMap['onLoad']) => void;
55
+ /** Fires whenever buffering starts or stops. */
56
+ onBuffering?: (buffering: boolean) => void;
57
+ onError?: (error: VideoError) => void;
34
58
  }
35
59
 
36
60
  /**
@@ -41,57 +65,114 @@ export interface VideoPlayerProps extends ViewProps {
41
65
  * ```tsx
42
66
  * <VideoPlayer source={{ id: '123', uri }} style={{ aspectRatio: 16 / 9 }} />
43
67
  * ```
68
+ *
69
+ * `ref` exposes the underlying `VideoManager` for imperative control
70
+ * (`ref.current.play()`, `.seek()`, …) — the same instance `useVideo()`
71
+ * returns elsewhere in the app.
44
72
  */
45
- export function VideoPlayer({
46
- source,
47
- autoplay = true,
48
- surfaceId,
49
- controls = true,
50
- resizeMode,
51
- orientation,
52
- fullscreenOrientation,
53
- style,
54
- ...rest
55
- }: VideoPlayerProps) {
56
- const manager = useVideoManager();
57
- const id = surfaceId ?? `player:${source.id}`;
73
+ export const VideoPlayer = forwardRef<VideoManager, VideoPlayerProps>(
74
+ (
75
+ {
76
+ source,
77
+ autoplay = true,
78
+ surfaceId,
79
+ controls = true,
80
+ resizeMode,
81
+ repeat,
82
+ muted,
83
+ orientation,
84
+ fullscreenOrientation,
85
+ pauseOnFocusLost = true,
86
+ onLoadComplete,
87
+ onBuffering,
88
+ onError,
89
+ style,
90
+ ...rest
91
+ },
92
+ ref
93
+ ) => {
94
+ const manager = useVideoManager();
95
+ const id = surfaceId ?? `player:${source.id}`;
58
96
 
59
- useEffect(() => {
60
- manager.setSource(source, { autoplay, surfaceId: id });
61
- // Attach on mount / when the video identity changes. Other source
62
- // fields (title, headers) don't retrigger: identity is source.id.
63
- // eslint-disable-next-line react-hooks/exhaustive-deps
64
- }, [manager, source.id, id]);
97
+ useImperativeHandle(ref, () => manager, [manager]);
65
98
 
66
- useEffect(() => {
67
- if (resizeMode) {
68
- manager.setResizeMode(resizeMode);
69
- }
70
- }, [manager, resizeMode]);
99
+ useEffect(() => {
100
+ manager.setSource(source, { autoplay, surfaceId: id });
101
+ // Attach on mount / when the video identity changes. Other source
102
+ // fields (title, headers) don't retrigger: identity is source.id.
103
+ // eslint-disable-next-line react-hooks/exhaustive-deps
104
+ }, [manager, source.id, id]);
71
105
 
72
- useEffect(() => {
73
- if (!orientation || orientation === 'auto') {
74
- return;
75
- }
76
- manager.setOrientation(orientation);
77
- return () => manager.setOrientation('auto');
78
- }, [manager, orientation]);
106
+ useEffect(() => {
107
+ if (resizeMode) {
108
+ manager.setResizeMode(resizeMode);
109
+ }
110
+ }, [manager, resizeMode]);
79
111
 
80
- useEffect(() => {
81
- if (!fullscreenOrientation) {
82
- return;
83
- }
84
- manager.setFullscreenOrientation(fullscreenOrientation);
85
- return () => manager.setFullscreenOrientation(null);
86
- }, [manager, fullscreenOrientation]);
112
+ useEffect(() => {
113
+ if (repeat === undefined) {
114
+ return;
115
+ }
116
+ manager.setRepeat(repeat);
117
+ }, [manager, repeat]);
87
118
 
88
- return (
89
- <View style={[styles.container, style]} {...rest}>
90
- <VideoSurface surfaceId={id} style={styles.surface} />
91
- {controls ? <VideoControls /> : null}
92
- </View>
93
- );
94
- }
119
+ useEffect(() => {
120
+ if (muted === undefined) {
121
+ return;
122
+ }
123
+ if (muted) {
124
+ manager.mute();
125
+ } else {
126
+ manager.unmute();
127
+ }
128
+ }, [manager, muted]);
129
+
130
+ useEffect(() => {
131
+ if (!orientation || orientation === 'auto') {
132
+ return;
133
+ }
134
+ manager.setOrientation(orientation);
135
+ return () => manager.setOrientation('auto');
136
+ }, [manager, orientation]);
137
+
138
+ useEffect(() => {
139
+ if (!fullscreenOrientation) {
140
+ return;
141
+ }
142
+ manager.setFullscreenOrientation(fullscreenOrientation);
143
+ return () => manager.setFullscreenOrientation(null);
144
+ }, [manager, fullscreenOrientation]);
145
+
146
+ useEffect(() => {
147
+ if (!pauseOnFocusLost) {
148
+ return;
149
+ }
150
+ const sub = AppState.addEventListener('change', (next) => {
151
+ // Only the player currently owning the engine reacts, so a video
152
+ // attached to another surface keeps playing untouched.
153
+ if (next !== 'active' && manager.store.getState().surfaceId === id) {
154
+ manager.pause();
155
+ }
156
+ });
157
+ return () => sub.remove();
158
+ }, [manager, id, pauseOnFocusLost]);
159
+
160
+ useVideoEvents({
161
+ onLoad: onLoadComplete,
162
+ onBuffer: (e) => onBuffering?.(e.buffering),
163
+ onError,
164
+ });
165
+
166
+ return (
167
+ <View style={[styles.container, style]} {...rest}>
168
+ <VideoSurface surfaceId={id} style={styles.surface} />
169
+ {controls ? <VideoControls /> : null}
170
+ </View>
171
+ );
172
+ }
173
+ );
174
+
175
+ VideoPlayer.displayName = 'VideoPlayer';
95
176
 
96
177
  const styles = StyleSheet.create({
97
178
  container: {
package/src/index.ts CHANGED
@@ -14,6 +14,12 @@ export {
14
14
  type VideoSurfaceProps,
15
15
  } from './components/VideoSurface';
16
16
  export { VideoPlayer, type VideoPlayerProps } from './components/VideoPlayer';
17
+ export {
18
+ VideoFeed,
19
+ feedSurfaceId,
20
+ type VideoFeedProps,
21
+ type VideoFeedRenderInfo,
22
+ } from './components/VideoFeed';
17
23
  export { FullscreenPlayer } from './components/FullscreenPlayer';
18
24
  export {
19
25
  FloatingPlayer,