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

Seekbar controls feature #72

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ These are the various controls that you can turn on/off as needed. All of these
| disableFullscreen | Hide the fullscreen button |
| disablePlayPause | Hide the play/pause toggle and the rewind/forward buttons |
| disableSeekButtons | Hide the rewind/forward buttons (but not play/pause) |
| disableSeekbar | Hide the seekbar |
| disableSeekBar | Hide the seekbar |
| disableVolume | Hide the Volume control |
| disableTimer | Hide the timer |
| disableBack | Hide the back button |
Expand Down
84 changes: 80 additions & 4 deletions src/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const VideoPlayer = (props: VideoPlayerProps) => {
disableVolume = false,
disableFullscreen = false,
disableTimer = false,
disableSeekbar = false,
disableSeekBar = false,
disablePlayPause = false,
disableSeekButtons = false,
navigator,
Expand Down Expand Up @@ -100,6 +100,11 @@ export const VideoPlayer = (props: VideoPlayerProps) => {
const [error, setError] = useState(false);
const [duration, setDuration] = useState(0);

// Seeking variables
const [pressCount, setPressCount] = useState(0);
const [timeoutId, setTimeoutId] = useState<NodeJS.Timeout | null>(null);
const [rewindPressCount, setRewindPressCount] = useState(0);

const videoRef = props.videoRef || _videoRef;

const toggleFullscreen = () => setIsFullscreen((prevState) => !prevState);
Expand Down Expand Up @@ -287,6 +292,71 @@ export const VideoPlayer = (props: VideoPlayerProps) => {
inverted: invertedPan,
});

const handleRewindPress = () => {
const x: NodeJS.Timeout = setTimeout(() => {
if (rewindPressCount === 3) {
setRewindPressCount(0);
} else {
let newCount = rewindPressCount + 1;
setRewindPressCount(newCount);
}
}, 500);

setTimeoutId(x);

return function () {
setRewindPressCount(0);
clearTimeout(timeoutId as unknown as number);
};
};

// I only have this working for fast forward right now
const handleFastForwardPress = () => {
const x: NodeJS.Timeout = setTimeout(() => {
if (pressCount === 3) {
setPressCount(0);
} else {
let newCount = pressCount + 1;
setPressCount(newCount);
}
}, 500);

setTimeoutId(x);

return function () {
setPressCount(0);
clearTimeout(timeoutId as unknown as number);
};
};

useEffect(() => {
console.log('props.disableSeekbar? ', props.disableSeekBar);
}, [disableSeekBar]);

useEffect(() => {
let skipTime = duration * 0.0013 * rewindPressCount;

if (currentTime < duration && rewindPressCount === 1) {
videoRef?.current?.seek(currentTime - rewindTime);
} else if (currentTime < duration && rewindPressCount > 1) {
videoRef?.current?.seek(currentTime - skipTime);
} else {
setPaused(false);
}
}, [rewindPressCount, currentTime, duration, videoRef]);

useEffect(() => {
let skipTime = duration * 0.0013 * pressCount;

if (currentTime < duration && pressCount === 1) {
videoRef?.current?.seek(currentTime + rewindTime);
} else if (currentTime < duration && pressCount > 1) {
videoRef?.current?.seek(currentTime + skipTime);
} else {
setPaused(false);
}
}, [pressCount, currentTime, duration, videoRef]);

useEffect(() => {
if (currentTime >= duration) {
videoRef?.current?.seek(0);
Expand Down Expand Up @@ -322,8 +392,12 @@ export const VideoPlayer = (props: VideoPlayerProps) => {

useEffect(() => {
if (_paused) {
setPressCount(0);
setRewindPressCount(0);
typeof events.onPause === 'function' && events.onPause();
} else {
setPressCount(0);
setRewindPressCount(0);
typeof events.onPlay === 'function' && events.onPlay();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -433,18 +507,20 @@ export const VideoPlayer = (props: VideoPlayerProps) => {
togglePlayPause={togglePlayPause}
resetControlTimeout={resetControlTimeout}
showControls={showControls}
onPressRewind={() =>
onPressSkipBackward={() =>
videoRef?.current?.seek(currentTime - rewindTime)
}
onPressForward={() =>
onPressRewind={() => handleRewindPress()}
onPressForward={() => handleFastForwardPress()}
onPressSkipForward={() =>
videoRef?.current?.seek(currentTime + rewindTime)
}
/>
<BottomControls
animations={animations}
panHandlers={seekPanResponder.panHandlers}
disableTimer={disableTimer}
disableSeekbar={disableSeekbar}
disableSeekBar={disableSeekBar}
showHours={showHours}
showDuration={showDuration}
paused={_paused}
Expand Down
Binary file added src/assets/img/left-seek-rewind-2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/left-seek-rewind-3x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/left-seek-rewind.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/right-seek-forward-2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/right-seek-forward-3x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/right-seek-forward.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/components/BottomControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface BottomControlsProps {
animations: VideoAnimations;
panHandlers: GestureResponderHandlers;
disableTimer: boolean;
disableSeekbar: boolean;
disableSeekBar: boolean;
showDuration: boolean;
showHours: boolean;
paused: boolean;
Expand All @@ -43,7 +43,7 @@ export const BottomControls = ({
showControls,
animations,
panHandlers,
disableSeekbar,
disableSeekBar,
disableTimer,
duration,
seekColor,
Expand Down Expand Up @@ -78,7 +78,7 @@ export const BottomControls = ({
</Timer>
);

const seekbarControl = disableSeekbar ? (
const seekbarControl = disableSeekBar ? (
<NullControl />
) : (
<Seekbar
Expand Down
22 changes: 22 additions & 0 deletions src/components/PlayPause/PlayPause.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ interface PlayPauseProps {
showControls: boolean;
onPressForward: () => void;
onPressRewind: () => void;
onPressSkipForward: () => void;
onPressSkipBackward: () => void;
}

const play = require('../../assets/img/play.png');
const pause = require('../../assets/img/pause.png');
const rewind = require('../../assets/img/rewind.png');
const forward = require('../../assets/img/forward.png');
const skipForward = require('../../assets/img/right-seek-forward-3x.png');
const skipBackward = require('../../assets/img/left-seek-rewind-3x.png');

export const PlayPause = ({
animations,
Expand All @@ -34,6 +38,8 @@ export const PlayPause = ({
showControls,
onPressForward,
onPressRewind,
onPressSkipForward,
onPressSkipBackward,
}: PlayPauseProps) => {
let source = paused ? play : pause;

Expand All @@ -58,6 +64,14 @@ export const PlayPause = ({
<Image source={rewind} resizeMode={'contain'} style={styles.rewind} />
</Control>
) : null}
{!disableSeekButtons ? (
<Control
disabled={!showControls}
callback={onPressSkipBackward}
resetControlTimeout={resetControlTimeout}>
<Image source={skipBackward} resizeMode={'contain'} style={styles.seek} />
</Control>
) : null}
<Control
disabled={!showControls}
callback={togglePlayPause}
Expand All @@ -67,6 +81,14 @@ export const PlayPause = ({
{...(Platform.isTV ? {hasTVPreferredFocus: showControls} : {})}>
<Image source={source} resizeMode={'contain'} style={styles.play} />
</Control>
{!disableSeekButtons ? (
<Control
disabled={!showControls}
callback={onPressSkipForward}
resetControlTimeout={resetControlTimeout}>
<Image source={skipForward} resizeMode={'contain'} style={styles.seek} />
</Control>
) : null}
{!disableSeekButtons ? (
<Control
disabled={!showControls}
Expand Down
9 changes: 8 additions & 1 deletion src/components/PlayPause/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@ export const styles = StyleSheet.create({
alignItems: 'center',
},
play: {},
rewind: {},
seek: {
height: 29,
width: 29,
},
rewind: {
height: 44,
width: 44,
},
});
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export interface VideoPlayerProps extends VideoProperties {
*
* @default false
*/
disableSeekbar?: boolean;
disableSeekBar?: boolean;

/**
* Hide the play/pause toggle and the rewind/forward buttons
Expand Down