android-youtube-player icon indicating copy to clipboard operation
android-youtube-player copied to clipboard

Is there any method to check if player is already initialized?

Open BhagyashreeWani opened this issue 2 years ago • 1 comments

Is there any method to check if player is already initialized? Or I am getting below error (Screenshot attached) in youtubePlayerview. Same videoId is working with https://www.youtube.com/

youtubePLayerSS

After getting such error for first video, I am unable to play next videoId in same youtubePlayerView and getting error as - java.lang.IllegalStateException: This YouTubePlayerView has already been initialized.

I am using - Kotlin view binding reference from xml - binding.youtubePlayerView

Originally posted by @BhagyashreeWani in https://github.com/PierfrancescoSoffritti/android-youtube-player/issues/832#issuecomment-1086702380

BhagyashreeWani avatar Apr 04 '22 17:04 BhagyashreeWani

I suggest you take a look at the examples in the sample app. A YouTubePlayer is initialized from the moment you get it.

PierfrancescoSoffritti avatar Apr 04 '22 18:04 PierfrancescoSoffritti

I have the same issue. I used it in a recyclerview. And it gives this error starting from the 3rd position usually. I initialized the ytplay view using the initialize method.

Devenom1 avatar May 20 '23 15:05 Devenom1

Have you looked at the sample app? it works there.

PierfrancescoSoffritti avatar May 20 '23 15:05 PierfrancescoSoffritti

Hey @PierfrancescoSoffritti thanks for replying so soon. Yes I did but I also kept the code to enable a fullscreen view (which is not in the recyclerview example). This requires us to manually call the initialize method (if I'm not mistaken). It gives an error on that line.

I don't think this is helpful but just in case it is this is what I do. I have an activity with a fragment in it. The RecyclerView is in the fragment. The RecyclerView has access to the fullscreen FrameLayout of the activity because I passed a reference to it.

Devenom1 avatar May 20 '23 16:05 Devenom1

So this is what is happening in my case. I use a RecyclerView because I have multiple videos. I also require to use the full screen functionality on each video. So I add this code like this in the recyclerview adapter

youtubePlayerView.enableAutomaticInitialization = false

val iFramePlayerOptions = IFramePlayerOptions.Builder()
    .controls(1)
    .fullscreen(1) // enable full screen button
    .build()

youtubePlayerView.addFullscreenListener(object : FullscreenListener {
    override fun onEnterFullscreen(fullscreenView: View, exitFullscreen: () -> Unit) {
        isFullscreen = true
       .....
    }

    override fun onExitFullscreen() {
        isFullscreen = false
       .....
    }
})

youtubePlayerView.initialize(object : AbstractYouTubePlayerListener() {
    override fun onReady(youTubePlayer: YouTubePlayer) {
        youTubePlayer.cueVideo(video.youtubeVideoId, 0f)
        ......
    }
}, iFramePlayerOptions)

For the fullscreen functionality we need the initialize method (correct me if I am wrong). So here's whats happening in the recyclerview adapter. Since the cells/item views get recycled the view containing the youtube player view also gets recycled. And because of that one of the youtube player view that was already initialized before calls the initialize method again.

I've very new to this library. I love it though. There's at least one good youtube library that supports youtube videos. Please correct me if I'm doing something wrong. Or let me know if there is a work around.

I've implemented a work around but it's not a good one and affects performance.

Devenom1 avatar May 20 '23 17:05 Devenom1

What error exactly are you getting? I don't think the issue is the same as described in the original message for this issue? I am going to close this issue, please open a new one for your problem so we can discuss there. Share all the information you have: what problem you're encountering, what your code looks like, what version of the library are you using, etc.

PierfrancescoSoffritti avatar May 21 '23 17:05 PierfrancescoSoffritti