-
Notifications
You must be signed in to change notification settings - Fork 255
/
main.js
executable file
·172 lines (152 loc) · 4.42 KB
/
main.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
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
/* @flow */
import React from 'react'
import {
Button,
FlatList,
Image,
StyleSheet,
Text,
View,
Platform,
ToolbarAndroid,
TouchableOpacity,
} from 'react-native'
import styles from './main.style'
import GoogleCast, { CastButton } from 'react-native-google-cast'
class Main extends React.Component {
constructor(props) {
super(props)
this.cast = this.cast.bind(this)
this.renderVideo = this.renderVideo.bind(this)
this.subtitlesEnabled = false
this.state = {
videos: [],
}
}
componentDidMount() {
this.registerListeners()
GoogleCast.getCastState().then(console.log)
// GoogleCast.showIntroductoryOverlay();
const CAST_VIDEOS_URL =
'https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/f.json'
fetch(CAST_VIDEOS_URL)
.then(response => response.json())
.then(data => {
const mp4Url = data.categories[0].mp4
const imagesUrl = data.categories[0].images
this.setState({
videos: data.categories[0].videos.map(video => ({
title: video.title,
subtitle: video.subtitle,
studio: video.studio,
duration: video.duration,
mediaUrl: mp4Url + video.sources[2].url,
imageUrl: imagesUrl + video['image-480x270'],
posterUrl: imagesUrl + video['image-780x1200'],
})),
})
})
.catch(console.error)
}
cast(video) {
GoogleCast.getCastDevice().then(console.log)
GoogleCast.castMedia(video)
GoogleCast.launchExpandedControls()
this.sendMessage()
}
onActionSelected = position => {
switch (position) {
case 0:
GoogleCast.play()
break
case 1:
GoogleCast.pause()
break
case 2:
GoogleCast.stop()
break
case 3:
this.subtitlesEnabled = !this.subtitlesEnabled
GoogleCast.toggleSubtitles(this.subtitlesEnabled)
break
}
}
render() {
return (
<View style={styles.container}>
{Platform.OS === 'android' ? (
<ToolbarAndroid
contentInsetStart={4}
actions={[
{ title: 'Play', show: 'always' },
{ title: 'Pause', show: 'always' },
{ title: 'Stop', show: 'always' },
{ title: 'CC (en)', show: 'always' },
]}
onActionSelected={this.onActionSelected}
style={styles.toolbarAndroid}
>
<CastButton style={styles.castButtonAndroid} />
</ToolbarAndroid>
) : (
<View style={styles.toolbarIOS}>
<Button
title="Stop"
onPress={() => GoogleCast.endSession()}
style={styles.stopButton}
/>
<CastButton style={styles.castButtonIOS} />
</View>
)}
<FlatList
data={this.state.videos}
keyExtractor={(item, index) => index.toString()}
renderItem={this.renderVideo}
style={{ width: '100%', alignSelf: 'stretch' }}
/>
</View>
)
}
renderVideo({ item }) {
const video = item
return (
<TouchableOpacity
key={video.title}
onPress={() => this.cast(video)}
style={{ flexDirection: 'row', padding: 10 }}
>
<Image
source={{ uri: video.imageUrl }}
style={{ width: 160, height: 90 }}
/>
<View style={{ flex: 1, marginLeft: 10, alignSelf: 'center' }}>
<Text style={{}}>{video.title}</Text>
<Text style={{ color: 'gray' }}>{video.studio}</Text>
</View>
</TouchableOpacity>
)
}
registerListeners() {
const events = `
SESSION_STARTING SESSION_STARTED SESSION_START_FAILED SESSION_SUSPENDED
SESSION_RESUMING SESSION_RESUMED SESSION_ENDING SESSION_ENDED
MEDIA_STATUS_UPDATED MEDIA_PLAYBACK_STARTED MEDIA_PLAYBACK_ENDED MEDIA_PROGRESS_UPDATED
CHANNEL_CONNECTED CHANNEL_DISCONNECTED CHANNEL_MESSAGE_RECEIVED
`
.trim()
.split(/\s+/)
console.log(events)
events.forEach(event => {
GoogleCast.EventEmitter.addListener(GoogleCast[event], function() {
console.log(event, arguments)
})
})
}
sendMessage() {
const channel = 'urn:x-cast:com.reactnative.googlecast.example'
GoogleCast.initChannel(channel).then(() => {
GoogleCast.sendMessage(channel, JSON.stringify({ message: 'Hello' }))
})
}
}
export default Main