Created
August 21, 2021 07:16
-
-
Save theapache64/99d5582418dba8b8aee65edacd14e338 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.desktop.SwingPanel | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.size | |
import androidx.compose.material.Button | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.ExperimentalComposeUiApi | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.unit.dp | |
import androidx.compose.ui.window.Window | |
import androidx.compose.ui.window.application | |
import uk.co.caprica.vlcj.factory.discovery.NativeDiscovery | |
import uk.co.caprica.vlcj.player.component.CallbackMediaPlayerComponent | |
@ExperimentalComposeUiApi | |
fun main(args: Array<String>) = application { | |
Window { | |
Column( | |
modifier = Modifier.fillMaxSize() | |
) { | |
val playerState = remember { PlayerState() } | |
VideoPlayer( | |
modifier = Modifier.size(300.dp), | |
url = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", | |
playerState = playerState | |
) | |
@Suppress("SameParameterValue") | |
PlayPauseButton( | |
isPlay = playerState.isPlay(), | |
onPlayClicked = { | |
playerState.play() | |
}, | |
onPauseClicked = { | |
playerState.pause() | |
} | |
) | |
} | |
} | |
} | |
@Composable | |
fun PlayPauseButton( | |
modifier: Modifier = Modifier, | |
isPlay: Boolean, | |
onPlayClicked: () -> Unit, | |
onPauseClicked: () -> Unit | |
) { | |
Button( | |
modifier = modifier, | |
onClick = { | |
if (isPlay) { | |
onPauseClicked() | |
} else { | |
onPlayClicked() | |
} | |
}, | |
) { | |
Text( | |
if (isPlay) { | |
"PAUSE" | |
} else { | |
"PLAY" | |
} | |
) | |
} | |
} | |
private class PlayerState { | |
var playPause by mutableStateOf(true) | |
fun play() { | |
playPause = true | |
} | |
fun pause() { | |
playPause = false | |
} | |
fun isPlay() = playPause | |
fun isPause() = !playPause | |
} | |
@Composable | |
private fun VideoPlayer( | |
modifier: Modifier = Modifier, | |
url: String, | |
playerState: PlayerState | |
) { | |
println("Video player for $url") | |
NativeDiscovery().discover() | |
val mediaPlayerComponent = remember { CallbackMediaPlayerComponent() } | |
LaunchedEffect(Unit) { | |
val ok = mediaPlayerComponent.mediaPlayer().media().play(url) | |
println("play gave $ok") | |
} | |
// Play/Pause control | |
mediaPlayerComponent.mediaPlayer().controls().let { controls -> | |
if (playerState.isPlay()) { | |
controls.play() | |
} | |
if (playerState.isPause()) { | |
controls.pause() | |
} | |
} | |
return SwingPanel( | |
background = Color.Transparent, | |
modifier = modifier, | |
factory = { | |
mediaPlayerComponent | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment