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

View File

@ -37,13 +37,15 @@ import net.newpipe.newplayer.utils.VideoSize
data class VideoPlayerUIState( data class VideoPlayerUIState(
val playing: Boolean, val playing: Boolean,
var fullscreen: Boolean, var fullscreen: Boolean,
var uiVissible: Boolean var uiVissible: Boolean,
var contentRatio: Float
) { ) {
companion object { companion object {
val DEFAULT = VideoPlayerUIState( val DEFAULT = VideoPlayerUIState(
playing = false, playing = false,
fullscreen = false, fullscreen = false,
uiVissible = false uiVissible = false,
0F
) )
} }
} }
@ -60,7 +62,8 @@ interface VideoPlayerViewModel {
fun switchToEmbeddedView() fun switchToEmbeddedView()
interface Listener { 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) { // We need to updated the layout of our player view if the video ratio changes
super.onMediaItemTransition(mediaItem, reason) // However, this should be done differently depending on weather we are in
println("gurken mediaitem transition") // 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 != videoSize) {
val newRatio = videoSize.getRatio()
if(current_video_size.getRatio() != newRatio) {
if(current_video_size.getRatio() != videoSize.getRatio()) { mutableUiState.update {
listener?.contentRatioChagned(videoSize.getRatio()) it.copy(contentRatio = newRatio)
}
if(!mutableUiState.value.fullscreen) {
listener?.requestUpdateLayoutRatio(newRatio)
}
} }
current_video_size = videoSize current_video_size = videoSize
} }
@ -119,7 +128,6 @@ class VideoPlayerViewModelImpl @Inject constructor(
player.playWhenReady = true player.playWhenReady = true
} }
override fun play() { override fun play() {
println("gurken Play") println("gurken Play")
player.play() player.play()
@ -148,6 +156,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
mutableUiState.update { mutableUiState.update {
it.copy(fullscreen = true) it.copy(fullscreen = true)
} }
listener?.switchToFullscreen()
} }
override fun onCleared() { override fun onCleared() {

View File

@ -31,7 +31,8 @@
<androidx.fragment.app.FragmentContainerView <androidx.fragment.app.FragmentContainerView
android:id="@+id/player_frament_view" android:id="@+id/player_frament_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="300dp" android:layout_height="wrap_content"
android:minHeight="50dp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="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" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 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 <FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent" 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 <androidx.compose.ui.platform.ComposeView
android:id="@+id/player_copose_view" android:id="@+id/player_copose_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" /> android:layout_height="match_parent" />
</FrameLayout> </FrameLayout>