PierfrancescoSoffritti / android-youtube-player

YouTube Player library for Android and Chromecast, stable and customizable.
https://pierfrancescosoffritti.github.io/android-youtube-player/
MIT License
3.44k stars 765 forks source link

This YouTubePlayerView has already been initialized. #1150

Closed ghimiresiddhartha closed 5 months ago

ghimiresiddhartha commented 5 months ago

I did not want to provide lifecycle while playing the video instead I tried something like this step, removing the listener and releasing the player during onViewDetachFromWindow and reinitializing during onViewAttachToWindow. But I received the error This YouTubePlayerView has already been initialized. . While going through the source code, I found out that there is a variable check at LegacyYoutubePlayerView class named as isYouTubePlayerReady. And since it did not meet the requirement and it throw me this error.

I suggest you to try this code by yourself :

override fun onViewAttachedToWindow(holder: ViewHolder) {
        val options = IFramePlayerOptions.Builder().controls(0).build()
        holder.binding.youtubePlayerView.initialize(youtubePlayerListener, options)
        super.onViewAttachedToWindow(holder)
    }

    override fun onViewDetachedFromWindow(holder: ViewHolder) {
        holder.binding.youtubePlayerView.apply {
                removeYouTubePlayerListener(youtubePlayerListener)
                release()
            }
        super.onViewDetachedFromWindow(holder)
    }

Scenario to recreate check the instance of the player. During onViewAttach, a new youtubePlayerView instance is created. And during on detach previously loaded instance is provided. Everytime a new playerView instance is created from onAttach, it releases the previously loaded playerViewInstance which is good but when I try to reinitialize the previously released playerView instance again, it provides me the error. I don't know what is wrong here, since I already cleared previously initialized view's listener and also released it during onViewDetachedFromWindow. Why am I not able to reinitialize during onViewAttachedToWindow?

KeyurGedia commented 5 months ago

Having same issue in recyclerview. also set null to playerView and youTubePlayer instance variable, still getting this error on re-initialization.

PierfrancescoSoffritti commented 5 months ago

You can't re-initialize a released instance, you need to create a new one. So i don't think this is a bug.

But please let me know if you can replicate in the sample app.

ghimiresiddhartha commented 5 months ago

Apologies for marking this as a bug. I wanted to ensure that re-initializing the instance was possible. I am closing this question with this comment.

Thank you for your assistance!