question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

YouTubePlayer onReady() not called on second Activity

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
roelvogelcommented, May 11, 2022

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.

1reaction
LukaszRogozinskicommented, Apr 28, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

onReady never called when enter and exit activity quickly
If the youtube player display playback error, if you click the video, loadvideo will be ... YouTubePlayer onReady() not called on second Activity...
Read more >
YouTube Player API onReady not firing for www. ...
The following code works in JsFiddle, but does not work in StackOverflow editor. window.onYouTubeIframeAPIReady = function() { player = new YT.
Read more >
How to Implement YoutubePlayerView Library in Android?
Inside this, we will create a simple button that will redirect to a new activity where we will play our YouTube video.
Read more >
PierfrancescoSoffritti / android-youtube-player Download
If the Activity/Fragment is in the background, but you created a YouTubePlayerListener that calls loadVideo when onReady is called, the video will start...
Read more >
YouTube Player API Reference for iframe Embeds
This function creates an <iframe> (and YouTube player) // after the API code downloads. var player; function onYouTubeIframeAPIReady() { player = new YT....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found