Skip to content

Commit

Permalink
Added a working seekbar
Browse files Browse the repository at this point in the history
  • Loading branch information
cornedor committed Feb 14, 2017
1 parent 5d30290 commit e6fb149
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.0
- The seekBar is now seek-able.
- Added `seekBarKnob` to `customStyles` to style the seek bar knob.

## 0.2.0

- `resizeMode` prop added, now defaults to `contain`
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ All other props are passed to the react-native-video component.
- playIcon
- seekBar
- seekBarProgress
- seekBarKnob
- thumbnail
- playButton
- playArrow

## Future features

- [ ] Make seek bar seekable.
- [X] Make seek bar seekable.
- [x] Make player customizable.
- [ ] Add volume control
- [ ] Add fullscreen button
- [X] Add fullscreen button
- [ ] Add fullscreen button for android
- [ ] Add loader
84 changes: 84 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ const styles = StyleSheet.create({
seekBarProgress: {
backgroundColor: '#F00',
},
seekBarKnob: {
width: 20,
height: 20,
borderRadius: 10,
marginHorizontal: -10,
marginVertical: -8.5,
backgroundColor: '#F00',
transform: [{ scale: 0.8 }],
},
overlayButton: {
flex: 1,
},
Expand All @@ -69,8 +78,14 @@ export default class VideoPlayer extends Component {
isMuted: props.defaultMuted,
isControlsVisible: !props.hideControlsOnStart,
duration: 0,
isSeeking: false,
};

this.seekBarWidth = 200;
this.wasPlayingBeforeSeek = props.autoplay;
this.seekTouchStart = 0;
this.seekProgressStart = 0;

this.onLayout = this.onLayout.bind(this);
this.onStartPress = this.onStartPress.bind(this);
this.onProgress = this.onProgress.bind(this);
Expand All @@ -80,6 +95,10 @@ export default class VideoPlayer extends Component {
this.onMutePress = this.onMutePress.bind(this);
this.showControls = this.showControls.bind(this);
this.onToggleFullScreen = this.onToggleFullScreen.bind(this);
this.onSeekBarLayout = this.onSeekBarLayout.bind(this);
this.onSeekGrant = this.onSeekGrant.bind(this);
this.onSeekRelease = this.onSeekRelease.bind(this);
this.onSeek = this.onSeek.bind(this);
}

componentDidMount() {
Expand All @@ -105,6 +124,9 @@ export default class VideoPlayer extends Component {
}

onProgress(event) {
if (this.state.isSeeking) {
return;
}
if (this.props.onProgress) {
this.props.onProgress(event);
}
Expand Down Expand Up @@ -157,6 +179,49 @@ export default class VideoPlayer extends Component {
this.player.presentFullscreenPlayer();
}

onSeekBarLayout({ nativeEvent }) {
this.seekBarWidth = nativeEvent.layout.width;
}

onSeekStartResponder() {
return true;
}

onSeekMoveResponder() {
return true;
}

onSeekGrant(e) {
this.seekTouchStart = e.nativeEvent.pageX;
this.seekProgressStart = this.state.progress;
this.wasPlayingBeforeSeek = this.state.isPlaying;
this.setState({
isSeeking: true,
isPlaying: false,
});
}

onSeekRelease() {
this.setState({
isSeeking: false,
isPlaying: this.wasPlayingBeforeSeek,
});
this.showControls();
}

onSeek(e) {
const diff = e.nativeEvent.pageX - this.seekTouchStart;
const ratio = 100 / this.seekBarWidth;
const progress = this.seekProgressStart + ((ratio * diff) / 100);


this.setState({
progress,
});

this.player.seek(progress * this.state.duration);
}

getSizeStyles() {
const { videoWidth, videoHeight } = this.props;
const { width } = this.state;
Expand Down Expand Up @@ -225,6 +290,7 @@ export default class VideoPlayer extends Component {
},
customStyles.seekBar,
]}
onLayout={this.onSeekBarLayout}
>
<View
style={[
Expand All @@ -233,6 +299,22 @@ export default class VideoPlayer extends Component {
customStyles.seekBarProgress,
]}
/>
{ !fullWidth ? (
<View
style={[
styles.seekBarKnob,
customStyles.seekBarKnob,
this.state.isSeeking ? { transform: [{ scale: 1 }] } : {},
this.state.isSeeking ? customStyles.seekBarKnobSeeking : {},
]}
onStartShouldSetResponder={this.onSeekStartResponder}
onMoveShouldSetPanResponder={this.onSeekMoveResponder}
onResponderGrant={this.onSeekGrant}
onResponderMove={this.onSeek}
onResponderRelease={this.onSeekRelease}
onResponderTerminate={this.onSeekRelease}
/>
) : null }
<View style={{ flexGrow: 1 - this.state.progress }} />
</View>
);
Expand Down Expand Up @@ -366,6 +448,8 @@ VideoPlayer.propTypes = {
playIcon: Icon.propTypes.style,
seekBar: View.propTypes.style,
seekBarProgress: View.propTypes.style,
seekBarKnob: View.propTypes.style,
seekBarKnobSeeking: View.propTypes.style,
thumbnail: Image.propTypes.style,
playButton: TouchableOpacity.propTypes.style,
playArrow: Icon.propTypes.style,
Expand Down

0 comments on commit e6fb149

Please sign in to comment.