Skip to content

Instantly share code, notes, and snippets.

Last active June 7, 2023 13:55
Show Gist options
  • Save novanto/5ea09bebd683a2bbdd9c4f697b6a6b28 to your computer and use it in GitHub Desktop.
Save novanto/5ea09bebd683a2bbdd9c4f697b6a6b28 to your computer and use it in GitHub Desktop.
This is an alternative solution for YoutubePlayerSupportFragment for AndroidX - written in Kotlin. Feel free to comment if the code is not working! this is a translated version of
import android.os.Bundle
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
// source :
open class YouTubePlayerSupportFragmentXKt : Fragment(), YouTubePlayer.Provider {
private val a: A = A()
private var b: Bundle? = null
private var c: YouTubePlayerView? = null
private var d: String? = null
private var e: YouTubePlayer.OnInitializedListener? = null
private val f = false
private fun a() {
if (c != null && e != null) {
c?.a(this.activity, this, d, e, b)
b = null
e = null
override fun initialize(p0: String?, p1: YouTubePlayer.OnInitializedListener?) {
this.d = ab.a(p0, "Developer key cannot be null or empty")
this.e = p1
override fun onCreate(var1: Bundle?) {
b = var1?.getBundle("YouTubePlayerSupportFragment.KEY_PLAYER_VIEW_STATE")
override fun onCreateView(var1: LayoutInflater, var2: ViewGroup?, var3: Bundle?): View? {
c = YouTubePlayerView(this.activity, null as AttributeSet?, 0, a)
return c
override fun onStart() {
override fun onResume() {
override fun onPause() {
override fun onSaveInstanceState(var1: Bundle) {
val var2 = if (c != null && c is YouTubePlayerView) {
(c as YouTubePlayerView).e()
} else b
var1.putBundle("YouTubePlayerSupportFragment.KEY_PLAYER_VIEW_STATE", var2)
override fun onStop() {
override fun onDestroyView() {
this.activity?.let {
c = null
override fun onDestroy() {
if (c != null && c is YouTubePlayerView) {
val var1 = this.activity
(c as YouTubePlayerView).b(var1 == null || var1.isFinishing)
companion object {
fun newInstance(): YouTubePlayerSupportFragmentXKt {
return YouTubePlayerSupportFragmentXKt()
private class A : YouTubePlayerView.b {
override fun a(
p0: YouTubePlayerView?,
p1: String?,
p2: YouTubePlayer.OnInitializedListener?
) {
val fragment = newInstance()
fragment.initialize(p1, fragment.e)
override fun a(p0: YouTubePlayerView?) {
// do nothing
Copy link

novanto commented Jul 17, 2021

I'm getting error "Cannot access 'b' : it is package-private in 'YouTubePlayerView'.
How can I import ''?

I think you are referencing a wrong thing. The only thing you import is and b shouldn't be imported since it's the Bundle defined in the class.

Copy link

Idealim commented Jul 18, 2021

Thanks! I have wrong directory name..

Copy link

Hm, I also get this error Cannot access 'b': it is package-private in 'YouTubePlayerView' but I do only import .ab. hm

Copy link

Ah TIL: the package has to be the package!

Copy link

little issue using this, here is what I get: java.lang.ClassCastException: com.template.youtubetest.MainActivity cannot be cast to

Copy link

lglf77 commented Feb 3, 2023

Post a simple git hub of the working code, if you want, confused, I think Kotlin is rubbish of novelty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment