-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPlayerActivity.kt
More file actions
141 lines (127 loc) · 6.07 KB
/
Copy pathPlayerActivity.kt
File metadata and controls
141 lines (127 loc) · 6.07 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
package com.theoplayer.sample.playback.offline
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
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.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import com.theoplayer.android.api.THEOplayerConfig
import com.theoplayer.android.api.THEOplayerGlobal
import com.theoplayer.android.api.THEOplayerView
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.api.source.TypedSource
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
class PlayerActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Enable all debug logs from THEOplayer.
THEOplayerGlobal.getSharedInstance(this).logger.enableAllTags()
super.onCreate(savedInstanceState)
val sourceUrl = intent.getStringExtra(PLAYER_PARAM__SOURCE_URL)
val sourceDescription = pendingSourceDescription?.also { pendingSourceDescription = null }
?: SourceDescription.Builder(TypedSource.Builder(sourceUrl ?: "").build()).build()
setContent {
val context = LocalContext.current
val theoplayerView = remember(context) {
THEOplayerView(context, THEOplayerConfig.Builder().build()).apply {
keepScreenOn = true
}
}
val player = rememberPlayer(theoplayerView)
val theoPlayer = theoplayerView.player
LaunchedEffect(player) {
// Coupling the orientation of the device with the fullscreen state.
theoplayerView.fullScreenManager.isFullScreenOrientationCoupled = true
// THEOplayer will automatically match the URL to any existing caching task.
theoPlayer.source = sourceDescription
theoPlayer.isAutoplay = true
// Attach 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)
}
}
THEOplayerTheme(useDarkTheme = true) {
Scaffold(
topBar = { AppTopBar() }
) { padding ->
DefaultUI(
modifier = Modifier
.padding(padding)
.fillMaxSize(),
player = player
)
}
}
}
}
companion object {
private val TAG = PlayerActivity::class.java.simpleName
private const val PLAYER_PARAM__SOURCE_URL = "SOURCE_URL"
private var pendingSourceDescription: SourceDescription? = null
fun play(context: Context, sourceDescription: SourceDescription) {
pendingSourceDescription = sourceDescription
val playIntent = Intent(context, PlayerActivity::class.java)
playIntent.putExtra(PLAYER_PARAM__SOURCE_URL, sourceDescription.sources[0].src)
context.startActivity(playIntent)
}
}
}