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

control rate (playback speed) #229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ In addition, the `<VideoPlayer />` also takes these props:
| seekColor | String(#HEX) | '#FFF' | Fill/handle colour of the seekbar |
| style | StyleSheet | null | React Native StyleSheet object that is appended to the video's parent `<View>` |
| tapAnywhereToPause | Boolean | false | If true, single tapping anywhere on the video (other than a control) toggles between playing and paused. |
| controlRate | Boolean | false | If true, show the control Rate button \[x1\] (Playback Speed). |

| showTimeRemaining | Boolean | true | If true, show the time remaing, else show the current time in the Player.
`<View>`
Expand Down
52 changes: 44 additions & 8 deletions VideoPlayer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {Component} from 'react';
import React, { Component } from 'react';
import Video from 'react-native-video';
import {
TouchableWithoutFeedback,
Expand Down Expand Up @@ -33,6 +33,7 @@ export default class VideoPlayer extends Component {
rate: 1,
showTimeRemaining: true,
showHours: false,
controlRate: false,
};

constructor(props) {
Expand Down Expand Up @@ -110,6 +111,7 @@ export default class VideoPlayer extends Component {
togglePlayPause: this._togglePlayPause.bind(this),
toggleControls: this._toggleControls.bind(this),
toggleTimer: this._toggleTimer.bind(this),
toggleRate: this._toggleRate.bind(this),
};

/**
Expand Down Expand Up @@ -162,7 +164,7 @@ export default class VideoPlayer extends Component {
}

componentDidUpdate = prevProps => {
const {isFullscreen} = this.props;
const { isFullscreen } = this.props;

if (prevProps.isFullscreen !== isFullscreen) {
this.setState({
Expand Down Expand Up @@ -271,7 +273,7 @@ export default class VideoPlayer extends Component {
* Either close the video or go to a
* new page.
*/
_onEnd() {}
_onEnd() { }

/**
* Set the error state to true which then
Expand Down Expand Up @@ -516,6 +518,16 @@ export default class VideoPlayer extends Component {
this.setState(state);
}

/**
* Toggle playback speed state (rate) on <Video> component
*/
_toggleRate() {
let state = this.state;
let rates = [0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2];
state.rate = rates[(1 + rates.indexOf(state.rate)) % 8];
this.setState(state);
}

/**
* Toggle between showing time remaining or
* video duration in the timer control
Expand Down Expand Up @@ -958,6 +970,9 @@ export default class VideoPlayer extends Component {
const fullscreenControl = this.props.disableFullscreen
? this.renderNullControl()
: this.renderFullscreen();
const RateControl = this.props.controlRate
? this.renderRate()
: this.renderNullControl();

return (
<Animated.View
Expand All @@ -975,6 +990,7 @@ export default class VideoPlayer extends Component {
<SafeAreaView style={styles.controls.topControlGroup}>
{backControl}
<View style={styles.controls.pullRight}>
{RateControl}
{volumeControl}
{fullscreenControl}
</View>
Expand Down Expand Up @@ -1005,13 +1021,13 @@ export default class VideoPlayer extends Component {
return (
<View style={styles.volume.container}>
<View
style={[styles.volume.fill, {width: this.state.volumeFillWidth}]}
style={[styles.volume.fill, { width: this.state.volumeFillWidth }]}
/>
<View
style={[styles.volume.track, {width: this.state.volumeTrackWidth}]}
style={[styles.volume.track, { width: this.state.volumeTrackWidth }]}
/>
<View
style={[styles.volume.handle, {left: this.state.volumePosition}]}
style={[styles.volume.handle, { left: this.state.volumePosition }]}
{...this.player.volumePanResponder.panHandlers}>
<Image
style={styles.volume.icon}
Expand Down Expand Up @@ -1103,12 +1119,12 @@ export default class VideoPlayer extends Component {
/>
</View>
<View
style={[styles.seekbar.handle, {left: this.state.seekerPosition}]}
style={[styles.seekbar.handle, { left: this.state.seekerPosition }]}
pointerEvents={'none'}>
<View
style={[
styles.seekbar.circle,
{backgroundColor: this.props.seekColor || '#FFF'},
{ backgroundColor: this.props.seekColor || '#FFF' },
]}
pointerEvents={'none'}
/>
Expand Down Expand Up @@ -1191,6 +1207,17 @@ export default class VideoPlayer extends Component {
return null;
}

/**
* Show playback speed (rate) icon
*/
renderRate() {
return this.renderControl(
<Text style={styles.controls.rateText}>x{this.state.rate}</Text>,
this.methods.toggleRate,
styles.controls.rate,
);
}

renderError() {
if (this.state.error) {
return (
Expand Down Expand Up @@ -1377,12 +1404,21 @@ const styles = {
timer: {
width: 80,
},
rate: {
width: 80,
},
timerText: {
backgroundColor: 'transparent',
color: '#FFF',
fontSize: 11,
textAlign: 'right',
},
rateText: {
backgroundColor: 'transparent',
color: '#FFF',
fontSize: 11,
textAlign: 'right',
},
}),
volume: StyleSheet.create({
container: {
Expand Down