-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPlayerActivity.kt
More file actions
241 lines (221 loc) · 11.1 KB
/
Copy pathPlayerActivity.kt
File metadata and controls
241 lines (221 loc) · 11.1 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package com.theoplayer.sample.playback.cast
import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import androidx.fragment.app.FragmentActivity
import androidx.mediarouter.app.MediaRouteButton
import com.google.android.gms.cast.framework.CastButtonFactory
import com.google.android.gms.cast.framework.CastContext
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.tasks.TaskExecutors
import com.theoplayer.android.api.THEOplayerConfig
import com.theoplayer.android.api.THEOplayerGlobal
import com.theoplayer.android.api.THEOplayerView
import com.theoplayer.android.api.cast.CastConfiguration
import com.theoplayer.android.api.cast.CastStrategy
import com.theoplayer.android.api.cast.cast
import com.theoplayer.android.api.cast.chromecast.ChromecastConnectionCallback
import com.theoplayer.android.api.event.chromecast.ChromecastEventTypes
import com.theoplayer.android.api.event.player.ErrorEvent
import com.theoplayer.android.api.event.player.PlayerEventTypes
import com.theoplayer.android.api.source.SourceDescription
import com.theoplayer.android.ui.DefaultUI
import com.theoplayer.android.ui.rememberPlayer
import com.theoplayer.android.ui.theme.THEOplayerTheme
import com.theoplayer.sample.common.AppTopBar
import com.theoplayer.sample.common.SourceManager
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.tasks.await
class PlayerActivity : FragmentActivity() {
private val config =
THEOplayerConfig.Builder()
.cast(
CastConfiguration.Builder()
.castStrategy(CastStrategy.MANUAL)
.build()
).build()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Enable all debug logs from THEOplayer.
THEOplayerGlobal.getSharedInstance(this).logger.enableAllTags()
// We need to call THEOplayerView constructor as soon as possible as it calls
// cast integration upon creation. In their turn, cast integration initializes
// cast framework and this initialization should happen as early as possible in the
// application lifecycle.
val theoPlayerView = THEOplayerView(this, config).apply {
keepScreenOn = true
// Allow background playback on the player to prevent Chromecast receiver from
// pausing when the app is backgrounded.
settings.setAllowBackgroundPlayback(true)
}
setContent {
val player = rememberPlayer(theoPlayerView)
val theoPlayer = player.player!!
val theoChromecast = player.cast!!.chromecast
LaunchedEffect(player) {
// Coupling the orientation of the device with the fullscreen state.
// The player will go fullscreen when the device is rotated to landscape
// and will also exit fullscreen when the device is rotated back to portrait.
player.theoplayerView!!.fullScreenManager.isFullScreenOrientationCoupled = true
// Configuring THEOplayer with defined SourceDescription object.
theoPlayer.source = SourceManager.BIG_BUCK_BUNNY_HLS_WITH_CAST_METADATA
// Set autoplay to start video whenever player is visible.
theoPlayer.isAutoplay = true
// Attach player event listeners.
theoPlayer.addEventListener(PlayerEventTypes.SOURCECHANGE) {
Log.i(TAG, "Event: SOURCECHANGE")
}
theoPlayer.addEventListener(PlayerEventTypes.CURRENTSOURCECHANGE) {
Log.i(TAG, "Event: CURRENTSOURCECHANGE")
}
theoPlayer.addEventListener(PlayerEventTypes.LOADEDDATA) {
Log.i(TAG, "Event: LOADEDDATA")
}
theoPlayer.addEventListener(PlayerEventTypes.LOADEDMETADATA) {
Log.i(TAG, "Event: LOADEDMETADATA")
}
theoPlayer.addEventListener(PlayerEventTypes.DURATIONCHANGE) {
Log.i(TAG, "Event: DURATIONCHANGE")
}
theoPlayer.addEventListener(PlayerEventTypes.TIMEUPDATE) {
// Log.i(TAG, "Event: TIMEUPDATE")
}
theoPlayer.addEventListener(PlayerEventTypes.PLAY) {
Log.i(TAG, "Event: PLAY")
}
theoPlayer.addEventListener(PlayerEventTypes.PLAYING) {
Log.i(TAG, "Event: PLAYING")
}
theoPlayer.addEventListener(PlayerEventTypes.PAUSE) {
Log.i(TAG, "Event: PAUSE")
}
theoPlayer.addEventListener(PlayerEventTypes.SEEKING) {
Log.i(TAG, "Event: SEEKING")
}
theoPlayer.addEventListener(PlayerEventTypes.SEEKED) {
Log.i(TAG, "Event: SEEKED")
}
theoPlayer.addEventListener(PlayerEventTypes.WAITING) {
Log.i(TAG, "Event: WAITING")
}
theoPlayer.addEventListener(PlayerEventTypes.READYSTATECHANGE) {
Log.i(TAG, "Event: READYSTATECHANGE")
}
theoPlayer.addEventListener(PlayerEventTypes.PRESENTATIONMODECHANGE) {
Log.i(TAG, "Event: PRESENTATIONMODECHANGE")
}
theoPlayer.addEventListener(PlayerEventTypes.VOLUMECHANGE) {
Log.i(TAG, "Event: VOLUMECHANGE")
}
theoPlayer.addEventListener(PlayerEventTypes.ENDED) {
Log.i(TAG, "Event: ENDED")
}
theoPlayer.addEventListener(PlayerEventTypes.ERROR) { event: ErrorEvent ->
Log.i(TAG, "Event: ERROR, error=" + event.errorObject)
}
// Attach Chromecast event listeners.
theoChromecast.addEventListener(ChromecastEventTypes.STATECHANGE) {
Log.i(TAG, "Event: CAST_STATECHANGE, state=" + it.state)
}
theoChromecast.addEventListener(ChromecastEventTypes.ERROR) {
Log.i(TAG, "Event: CAST_ERROR, error=" + it.error)
}
// Some applications that do not require a MediaRouteButton to control the connection
// with the Cast Receiver device can use the below APIs instead.
// theoChromecast.start()
// theoChromecast.stop()
// theoChromecast.join()
// theoChromecast.leave()
// Some streaming setups require casting a different stream to a Cast Receiver device
// than the one playing on a Cast Sender device, e.g. different DRM capabilities.
// Code below shows how to configure such a different stream to cast.
theoChromecast.setConnectionCallback(object : ChromecastConnectionCallback {
override fun onStart(sourceDescription: SourceDescription?): SourceDescription? {
return null
}
override fun onStop(sourceDescription: SourceDescription?): SourceDescription? {
return null
}
override fun onJoin(sourceDescription: SourceDescription?): SourceDescription? {
return null
}
override fun onLeave(sourceDescription: SourceDescription?): SourceDescription? {
return null
}
})
}
val isCastAvailable = checkChromeCastAvailable()
THEOplayerTheme(useDarkTheme = true) {
Scaffold(
topBar = {
AppTopBar(actions = {
if (isCastAvailable) {
AndroidView(
// This is a custom MediaRouterButton that is used to control the
// connection with the Cast Receiver device. Open Video UI already
// provides a default MediaRouterButton implementation, but you can
// also create your own custom button by using the
// CastButtonFactory.setUpMediaRouteButton() method.
factory = { context ->
MediaRouteButton(context).also {
CastButtonFactory.setUpMediaRouteButton(context, it)
}
}
)
}
})
}
) { padding ->
DefaultUI(
modifier = Modifier
.padding(padding)
.fillMaxSize(),
player = player
)
}
}
}
}
@Composable
private fun checkChromeCastAvailable(): Boolean {
var isCastAvailable by remember { mutableStateOf(false) }
val googleApi = GoogleApiAvailability.getInstance()
LaunchedEffect(googleApi) {
val googlePlayServicesAvailability =
googleApi.isGooglePlayServicesAvailable(applicationContext)
if (googlePlayServicesAvailability == ConnectionResult.SUCCESS) {
try {
CastContext.getSharedInstance(applicationContext, TaskExecutors.MAIN_THREAD)
.await()
isCastAvailable = true
} catch (_: CancellationException) {
} catch (e: Exception) {
Log.e(TAG, "Failed to obtain CastContext", e)
}
} else {
Log.i(
TAG,
"Google Play Services are not available. ${
googleApi.getErrorString(googlePlayServicesAvailability)
}"
)
}
}
return isCastAvailable
}
companion object {
private val TAG: String = PlayerActivity::class.java.simpleName
}
}