'YouTubeAndroidPlayerAPI only loads once

private lateinit var youtubePLayerInit: YouTubePlayer.OnInitializedListener
private fun openTrailer(videoId: String) {
    val dialog = Dialog(this)
    val trailerDialogBinding =  DialogTrailerBinding.inflate(layoutInflater)
    dialog.setContentView(trailerDialogBinding.root)
    dialog.setCanceledOnTouchOutside(false)

    val youtubeApiKey = "xxxxxxxxxxxxxxxxxxxxxxx"

    youtubePLayerInit = object: YouTubePlayer.OnInitializedListener {
        override fun onInitializationSuccess(
            p0: YouTubePlayer.Provider?,
            p1: YouTubePlayer?,
            p2: Boolean
        ) {
            p1?.loadVideo(videoId)
        }

        override fun onInitializationFailure(
            p0: YouTubePlayer.Provider?,
            p1: YouTubeInitializationResult?
        ) {
            Toast.makeText(applicationContext, "Video Failed to Load", Toast.LENGTH_SHORT).show()
        }
    }

    trailerDialogBinding.vvMovieTrailer.initialize(youtubeApiKey, youtubePLayerInit)
    dialog.show()
    trailerDialogBinding.btnExit.setOnClickListener {
        dialog.dismiss()
    }
}

Thing is that video loads only one time, when I reload the video then dismissing the dialog. It doesn't load, then after like 3 or 4 times of loading, the app crashes with the errors of :

E/YouTubeAndroidPlayerAPI: Embed config is not supported in RemoteEmbeddedPlayer.
E/YouTubeAndroidPlayerAPI: Error screen presenter should be present

It doesn't even show the ad failed to load. which is supposed to show when the video fails to load.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source