-
Notifications
You must be signed in to change notification settings - Fork 4
/
VideoPlayback.js
135 lines (119 loc) · 3.75 KB
/
VideoPlayback.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, {Component,PropTypes} from 'react';
import {
AppRegistry,
StyleSheet,
View,
TouchableHighlight,
Image,
Dimensions
} from 'react-native';
var Platform = require('react-native').Platform;
var Video = require('react-native-video').default;
var _video = Video;
var StartTimeNeed = 0.0;
var EndTimeNeed = 0.0;
var durationOfVideo = 0.0
var prevStartValue = 0.0
var SCREEN_HEIGH = Dimensions.get('window').height;
var SCREEN_WIDTH = Dimensions.get('window').width;
const styles = StyleSheet.create({
videoPlaybackContainer:{
flexDirection:'column',
justifyContent:'center',
alignItems:'center'
},
nativeVideoControls: {
left:0,
right:0,
width:SCREEN_WIDTH,
height: SCREEN_HEIGH*0.5
},
playPauseButton:{
position:'absolute',
alignSelf:'center',
width:50,
top:-SCREEN_HEIGH*0.25,
height:50
}
});
export default class VideoPlayback extends Component {
static propTypes = {
startTime:PropTypes.number.isRequired,
endTime:PropTypes.number.isRequired
}
componentWillReceiveProps(){
StartTimeNeed = this.props.startTime;
EndTimeNeed = this.props.endTime;
}
constructor(props, context, ...args) {
super(props, context, ...args);
this.setDuration = this.setDuration.bind(this.props.delegateForVideoPlayback);
this.setTime = this.setTime.bind(this.props.delegateForVideoPlayback);
}
state = {
duration: 0.0,
currentTime: 0.0,
isPaused:false
};
setDuration(data) {
durationOfVideo = data.duration;
this.setState({duration: data.duration});
}
setTime(data) {
if (data.currentTime>EndTimeNeed || prevStartValue != StartTimeNeed) {
_video.seek(StartTimeNeed);
}
prevStartValue = StartTimeNeed;
this.setState({currentTime: data.currentTime});
console.log('on set time',data.currentTime,StartTimeNeed,EndTimeNeed);
}
renderButton() {
return (
<TouchableHighlight onPress={this.onPressButton.bind(this)}>
<Image
style={styles.playPauseButton}
source={this.state.isPaused ? require('./videoPlayButton.png') : require('./videoPauseButton.png') }
/>
</TouchableHighlight>
);
}
onPressButton(){
this.setState({
isPaused:!this.state.isPaused
});
}
render() {
return( <View style={styles.videoPlaybackContainer}>
{this.renderVideoPlayer()}
{durationOfVideo != 0.0 ? this.renderButton() : this.nothing()}
</View>);
}
nothing(){
return (<View />);
}
renderVideoPlayer(){
return (
<Video
ref = {(vid)=>{
_video = vid;
}}
source={{uri: this.props.videoURLForPlayback}} // Can be a URL or a local file.
rate={1.0} // 0 is paused, 1 is normal.
volume={1.0} // 0 is muted, 1 is normal.
muted={false} // Mutes the audio entirely.
paused={this.state.isPaused} // Pauses playback entirely.
repeat={true}
controls={true}
playInBackground={false} // Audio continues to play when aentering background.
playWhenInactive={false} // [iOS] Video continues to play whcontrol or notification center are shown.
resizeMode="cover" // Fill the whole screen at aspect ratio.
onLoadStart={this.loadStart} // Callback when video starts to load
onLoad={this.setDuration} // Callback when video loads
onProgress={this.setTime} // Callback every ~250ms with currentTime
onEnd={this.onEnd} // Callback when playback finishes
onError={this.videoError} // Callback when video cannot be loaded
key={this.props.randkey}
style={styles.nativeVideoControls}/>
);
}
}