-
Notifications
You must be signed in to change notification settings - Fork 10
/
PlayerManager.js
173 lines (140 loc) Β· 4.33 KB
/
PlayerManager.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
173
import React from 'react'
import PropTypes from 'prop-types'
import { StyleSheet, View } from 'react-native'
import GoogleCast, { CastButton } from 'react-native-google-cast'
import Orientation from 'react-native-orientation'
import withIpFinder from 'modules/IpFinder/withIpFinder'
import dimensions from 'modules/dimensions'
import colors from 'modules/colors'
const styles = StyleSheet.create({
container: {
position: 'absolute',
zIndex: 2000,
width: dimensions.ICON_CAST_SIZE,
height: dimensions.ICON_CAST_SIZE,
},
castButton: {
width: dimensions.ICON_CAST_SIZE,
height: dimensions.ICON_CAST_SIZE,
tintColor: colors.PRIMARY_COLOR_200,
},
})
@withIpFinder
export default class PlayerManager extends React.Component {
constructor(props) {
super(props)
const { ipFinder, item } = props
this.state = {
mediaUrl: `http://${ipFinder.host}/watch/${item._id}`,
currentTime: 0,
casting: false,
}
}
componentDidMount() {
GoogleCast.EventEmitter.addListener(GoogleCast.SESSION_STARTED, this.handleCastSessionStarted)
GoogleCast.EventEmitter.addListener(GoogleCast.SESSION_ENDED, this.handleCastSessionEnded)
GoogleCast.EventEmitter.addListener(GoogleCast.MEDIA_PLAYBACK_STARTED, this.handleMediaPlaybackStarted)
GoogleCast.EventEmitter.addListener(GoogleCast.MEDIA_PROGRESS_UPDATED, this.handleMediaProgressUpdate)
GoogleCast.EventEmitter.addListener(GoogleCast.MEDIA_PLAYBACK_ENDED, this.handleMediaPlaybackEnded)
GoogleCast.getCastState().then((state) => {
if (state.toLowerCase() === 'connected') {
this.setState({
casting: true,
})
}
})
}
componentWillUnmount() {
const { casting } = this.state
GoogleCast.EventEmitter.removeAllListeners(GoogleCast.SESSION_STARTED)
GoogleCast.EventEmitter.removeAllListeners(GoogleCast.SESSION_ENDED)
GoogleCast.EventEmitter.removeAllListeners(GoogleCast.MEDIA_PLAYBACK_STARTED)
GoogleCast.EventEmitter.removeAllListeners(GoogleCast.MEDIA_PROGRESS_UPDATED)
if (casting) {
// Stop casting but keep the connection
GoogleCast.stop()
}
}
componentDidUpdate(prevProps, prevState, snapshot) {
const { isBuffering: wasBuffering } = prevProps
const { isBuffering } = this.props
if (wasBuffering && !isBuffering) {
// Buffering is done, check if we need to cast it
this.handleStartCasting()
}
}
handleStartCasting = (force = false) => {
const { item } = this.props
const { casting, mediaUrl } = this.state
if (force || casting) {
Orientation.lockToPortrait()
GoogleCast.castMedia({
title: (
item.type === 'episode'
? `${item.show.title}: ${item.number}. ${item.title}`
: item.title
),
subtitle: item.synopsis,
mediaUrl: `${mediaUrl}?device=chromecast`,
posterUrl: item.type === 'episode'
? item.show.images.poster.high
: item.images.poster.high,
})
}
}
handleCastSessionStarted = () => {
this.setState({
casting: true,
}, () => {
this.handleStartCasting(true)
})
}
handleCastSessionEnded = (error) => {
// TODO:: Check what the error is and maybe then add the device flag?
console.log('handleCastSessionEnded', error)
this.setState({
casting: false,
})
}
handleMediaPlaybackStarted = () => {
const { currentTime } = this.state
if (currentTime) {
GoogleCast.seek(currentTime)
}
}
handleMediaPlaybackEnded = (...a) => {
console.log('handleMediaPlaybackEnded', a)
}
handleMediaProgressUpdate = (...a) => {
console.log('handleMediaProgressUpdate', a)
}
handleSetCurrentTime = (currentTime) => {
this.setState({
currentTime,
})
}
renderCastButton = (style = {}) => {
return (
<View
style={[styles.container, style]}
pointerEvents={'box-none'}>
<CastButton style={styles.castButton} />
</View>
)
}
render() {
const { style, children } = this.props
const { casting, mediaUrl } = this.state
console.log('casting', casting)
return (
<View style={style}>
{children({
casting,
mediaUrl,
renderCastButton: this.renderCastButton,
setCurrentTime: this.handleSetCurrentTime,
})}
</View>
)
}
}