Skip to content

Instantly share code, notes, and snippets.

@novanto
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 https://gist.github.com/medyo/f226b967213c3b8ec6f6bebb5338a492#file-youtubeplayersupportfragmentx-java
package com.google.android.youtube.player
import android.os.Bundle
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.youtube.player.internal.ab
// source : https://gist.github.com/medyo/f226b967213c3b8ec6f6bebb5338a492#file-youtubeplayersupportfragmentx-java
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(f)
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
this.a()
}
override fun onCreate(var1: Bundle?) {
super.onCreate(var1)
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)
this.a()
return c
}
override fun onStart() {
super.onStart()
c?.a()
}
override fun onResume() {
super.onResume()
c?.b()
}
override fun onPause() {
c?.c()
super.onPause()
}
override fun onSaveInstanceState(var1: Bundle) {
super.onSaveInstanceState(var1)
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() {
c?.d()
super.onStop()
}
override fun onDestroyView() {
this.activity?.let {
c?.c(it.isFinishing)
c = null
}
super.onDestroyView()
}
override fun onDestroy() {
if (c != null && c is YouTubePlayerView) {
val var1 = this.activity
(c as YouTubePlayerView).b(var1 == null || var1.isFinishing)
}
super.onDestroy()
}
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
}
}
}
@emre-ozcan
Copy link

Thanks this is worked for me

@Idealim
Copy link

Idealim commented Jul 17, 2021

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

@novanto
Copy link
Author

novanto commented Jul 17, 2021

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

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

@Idealim
Copy link

Idealim commented Jul 18, 2021

Thanks! I have wrong directory name..

@tscholze
Copy link

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

@tscholze
Copy link

Ah TIL: the package has to be the package!

@desbiensa
Copy link

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

@lglf77
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