2016-11-09 12:31:42 -07:00
|
|
|
'use strict';
|
|
|
|
|
2023-11-22 07:03:57 -07:00
|
|
|
import React, {Component} from 'react';
|
2016-11-09 12:31:42 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
StyleSheet,
|
|
|
|
Text,
|
2023-11-22 07:03:57 -07:00
|
|
|
TextStyle,
|
2016-11-09 12:31:42 -07:00
|
|
|
TouchableOpacity,
|
|
|
|
View,
|
|
|
|
} from 'react-native';
|
|
|
|
|
2023-11-22 07:03:57 -07:00
|
|
|
import Video, {ResizeMode} from 'react-native-video';
|
2016-11-09 12:31:42 -07:00
|
|
|
|
|
|
|
class VideoPlayer extends Component {
|
2022-04-23 14:23:10 -06:00
|
|
|
constructor(props: any) {
|
2016-11-09 12:31:42 -07:00
|
|
|
super(props);
|
|
|
|
this.onLoad = this.onLoad.bind(this);
|
|
|
|
this.onProgress = this.onProgress.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
|
|
|
rate: 1,
|
|
|
|
volume: 1,
|
|
|
|
muted: false,
|
2023-11-22 07:03:57 -07:00
|
|
|
resizeMode: ResizeMode.CONTAIN,
|
2016-11-09 12:31:42 -07:00
|
|
|
duration: 0.0,
|
|
|
|
currentTime: 0.0,
|
2023-11-22 07:03:57 -07:00
|
|
|
paused: false,
|
2016-11-09 12:31:42 -07:00
|
|
|
};
|
|
|
|
|
2022-04-23 14:23:10 -06:00
|
|
|
onLoad(data: any) {
|
2016-11-09 12:31:42 -07:00
|
|
|
this.setState({duration: data.duration});
|
|
|
|
}
|
|
|
|
|
2022-04-23 14:23:10 -06:00
|
|
|
onProgress(data: any) {
|
2016-11-09 12:31:42 -07:00
|
|
|
this.setState({currentTime: data.currentTime});
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentTimePercentage() {
|
2022-04-23 14:23:10 -06:00
|
|
|
if (this.state.currentTime > 0 && this.state.duration !== 0) {
|
|
|
|
return this.state.currentTime / this.state.duration;
|
2016-11-09 12:31:42 -07:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 14:23:10 -06:00
|
|
|
renderRateControl(rate: number) {
|
2023-11-22 07:03:57 -07:00
|
|
|
const isSelected = this.state.rate === rate;
|
|
|
|
const style: TextStyle = StyleSheet.flatten([
|
|
|
|
styles.controlOption,
|
|
|
|
{fontWeight: isSelected ? 'bold' : 'normal'},
|
|
|
|
]);
|
2016-11-09 12:31:42 -07:00
|
|
|
|
|
|
|
return (
|
2023-11-22 07:03:57 -07:00
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => {
|
|
|
|
this.setState({rate: rate});
|
|
|
|
}}>
|
|
|
|
<Text style={style}>{rate}x</Text>
|
2016-11-09 12:31:42 -07:00
|
|
|
</TouchableOpacity>
|
2023-11-22 07:03:57 -07:00
|
|
|
);
|
2016-11-09 12:31:42 -07:00
|
|
|
}
|
|
|
|
|
2022-04-23 14:23:10 -06:00
|
|
|
renderResizeModeControl(resizeMode: string) {
|
2023-11-22 07:03:57 -07:00
|
|
|
const isSelected = this.state.resizeMode === resizeMode;
|
|
|
|
const style: TextStyle = StyleSheet.flatten([
|
|
|
|
styles.controlOption,
|
|
|
|
{fontWeight: isSelected ? 'bold' : 'normal'},
|
|
|
|
]);
|
2016-11-09 12:31:42 -07:00
|
|
|
|
|
|
|
return (
|
2023-11-22 07:03:57 -07:00
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => {
|
|
|
|
this.setState({resizeMode: resizeMode});
|
|
|
|
}}>
|
|
|
|
<Text style={style}>{resizeMode}</Text>
|
2016-11-09 12:31:42 -07:00
|
|
|
</TouchableOpacity>
|
2023-11-22 07:03:57 -07:00
|
|
|
);
|
2016-11-09 12:31:42 -07:00
|
|
|
}
|
|
|
|
|
2022-04-23 14:23:10 -06:00
|
|
|
renderVolumeControl(volume: number) {
|
2023-11-22 07:03:57 -07:00
|
|
|
const isSelected = this.state.volume === volume;
|
|
|
|
const style: TextStyle = StyleSheet.flatten([
|
|
|
|
styles.controlOption,
|
|
|
|
{fontWeight: isSelected ? 'bold' : 'normal'},
|
|
|
|
]);
|
2016-11-09 12:31:42 -07:00
|
|
|
|
|
|
|
return (
|
2023-11-22 07:03:57 -07:00
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => {
|
|
|
|
this.setState({volume: volume});
|
|
|
|
}}>
|
|
|
|
<Text style={style}>{volume * 100}%</Text>
|
2016-11-09 12:31:42 -07:00
|
|
|
</TouchableOpacity>
|
2023-11-22 07:03:57 -07:00
|
|
|
);
|
2016-11-09 12:31:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const flexCompleted = this.getCurrentTimePercentage() * 100;
|
|
|
|
const flexRemaining = (1 - this.getCurrentTimePercentage()) * 100;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
2023-11-22 07:03:57 -07:00
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.fullScreen}
|
|
|
|
onPress={() => {
|
|
|
|
this.setState({paused: !this.state.paused});
|
|
|
|
}}>
|
|
|
|
<Video
|
2024-06-28 03:33:10 -06:00
|
|
|
source={require('./assets/videos/broadchurch.mp4')}
|
2023-11-22 07:03:57 -07:00
|
|
|
style={styles.fullScreen}
|
|
|
|
rate={this.state.rate}
|
|
|
|
paused={this.state.paused}
|
|
|
|
volume={this.state.volume}
|
|
|
|
muted={this.state.muted}
|
|
|
|
resizeMode={this.state.resizeMode}
|
|
|
|
onLoad={this.onLoad}
|
|
|
|
onProgress={this.onProgress}
|
|
|
|
onEnd={() => {
|
|
|
|
console.log('Done!');
|
|
|
|
}}
|
|
|
|
repeat={true}
|
|
|
|
/>
|
2016-11-09 12:31:42 -07:00
|
|
|
</TouchableOpacity>
|
|
|
|
|
|
|
|
<View style={styles.controls}>
|
|
|
|
<View style={styles.generalControls}>
|
|
|
|
<View style={styles.rateControl}>
|
|
|
|
{this.renderRateControl(0.25)}
|
|
|
|
{this.renderRateControl(0.5)}
|
|
|
|
{this.renderRateControl(1.0)}
|
|
|
|
{this.renderRateControl(1.5)}
|
|
|
|
{this.renderRateControl(2.0)}
|
|
|
|
</View>
|
|
|
|
|
|
|
|
<View style={styles.volumeControl}>
|
|
|
|
{this.renderVolumeControl(0.5)}
|
|
|
|
{this.renderVolumeControl(1)}
|
|
|
|
{this.renderVolumeControl(1.5)}
|
|
|
|
</View>
|
|
|
|
|
|
|
|
<View style={styles.resizeModeControl}>
|
|
|
|
{this.renderResizeModeControl('cover')}
|
|
|
|
{this.renderResizeModeControl('contain')}
|
|
|
|
{this.renderResizeModeControl('stretch')}
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
|
|
|
|
<View style={styles.trackingControls}>
|
|
|
|
<View style={styles.progress}>
|
2023-11-22 07:03:57 -07:00
|
|
|
<View
|
|
|
|
style={[styles.innerProgressCompleted, {flex: flexCompleted}]}
|
|
|
|
/>
|
|
|
|
<View
|
|
|
|
style={[styles.innerProgressRemaining, {flex: flexRemaining}]}
|
|
|
|
/>
|
2016-11-09 12:31:42 -07:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
backgroundColor: 'black',
|
|
|
|
},
|
|
|
|
fullScreen: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
bottom: 0,
|
|
|
|
right: 0,
|
|
|
|
},
|
|
|
|
controls: {
|
2023-11-22 07:03:57 -07:00
|
|
|
backgroundColor: 'transparent',
|
2016-11-09 12:31:42 -07:00
|
|
|
borderRadius: 5,
|
|
|
|
position: 'absolute',
|
|
|
|
bottom: 20,
|
|
|
|
left: 20,
|
|
|
|
right: 20,
|
|
|
|
},
|
|
|
|
progress: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
borderRadius: 3,
|
|
|
|
overflow: 'hidden',
|
|
|
|
},
|
|
|
|
innerProgressCompleted: {
|
|
|
|
height: 20,
|
|
|
|
backgroundColor: '#cccccc',
|
|
|
|
},
|
|
|
|
innerProgressRemaining: {
|
|
|
|
height: 20,
|
|
|
|
backgroundColor: '#2C2C2C',
|
|
|
|
},
|
|
|
|
generalControls: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
borderRadius: 4,
|
|
|
|
overflow: 'hidden',
|
|
|
|
paddingBottom: 10,
|
|
|
|
},
|
|
|
|
rateControl: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'center',
|
|
|
|
},
|
|
|
|
volumeControl: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'center',
|
|
|
|
},
|
|
|
|
resizeModeControl: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
},
|
|
|
|
controlOption: {
|
|
|
|
alignSelf: 'center',
|
|
|
|
fontSize: 11,
|
2023-11-22 07:03:57 -07:00
|
|
|
color: 'white',
|
2016-11-09 12:31:42 -07:00
|
|
|
paddingLeft: 2,
|
|
|
|
paddingRight: 2,
|
|
|
|
lineHeight: 12,
|
|
|
|
},
|
2022-04-23 14:23:10 -06:00
|
|
|
trackingControls: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
},
|
2016-11-09 12:31:42 -07:00
|
|
|
});
|
2023-11-22 07:03:57 -07:00
|
|
|
export default VideoPlayer;
|