make ratio update work for embedded view

This commit is contained in:
Christian Schabesberger 2024-07-11 14:32:33 +02:00
parent 03ac3b5f16
commit f4264c3892
4 changed files with 53 additions and 26 deletions

View File

@ -24,8 +24,12 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.hilt.navigation.compose.hiltViewModel
@ -46,14 +50,24 @@ class PlayerFragment() : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewModel.listener = object:VideoPlayerViewModel.Listener{
override fun contentRatioChagned(ratio: Float) {
println("gurken ratio is: $ratio")
}
}
val view = inflater.inflate(R.layout.player_framgent, container, false)
val composeView = view.findViewById<ComposeView>(R.id.player_copose_view)
val frameView = view.findViewById<FrameLayout>(R.id.frame_layout)
viewModel.listener = object : VideoPlayerViewModel.Listener {
override fun requestUpdateLayoutRatio(ratio: Float) {
frameView.updateLayoutParams<ConstraintLayout.LayoutParams> {
dimensionRatio = "$ratio:1"
}
}
override fun switchToFullscreen() {
frameView.updateLayoutParams<ConstraintLayout.LayoutParams> {
println("gurken fullscreen")
}
}
}
composeView.apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)

View File

@ -37,13 +37,15 @@ import net.newpipe.newplayer.utils.VideoSize
data class VideoPlayerUIState(
val playing: Boolean,
var fullscreen: Boolean,
var uiVissible: Boolean
var uiVissible: Boolean,
var contentRatio: Float
) {
companion object {
val DEFAULT = VideoPlayerUIState(
playing = false,
fullscreen = false,
uiVissible = false
uiVissible = false,
0F
)
}
}
@ -60,7 +62,8 @@ interface VideoPlayerViewModel {
fun switchToEmbeddedView()
interface Listener {
fun contentRatioChagned(ratio: Float)
fun requestUpdateLayoutRatio(ratio: Float)
fun switchToFullscreen()
}
}
@ -95,20 +98,26 @@ class VideoPlayerViewModelImpl @Inject constructor(
}
}
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
super.onMediaItemTransition(mediaItem, reason)
println("gurken mediaitem transition")
// We need to updated the layout of our player view if the video ratio changes
// However, this should be done differently depending on weather we are in
// embedded or fullscreen view.
// If we are in embedded view, we tell the mother layout (only ConstraintLayout supported!)
// to change the ratio of the whole player view.
// If we are in fullscreen we only want to change the ratio of the SurfaceView
override fun onVideoSizeChanged(media3VideoSize: androidx.media3.common.VideoSize) {
super.onVideoSizeChanged(media3VideoSize)
val videoSize = VideoSize.fromMedia3VideoSize(media3VideoSize)
val videoSize = VideoSize.fromMedia3VideoSize(player.videoSize)
val hight = player.videoSize.height
val width = player.videoSize.width
println("gurken videoSize: $videoSize, currentSize: $width, $hight")
TODO("DEN DIRNENSOHN FIXEN")
if(current_video_size != videoSize) {
if(current_video_size.getRatio() != videoSize.getRatio()) {
listener?.contentRatioChagned(videoSize.getRatio())
val newRatio = videoSize.getRatio()
if(current_video_size.getRatio() != newRatio) {
mutableUiState.update {
it.copy(contentRatio = newRatio)
}
if(!mutableUiState.value.fullscreen) {
listener?.requestUpdateLayoutRatio(newRatio)
}
}
current_video_size = videoSize
}
@ -119,7 +128,6 @@ class VideoPlayerViewModelImpl @Inject constructor(
player.playWhenReady = true
}
override fun play() {
println("gurken Play")
player.play()
@ -148,6 +156,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
mutableUiState.update {
it.copy(fullscreen = true)
}
listener?.switchToFullscreen()
}
override fun onCleared() {

View File

@ -31,7 +31,8 @@
<androidx.fragment.app.FragmentContainerView
android:id="@+id/player_frament_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_height="wrap_content"
android:minHeight="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"

View File

@ -21,16 +21,19 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,2:1">
<androidx.compose.ui.platform.ComposeView
android:id="@+id/player_copose_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="match_parent" />
</FrameLayout>