133 lines
No EOL
4.5 KiB
Kotlin
133 lines
No EOL
4.5 KiB
Kotlin
/* NewPlayer
|
|
*
|
|
* @author Christian Schabesberger
|
|
*
|
|
* Copyright (C) NewPipe e.V. 2024 <code(at)newpipe-ev.de>
|
|
*
|
|
* NewPlayer is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* NewPlayer is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with NewPlayer. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package net.newpipe.newplayer.ui
|
|
|
|
import android.content.Intent
|
|
import android.view.SurfaceView
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
import androidx.compose.material3.Surface
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.DisposableEffect
|
|
import androidx.compose.runtime.LaunchedEffect
|
|
import androidx.compose.runtime.collectAsState
|
|
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.graphics.Color
|
|
import androidx.compose.ui.platform.LocalContext
|
|
import androidx.compose.ui.platform.LocalLifecycleOwner
|
|
import androidx.compose.ui.tooling.preview.Preview
|
|
import androidx.compose.ui.viewinterop.AndroidView
|
|
import androidx.lifecycle.Lifecycle
|
|
import androidx.lifecycle.LifecycleEventObserver
|
|
import kotlinx.coroutines.flow.collectLatest
|
|
import net.newpipe.newplayer.VideoPlayerActivity
|
|
import net.newpipe.newplayer.model.VideoPlayerViewModel
|
|
import net.newpipe.newplayer.model.VideoPlayerViewModelImpl
|
|
import net.newpipe.newplayer.ui.theme.VideoPlayerTheme
|
|
import net.newpipe.newplayer.utils.findActivity
|
|
|
|
@Composable
|
|
fun VideoPlayerUI(
|
|
viewModel: VideoPlayerViewModel,
|
|
) {
|
|
val uiState by viewModel.uiState.collectAsState()
|
|
|
|
var lifecycle by remember {
|
|
mutableStateOf(Lifecycle.Event.ON_CREATE)
|
|
}
|
|
|
|
val activity = LocalContext.current.findActivity()
|
|
|
|
val lifecycleOwner = LocalLifecycleOwner.current
|
|
DisposableEffect(lifecycleOwner) {
|
|
val observer = LifecycleEventObserver { _, event ->
|
|
lifecycle = event
|
|
}
|
|
lifecycleOwner.lifecycle.addObserver(observer)
|
|
|
|
onDispose {
|
|
lifecycleOwner.lifecycle.removeObserver(observer)
|
|
}
|
|
}
|
|
|
|
LaunchedEffect(key1 = Unit) {
|
|
viewModel.events?.collectLatest { event ->
|
|
when (event) {
|
|
VideoPlayerViewModel.Events.SwitchToEmbeddedView -> {
|
|
activity?.finish()
|
|
}
|
|
|
|
VideoPlayerViewModel.Events.SwitchToFullscreen -> {
|
|
val fullscreen_activity_intent =
|
|
Intent(activity!!.findActivity(), VideoPlayerActivity::class.java)
|
|
activity.startActivity(fullscreen_activity_intent)
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Surface(
|
|
modifier = Modifier.fillMaxSize(),
|
|
color = Color.Black
|
|
) {
|
|
AndroidView(
|
|
modifier = Modifier.fillMaxSize(),
|
|
factory = { context ->
|
|
SurfaceView(context).also {
|
|
viewModel.player?.setVideoSurfaceView(it)
|
|
}
|
|
}, update = {
|
|
when (lifecycle) {
|
|
Lifecycle.Event.ON_PAUSE -> {
|
|
println("gurken state on pause")
|
|
viewModel.pause()
|
|
}
|
|
|
|
else -> Unit
|
|
}
|
|
})
|
|
|
|
val isPlaying = viewModel.player!!.isPlaying
|
|
println("is Player playing: $isPlaying")
|
|
VideoPlayerControllerUI(
|
|
isPlaying = uiState.playing,
|
|
fullscreen = uiState.fullscreen,
|
|
play = viewModel::play,
|
|
pause = viewModel::pause,
|
|
prevStream = viewModel::prevStream,
|
|
nextStream = viewModel::nextStream,
|
|
switchToFullscreen = viewModel::switchToFullscreen,
|
|
switchToEmbeddedView = viewModel::switchToEmbeddedView
|
|
)
|
|
}
|
|
}
|
|
|
|
@Preview(device = "spec:width=1080px,height=700px,dpi=440,orientation=landscape")
|
|
@Composable
|
|
fun PlayerUIPreviewEmbeded() {
|
|
VideoPlayerTheme {
|
|
VideoPlayerUI(viewModel = VideoPlayerViewModelImpl.dummy)
|
|
}
|
|
} |