Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to define poster props as Image type and render poster as custom component #3972

Merged
merged 15 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions docs/pages/component/props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,26 @@ Determine whether the media should continue playing when notifications or the Co
### `poster`

<PlatformsList types={['All']} />
> [!WARNING]
> Value: string with a URL for the poster is deprecated, use `poster` as object instead

An image to display while the video is loading

Value: string with a URL for the poster, e.g. "https://baconmockup.com/300/200/"
Value: Props for the `Image` component. The poster is visible when the source attribute is provided.

### `posterResizeMode`
```javascript
<Video>
poster={{
source: { uri: "https://baconmockup.com/300/200/" },
resizeMode: "cover",
// ...
}}
</Video>
````

### `posterResizeMode`
> [!WARNING]
> deprecated, use `poster` with `resizeMode` key instead
<PlatformsList types={['All']} />

Determines how to resize the poster image when the frame doesn't match the raw video dimensions.
Expand Down Expand Up @@ -486,6 +499,22 @@ Speed at which the media should play.
- **1.0** - Play at normal speed (default)
- **Other values** - Slow down or speed up playback

### `renderLoader`

<PlatformsList types={['All']} />

Allows you to create custom components to display while the video is loading. If both `renderLoader` and `poster` are provided, `poster` will be ignored.
moskalakamil marked this conversation as resolved.
Show resolved Hide resolved

```javascript
<Video>
renderLoader={
<View>
<Text>Custom Loader</Text>
</View>
}
</Video>
````

### `repeat`

<PlatformsList types={['All']} />
Expand Down
10 changes: 5 additions & 5 deletions examples/basic/src/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Video, {
import styles from './styles';
import {AdditionalSourceInfo} from './types';
import {bufferConfig, srcList, textTracksSelectionBy} from './constants';
import {Overlay, toast} from './components';
import {Overlay, toast, VideoLoader} from './components';

type Props = NonNullable<unknown>;

Expand Down Expand Up @@ -66,7 +66,7 @@ const VideoPlayer: FC<Props> = ({}) => {
const [repeat, setRepeat] = useState(false);
const [controls, setControls] = useState(false);
const [useCache, setUseCache] = useState(false);
const [poster, setPoster] = useState<string | undefined>(undefined);
const [showPoster, setShowPoster] = useState<boolean>(false);
const [showNotificationControls, setShowNotificationControls] =
useState(false);
const [isSeeking, setIsSeeking] = useState(false);
Expand Down Expand Up @@ -256,7 +256,7 @@ const VideoPlayer: FC<Props> = ({}) => {
cacheSizeMB: useCache ? 200 : 0,
}}
preventsDisplaySleepDuringVideoPlayback={true}
poster={poster}
renderLoader={showPoster ? <VideoLoader /> : undefined}
onPlaybackRateChange={onPlaybackRateChange}
onPlaybackStateChanged={onPlaybackStateChanged}
bufferingStrategy={BufferingStrategyType.DEFAULT}
Expand Down Expand Up @@ -288,7 +288,7 @@ const VideoPlayer: FC<Props> = ({}) => {
paused={paused}
volume={volume}
setControls={setControls}
poster={poster}
showPoster={showPoster}
setDecoration={setDecoration}
rate={rate}
setFullscreen={setFullscreen}
Expand All @@ -298,7 +298,7 @@ const VideoPlayer: FC<Props> = ({}) => {
setIsSeeking={setIsSeeking}
repeat={repeat}
setRepeat={setRepeat}
setPoster={setPoster}
setShowPoster={setShowPoster}
setRate={setRate}
setResizeMode={setResizeMode}
setShowNotificationControls={setShowNotificationControls}
Expand Down
22 changes: 4 additions & 18 deletions examples/basic/src/components/Indicator.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import React, {FC, memo} from 'react';
import {ActivityIndicator, View} from 'react-native';
import styles from '../styles.tsx';
import React, {memo} from 'react';
import {ActivityIndicator} from 'react-native';

type Props = {
isLoading: boolean;
};

const _Indicator: FC<Props> = ({isLoading}) => {
if (!isLoading) {
return <View />;
}
return (
<ActivityIndicator
color="#3235fd"
size="large"
style={styles.IndicatorStyle}
/>
);
const _Indicator = () => {
return <ActivityIndicator color="#3235fd" size="large" />;
};

export const Indicator = memo(_Indicator);
21 changes: 7 additions & 14 deletions examples/basic/src/components/Overlay.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import React, {forwardRef, memo, useCallback} from 'react';
import {Indicator} from './Indicator.tsx';
import {View} from 'react-native';
import styles from '../styles.tsx';
import ToggleControl from '../ToggleControl.tsx';
import {
isAndroid,
isIos,
samplePoster,
textTracksSelectionBy,
} from '../constants';
import {isAndroid, isIos, textTracksSelectionBy} from '../constants';
import MultiValueControl, {
MultiValueControlPropType,
} from '../MultiValueControl.tsx';
Expand Down Expand Up @@ -65,8 +59,8 @@ type Props = {
setPaused: (value: boolean) => void;
repeat: boolean;
setRepeat: (value: boolean) => void;
poster: string | undefined;
setPoster: (value: string | undefined) => void;
showPoster: boolean;
setShowPoster: (value: boolean) => void;
muted: boolean;
setMuted: (value: boolean) => void;
currentTime: number;
Expand Down Expand Up @@ -107,8 +101,8 @@ const _Overlay = forwardRef<VideoRef, Props>((props, ref) => {
setPaused,
setRepeat,
repeat,
setPoster,
poster,
setShowPoster,
showPoster,
setMuted,
muted,
duration,
Expand Down Expand Up @@ -227,13 +221,12 @@ const _Overlay = forwardRef<VideoRef, Props>((props, ref) => {

const toggleRepeat = () => setRepeat(!repeat);

const togglePoster = () => setPoster(poster ? undefined : samplePoster);
const togglePoster = () => setShowPoster(!showPoster);

const toggleMuted = () => setMuted(!muted);

return (
<>
<Indicator isLoading={isLoading} />
<View style={styles.topControls}>
<View style={styles.resizeModeControl}>
<TopControl
Expand Down Expand Up @@ -279,7 +272,7 @@ const _Overlay = forwardRef<VideoRef, Props>((props, ref) => {
<ToggleControl onPress={toggleFullscreen} text="fullscreen" />
<ToggleControl onPress={toggleDecoration} text="decoration" />
<ToggleControl
isSelected={!!poster}
isSelected={!!showPoster}
onPress={togglePoster}
selectedText="poster"
unselectedText="no poster"
Expand Down
15 changes: 15 additions & 0 deletions examples/basic/src/components/VideoLoader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Text, View} from 'react-native';
import {Indicator} from './Indicator.tsx';
import React, {memo} from 'react';
import styles from '../styles.tsx';

const _VideoLoader = () => {
return (
<View style={styles.indicatorContainer}>
<Text style={styles.indicatorText}>Loading...</Text>
<Indicator />
</View>
);
};

export const VideoLoader = memo(_VideoLoader);
1 change: 1 addition & 0 deletions examples/basic/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './VideoLoader';
export * from './Indicator';
export * from './Seeker';
export * from './AudioTracksSelector';
Expand Down
4 changes: 0 additions & 4 deletions examples/basic/src/constants/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ export const srcAndroidList = [
},
];

// poster which can be displayed
export const samplePoster =
'https://upload.wikimedia.org/wikipedia/commons/1/18/React_Native_Logo.png';

export const srcList: SampleVideoSource[] = srcAllPlatformList.concat(
isAndroid ? srcAndroidList : srcIosList,
);
Expand Down
10 changes: 8 additions & 2 deletions examples/basic/src/styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@ const styles = StyleSheet.create({
borderWidth: 1,
borderColor: 'red',
},
IndicatorStyle: {
flex: 1,
indicatorContainer: {
justifyContent: 'center',
alignItems: 'center',
gap: 10,
width: '100%',
height: '100%',
},
indicatorText: {
color: 'white',
},
seekbarContainer: {
flex: 1,
Expand Down
102 changes: 83 additions & 19 deletions src/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
type StyleProp,
type ImageStyle,
type NativeSyntheticEvent,
type ImageResizeMode,
type ViewStyle,
} from 'react-native';

import NativeVideoComponent, {
Expand Down Expand Up @@ -77,8 +79,9 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
source,
style,
resizeMode,
posterResizeMode,
poster,
posterResizeMode,
renderLoader,
fullscreen,
drm,
textTracks,
Expand Down Expand Up @@ -123,26 +126,28 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
ref,
) => {
const nativeRef = useRef<ComponentRef<VideoComponentType>>(null);
const [showPoster, setShowPoster] = useState(!!poster);

const isPosterDeprecated = typeof poster === 'string';

const hasPoster = useMemo(() => {
if (renderLoader) {
return true;
}

if (isPosterDeprecated) {
return !!poster;
}

return !!poster?.source;
}, [isPosterDeprecated, poster, renderLoader]);

const [showPoster, setShowPoster] = useState(hasPoster);
const [isFullscreen, setIsFullscreen] = useState(fullscreen);
const [
_restoreUserInterfaceForPIPStopCompletionHandler,
setRestoreUserInterfaceForPIPStopCompletionHandler,
] = useState<boolean | undefined>();

const hasPoster = !!poster;

const posterStyle = useMemo<StyleProp<ImageStyle>>(
() => ({
...StyleSheet.absoluteFillObject,
resizeMode:
posterResizeMode && posterResizeMode !== 'none'
? posterResizeMode
: 'contain',
}),
[posterResizeMode],
);

const src = useMemo<VideoSrc | undefined>(() => {
if (!source) {
return undefined;
Expand Down Expand Up @@ -606,14 +611,75 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
: ViewType.SURFACE;
}, [drm, useSecureView, useTextureView, viewType]);

const _renderPoster = useCallback(() => {
// poster resize mode
let _posterResizeMode: ImageResizeMode = 'contain';

if (!isPosterDeprecated && poster?.resizeMode) {
_posterResizeMode = poster.resizeMode;
} else if (posterResizeMode && posterResizeMode !== 'none') {
_posterResizeMode = posterResizeMode;
}

// poster style
const baseStyle: StyleProp<ImageStyle> = {
...StyleSheet.absoluteFillObject,
resizeMode: _posterResizeMode,
};

let posterStyle: StyleProp<ImageStyle> = baseStyle;

if (!isPosterDeprecated && poster?.style) {
const styles = Array.isArray(poster.style)
? poster.style
: [poster.style];
posterStyle = [baseStyle, ...styles];
}

// render poster
if (renderLoader && poster) {
console.warn(
'You provided both `renderLoader` and `poster` props. `Poster` props is ignored.',
);
}

if (renderLoader) {
// check if valid jsx
if (React.isValidElement(renderLoader)) {
console.warn(
'Invalid renderLoader component. Please provide a valid JSX component',
);
return;
}

return <View style={StyleSheet.absoluteFill}>{renderLoader}</View>;
}

return (
<Image
{...(isPosterDeprecated ? {} : poster)}
source={isPosterDeprecated ? {uri: poster} : poster?.source}
style={posterStyle}
/>
);
}, [isPosterDeprecated, poster, posterResizeMode, renderLoader]);

const _style: StyleProp<ViewStyle> = useMemo(
() => ({
...StyleSheet.absoluteFillObject,
...(showPoster ? {display: 'none'} : {}),
}),
[showPoster],
);

return (
<View style={style}>
<NativeVideoComponent
ref={nativeRef}
{...rest}
src={src}
drm={_drm}
style={StyleSheet.absoluteFill}
style={_style}
moskalakamil marked this conversation as resolved.
Show resolved Hide resolved
resizeMode={resizeMode}
fullscreen={isFullscreen}
restoreUserInterfaceForPIPStopCompletionHandler={
Expand Down Expand Up @@ -688,9 +754,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
}
viewType={_viewType}
/>
{hasPoster && showPoster ? (
<Image style={posterStyle} source={{uri: poster}} />
) : null}
{hasPoster && showPoster ? _renderPoster() : null}
moskalakamil marked this conversation as resolved.
Show resolved Hide resolved
</View>
);
},
Expand Down
Loading
Loading