PierfrancescoSoffritti / android-youtube-player

YouTube Player library for Android and Chromecast, stable and customizable.
https://pierfrancescosoffritti.github.io/android-youtube-player/
MIT License
3.42k stars 759 forks source link

How to Make the YoutubePlayer Display Area Full Screen in JetPack Compose? #1110

Closed k21rs123 closed 2 months ago

k21rs123 commented 8 months ago

Question / Problem

@PierfrancescoSoffritti How to Change the Display Area in JetPack Compose: Tried fillMaxHeight, fillMaxSize, and Height, But Size Doesn't Change. Need Guidance.

youTubePlayerView.matchParent() <- crash

var jsonArray = mutableListOf<Questionnaire>()

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun SecondScreen() {
    //  Screen composable content
    VerticalPagerParts(jsonArray.size)

}

@Composable
fun YouTubePlayerWrapper(videoId: String) {
    val configuration = LocalConfiguration.current
    val screenHeight = configuration.screenHeightDp.dp

    AndroidView(
        modifier = Modifier
            .height(screenHeight)

            .fillMaxHeight(),
        factory = { context ->

            val youTubePlayerView = YouTubePlayerView(context)
            youTubePlayerView.apply {

                addYouTubePlayerListener(object : AbstractYouTubePlayerListener() {
                    override fun onReady(youTubePlayer: YouTubePlayer) {
                        youTubePlayer.cueVideo(videoId, 0f)
                        Log.d("height",screenHeight.toString())
                    }

                    override fun onStateChange(
                        youTubePlayer: YouTubePlayer, state: PlayerConstants.PlayerState
                    ) {
                    }
                })
            }
//            youTubePlayerView.inflateCustomPlayerUi()

        },

    )
}

@Composable
@ExperimentalFoundationApi
fun VerticalPagerParts(pages: Int) {

    val pagerState = rememberPagerState(pageCount = {
        pages
    })

    val videoIds = jsonArray.let { data ->
        data.mapNotNull { it.videoId }
    }
    VerticalPager(
        state = pagerState,
        modifier = Modifier.fillMaxHeight()
    ) { page ->

        YouTubePlayerWrapper(videoId = videoIds[page])

    }
}

Current Layout

Desired Layout

MuazKrtl commented 7 months ago

Remove the modifier inside androidView and try like this(with adding ViewGroup.LayoutParams)

AndroidView( factory = {

        val view = YouTubePlayerView(it).apply {
            enableAutomaticInitialization = false
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
k21rs123 commented 2 months ago

Sorry for the delayed reply. You are my god