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

YouTubePlayer onReady() not called on second Activity #846

Closed
LukaszRogozinski opened this issue Apr 27, 2022 · 5 comments
Closed

YouTubePlayer onReady() not called on second Activity #846

LukaszRogozinski opened this issue Apr 27, 2022 · 5 comments
Labels

Comments

@LukaszRogozinski
Copy link

android-youtube-player: version 11.0.1
jetpack-compose
tested on: Android API 27 and API 29
I created new Empty Compose Activity in Android Studio and initialized YouTubePlayer in my FirstActivity like this:

class FirstActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    val lifecycleOwner = LocalLifecycleOwner.current
                    AndroidView(
                        factory = { context ->
                            val youTubePlayerView = YouTubePlayerView(context)
                            lifecycleOwner.lifecycle.addObserver(youTubePlayerView)
                            youTubePlayerView.enableAutomaticInitialization = false
                            val listener: YouTubePlayerListener =
                                object : AbstractYouTubePlayerListener() {

                                    override fun onReady(youTubePlayer: YouTubePlayer) {

                                        val defaultYouTubePlayerController =
                                            DefaultPlayerUiController(
                                                youTubePlayerView,
                                                youTubePlayer
                                            )
                                        defaultYouTubePlayerController.showYouTubeButton(false)
                                        defaultYouTubePlayerController.setFullScreenButtonClickListener {
                                            startNewActivity()
                                        }
                                        youTubePlayerView.setCustomPlayerUi(
                                            defaultYouTubePlayerController.rootView
                                        )
                                        youTubePlayer.cueVideo("6JYIGclVQdw", 0f)
                                    }
                                }

                            val options =
                                IFramePlayerOptions
                                    .Builder()
                                    .controls(0)
                                    .rel(0)
                                    .build()
                            youTubePlayerView.initialize(listener, true, options)
                            youTubePlayerView
                        }
                    )
                }
            }
        }
    }

    fun startNewActivity() {
        val intent = Intent(this, SecondActivity::class.java)
        this.startActivity(intent)
    }
}

and here everything is fine, YouTubePlayer is initialized, video is loaded and onReady() method is called. When I click full screen button which redirect us to SecondActivity the method onReady() from YouTubePlayer in SecondActivity is not called. This is how SecondActivity looks like:

class SecondActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    val lifecycleOwner = LocalLifecycleOwner.current
                    AndroidView(
                        factory = { context ->
                            YouTubePlayerView(context).apply {
                                lifecycleOwner.lifecycle.addObserver(this)

                                addYouTubePlayerListener(object : AbstractYouTubePlayerListener() {
                                    override fun onReady(youTubePlayer: YouTubePlayer) {
                                        youTubePlayer.cueVideo("LvetJ9U_tVY", 0f)
                                    }
                                })
                            }
                        }
                    )
                }
            }
        }
    }
}

Strange thing is that when I play video in FirstActivity and then open SecondActivity, most of the time everything is fine, YouTubePlayer in SecondActivity is loaded correctly and we can play video. But when I'm in FirstActivity and do NOT hit "play" button, but try to open SecondActivity, the onReady() method from YouTubePlayer in SecondActivity is almost never called.

@PierfrancescoSoffritti
Copy link
Owner

Hi, could be related to #830 ?

@LukaszRogozinski
Copy link
Author

Not really, because I got the issue even when player in FirstActivity is loaded and then wait 2-3 seconds before navigate to other screen

@LukaszRogozinski
Copy link
Author

For me it looks like the state of firstly created player have impact on every other created YouTubePlayer. For example when I make SecondActivity as my initial Activity, the player is loaded correctly. Also I made test where in my first Activity I created multiple instances of YouTubePlayer and all of them loaded correctly, but when I switched to another Activity the player in that another Activity also does not called onReady() method.

@roelvogel
Copy link

I think I'm experiencing the same issue. The firstly created player always works, instances after that have a chance of not working. The ones that do not work are either in a new Fragment or another Activity.

@PierfrancescoSoffritti
Copy link
Owner

This should be fixed in version 11.1.0.

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

3 participants