Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
k21rs123 opened this issue Feb 12, 2024 · 1 comment
Open
Labels

Comments

@k21rs123
Copy link

k21rs123 commented Feb 12, 2024

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
Copy link

MuazKrtl commented Mar 7, 2024

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
            )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants