YouTubePlayer onReady() not called on second Activity
See original GitHub issueandroid-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:
- Created a year ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
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.
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.