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
|
import React, { Component } from 'react';
import {
StyleSheet,
View
} from 'react-native';
import Container from '../components/container'
import ClearText from '../components/text'
import Button from '../components/button'
import Youtube from '../components/youtube'
export default class Livestream extends Component {
constructor(props) {
super()
this.state = {
ytid: getYTID(choice(props.content.streams).uri),
isReady: false,
currentTime: 0,
duration: 0,
quality: 0,
status: 'unloaded',
error: null,
}
}
render() {
const buttons = this.props.content.streams.map( (stream, i) => {
const ytid = getYTID(stream.uri || "")
if (! ytid) return null
let buttonStyle, textStyle
if (ytid === this.state.ytid) {
buttonStyle = styles.activeButton
textStyle = styles.activeButtonText
}
return (
<Button
key={'button_' + i}
buttonStyle={buttonStyle}
textStyle={textStyle}
label={stream.text}
onPress={() => {
this.setState({ ytid })
}}
/>
)
}).filter(button => !!button)
return (
<Container heading='Livestream' bodyStyle={styles.bodyStyle}>
<View style={styles.videoContainer} className='videoContainer'>
<Youtube ytid={this.state.ytid} />
<View style={styles.overlay}></View>
</View>
<View style={styles.buttons}>
{buttons}
</View>
<View style={styles.contentContainer}>
<ClearText style={styles.body}>{this.props.content.body}</ClearText>
</View>
</Container>
)
}
}
function randint(n){ return Math.floor(Math.random() * n) }
function choice(a){ return a[randint(a.length)] }
function getYTID(url) {
return (
url.match(/v=([-_a-zA-Z0-9]{11})/i)
|| url.match(/youtu.be\/([-_a-zA-Z0-9]{11})/i)
|| url.match(/embed\/([-_a-zA-Z0-9]{11})/i)
)[1].split('&')[0];
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center',
},
bodyStyle: {
flex: 1,
},
body: {
textAlign: 'left',
marginTop: 40,
width: '75%',
},
contentContainer: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center',
width: '100%',
},
videoContainer: {
justifyContent: 'flex-start',
height: 400,
width: '100%',
padding: 10,
},
video: {
alignSelf: 'stretch',
height: 400,
width: '100%',
backgroundColor: 'black',
marginVertical: 10
},
overlay: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,0)'
},
buttons: {
width: '100%',
paddingTop: 20,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
button: {
margin: 20,
},
activeButtonText: {
color: 'red',
},
activeButton: {
backgroundColor: '#bbb',
borderBottomColor: '#888',
},
})
|