-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.kt
155 lines (150 loc) · 6.21 KB
/
Main.kt
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
package dev.silenium.multimedia.compose
import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.awaitApplication
import dev.silenium.multimedia.compose.player.VideoSurfaceWithControls
import dev.silenium.multimedia.compose.player.rememberVideoPlayer
import dev.silenium.multimedia.compose.util.LocalFullscreenProvider
import dev.silenium.multimedia.core.annotation.InternalMultimediaApi
import kotlinx.coroutines.*
import java.nio.file.Files
import kotlin.io.path.absolutePathString
import kotlin.io.path.outputStream
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
@OptIn(InternalMultimediaApi::class)
@Composable
fun App() {
MaterialTheme(if (isSystemInDarkTheme()) darkColors() else lightColors()) {
val file = remember {
val videoFile = Files.createTempFile("video", ".webm")
Thread.currentThread().contextClassLoader.getResourceAsStream("1080p.webm").use {
videoFile.outputStream().use(it::copyTo)
}
videoFile.apply { toFile().deleteOnExit() }
}
var ready by remember { mutableStateOf(false) }
val coroutineScope = rememberCoroutineScope()
val player = rememberVideoPlayer(
onInitialized = {
ready = true
},
)
DisposableEffect(Unit) {
onDispose {
ready = false
}
}
LaunchedEffect(file) {
withContext(Dispatchers.Default) {
while (!ready && isActive) delay(10.milliseconds)
player.command("loadfile", file.absolutePathString())
}
}
val fullscreen = LocalFullscreenProvider.current.isFullscreen
val lazyState = rememberLazyListState()
BoxWithConstraints(
modifier = Modifier.background(MaterialTheme.colors.background).fillMaxSize()
) {
var visible by remember { mutableStateOf(true) }
var wasPaused by remember { mutableStateOf("no") }
LaunchedEffect(Unit) {
while (isActive) {
delay(2.seconds)
visible = !visible
}
}
val modifier = when {
fullscreen -> Modifier.size(
this@BoxWithConstraints.maxWidth,
this@BoxWithConstraints.maxHeight
)
else -> Modifier.requiredSizeIn(
this@BoxWithConstraints.minWidth,
this@BoxWithConstraints.minHeight,
this@BoxWithConstraints.maxWidth,
this@BoxWithConstraints.maxHeight,
)
}
LazyColumn(
modifier = modifier,
state = lazyState,
userScrollEnabled = !fullscreen,
) {
if (visible) {
item(key = "video", contentType = "video") {
VideoSurfaceWithControls(
player = player,
modifier = Modifier.fillParentMaxSize().animateItem(),
showStats = true,
controlFocusRequester = remember { FocusRequester() },
)
DisposableEffect(Unit) {
coroutineScope.launch {
println("Setting pause to $wasPaused")
player.setProperty("pause", wasPaused)
}
onDispose {
coroutineScope.launch {
wasPaused = player.getProperty<String>("pause").getOrNull() ?: "no"
player.setProperty("pause", "yes")
}
}
}
}
} else {
item(key = "video", contentType = "empty") {
Box(
modifier = Modifier.fillParentMaxSize().animateItem(),
) {
Text(
text = "Video player is not visible",
modifier = Modifier.padding(16.dp).align(Alignment.Center),
style = MaterialTheme.typography.h6,
color = MaterialTheme.colors.onBackground
)
}
}
}
item(key = "text", contentType = "text") {
Text(
text = "This is a test video player",
modifier = Modifier.padding(16.dp),
style = MaterialTheme.typography.h6,
color = MaterialTheme.colors.onBackground
)
}
}
}
var previousIndex by remember { mutableStateOf(0) }
var previousOffset by remember { mutableStateOf(0) }
LaunchedEffect(fullscreen) {
if (fullscreen) {
previousIndex = lazyState.firstVisibleItemIndex
previousOffset = lazyState.firstVisibleItemScrollOffset
lazyState.scrollToItem(0)
} else {
lazyState.scrollToItem(previousIndex, previousOffset)
}
}
}
}
suspend fun main(): Unit = awaitApplication {
val state = LocalFullscreenProvider.current.windowState
Window(state = state, onCloseRequest = ::exitApplication) {
App()
}
}