Repository navigation
Expand file tree
/
Copy pathVideoViewer.js
More file actions
174 lines (161 loc) · 4.81 KB
/
Copy pathVideoViewer.js
File metadata and controls
174 lines (161 loc) · 4.81 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* 视频预览界面
*/
import * as React from 'react'
import { Image, View, TouchableOpacity } from 'react-native'
import Video from 'react-native-video'
import { getPublicAssets } from '../../assets'
import { Progress } from '../Progress'
import { VIDEO_STATUS } from './MediaConst'
import styles from './styles'
export default class VideoViewer extends React.Component {
props: {
uri: Object,
withBackBtn: boolean,
backAction: () => {},
}
static defaultProps = {
withBackBtn: false,
}
constructor(props) {
super(props)
this.state = {
videoPaused: true, // 视频是否暂停
videoControllerVisible: false, // 视频控制器是否显示
videoStatus: VIDEO_STATUS.STOP, // 视频状态
}
}
shouldComponentUpdate(nextProps, nextState) {
return (
JSON.stringify(this.props) !== JSON.stringify(nextProps) ||
JSON.stringify(this.state) !== JSON.stringify(nextState)
)
}
setVideoControllerVisible = (option = {}) => {
if (option.visible === undefined) {
option.visible = !this.state.videoControllerVisible
}
if (option.autoDismiss && option.visible) {
this.timer = setTimeout(() => {
this.setState({
videoControllerVisible: false,
})
clearTimeout(this.timer)
this.timer = null
}, 2000)
} else if (!option.visible) {
if (this.timer) {
clearTimeout(this.timer)
this.timer = null
}
}
if (option.visible !== this.state.videoControllerVisible) {
this.setState({
videoControllerVisible: option.visible,
})
}
}
/** 视频播放/暂停 **/
play = () => {
if (!this.props.uri || !this.player) return null
if (this.state.videoStatus === VIDEO_STATUS.STOP) {
this.player.seek(0, 0)
this.mProgress.progress = 0
}
let videoPaused = !this.state.videoPaused
this.setState({
videoPaused: videoPaused,
videoStatus: videoPaused ? VIDEO_STATUS.PAUSE : VIDEO_STATUS.PLAYING,
})
}
/** 视频暂停 **/
pause = () => {
if (!this.props.uri || !this.player) return null
this.setState({
videoPaused: true,
videoStatus: VIDEO_STATUS.PAUSE,
})
}
renderProgress = () => {
return (
<Progress
ref={ref => (this.mProgress = ref)}
style={styles.progressView}
progressAniDuration={0}
progressColor={'#rgba(123, 183, 54, 0.5)'}
/>
)
}
render() {
return (
<View style={styles.container}>
<Video
source={{ uri: this.props.uri }}
ref={ref => (this.player = ref)}
paused={this.state.videoPaused}
repeat={false}
style={styles.video}
onEnd={() => {
if (this.player) {
// this.player.seek(0, 0)
if (this.mProgress) {
this.mProgress.progress = 1
}
this.setState({
videoPaused: true,
videoStatus: VIDEO_STATUS.STOP,
})
}
}}
onProgress={({ currentTime, seekableDuration }) => {
// console.warn(currentTime, seekableDuration)
currentTime = currentTime.toFixed() - 1 + 1
seekableDuration = seekableDuration.toFixed() - 1 + 1
if (this.mProgress) {
this.mProgress.progress = currentTime / seekableDuration
}
}}
/>
{this.renderProgress()}
<View style={styles.videoControlView}>
<TouchableOpacity
onPress={() =>
this.setVideoControllerVisible({ autoDismiss: true })
}
style={styles.videoControlView}
/>
{(this.state.videoControllerVisible || this.state.videoPaused) && (
<TouchableOpacity onPress={() => this.play()} style={styles.play}>
<View style={styles.playOverlay} />
<Image
resizeMode={'contain'}
source={
this.state.videoPaused
? getPublicAssets().common.icon_play_white
: getPublicAssets().common.icon_pause_white
}
style={styles.smallIcon}
/>
</TouchableOpacity>
)}
{this.props.withBackBtn && (
<TouchableOpacity
onPress={() => {
if (typeof this.props.backAction === 'function') {
this.props.backAction()
}
}}
style={styles.backBtn}
>
<View style={styles.backBtnBg} />
<Image
style={styles.backIcon}
source={require('../../assets/public/Frenchgrey/icon-back-white.png')}
/>
</TouchableOpacity>
)}
</View>
</View>
)
}
}