Youtube player support fragment no longer working on Android studio 3.2 (androidx)

前端 未结 6 1007
梦谈多话
梦谈多话 2020-11-30 12:35

I just updated my Android Studio to version 3.2 and followed instructions to use androidx.

I\'ve been using a Youtube fragment inside a Fragment activity and everyth

6条回答
  •  余生分开走
    2020-11-30 12:46

    Many thanks to both @Hosszuful and @Mehdi. I have followed your advice and it worked very nicely.

    A few weeks after I asked this question I "translated" my app to Kotlin and, therefore, I tried to translate your answer as well.

    This is what I ended up with and it's working for me.

    package com.google.android.youtube.player //<--- IMPORTANT!!!!
    
    import android.os.Bundle
    import android.view.LayoutInflater
    import android.view.ViewGroup
    import androidx.fragment.app.Fragment
    import com.google.android.youtube.player.internal.ab
    import java.util.*
    
    class YouTubePlayerSupportFragmentX : Fragment(), YouTubePlayer.Provider {
        private val a = ViewBundle()
        private var b: Bundle? = null
        private var c: YouTubePlayerView? = null
        private var d: String? = null
        private var e: YouTubePlayer.OnInitializedListener? = null
        override fun initialize(var1: String, var2: YouTubePlayer.OnInitializedListener) {
            d = ab.a(var1, "Developer key cannot be null or empty")
            e = var2
            a()
        }
    
        private fun a() {
            if (c != null && e != null) {
                c?.a(this.activity, this, d, e, b)
                b = null
                e = null
            }
        }
    
        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?): android.view.View? {
            c = YouTubePlayerView(Objects.requireNonNull(this.activity), null, 0, a)
            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)
            (if (c != null) c?.e() else b)?.let { var2 ->
                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) {
                val var1 = this.activity
                c?.b(var1 == null || var1.isFinishing)
            }
            super.onDestroy()
        }
    
        private inner class ViewBundle : YouTubePlayerView.b {
            override fun a(var1: YouTubePlayerView, var2: String, var3: YouTubePlayer.OnInitializedListener) {
                e?.let { initialize(var2, it) }
            }
    
            override fun a(var1: YouTubePlayerView) {}
        }
    
        companion object {
            fun newInstance(): YouTubePlayerSupportFragmentX {
                return YouTubePlayerSupportFragmentX()
            }
        }
    }
    

    There may be better ways to write it down and any help on that regard would be mostly appreciated but, if anyone else was looking for the Kotlin version of this problem's solution, this code should do.

    PS: I'm gonna leave @Mehdi's answer as the accepted one because he's also sharing credits with @Hosszuful and because my answer is just the Kotlin version of what they suggest.

提交回复
热议问题