diff --git a/app/src/main/java/net/newpipe/newplayer/PlayerFragment.kt b/app/src/main/java/net/newpipe/newplayer/PlayerFragment.kt index a199f85..c3670e8 100644 --- a/app/src/main/java/net/newpipe/newplayer/PlayerFragment.kt +++ b/app/src/main/java/net/newpipe/newplayer/PlayerFragment.kt @@ -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(R.id.player_copose_view) + val frameView = view.findViewById(R.id.frame_layout) + + viewModel.listener = object : VideoPlayerViewModel.Listener { + override fun requestUpdateLayoutRatio(ratio: Float) { + frameView.updateLayoutParams { + dimensionRatio = "$ratio:1" + } + } + + override fun switchToFullscreen() { + frameView.updateLayoutParams { + + println("gurken fullscreen") + } + } + } composeView.apply { setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) diff --git a/app/src/main/java/net/newpipe/newplayer/model/VideoPlayerViewModel.kt b/app/src/main/java/net/newpipe/newplayer/model/VideoPlayerViewModel.kt index 0f02692..307f1c9 100644 --- a/app/src/main/java/net/newpipe/newplayer/model/VideoPlayerViewModel.kt +++ b/app/src/main/java/net/newpipe/newplayer/model/VideoPlayerViewModel.kt @@ -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() { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 31aba17..53ed26d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -31,7 +31,8 @@ + android:layout_height="wrap_content" + xmlns:app="http://schemas.android.com/apk/res-auto"> + android:layout_height="0dp" + app:layout_constraintDimensionRatio="H,2:1"> + android:layout_height="match_parent" />