SvenTiigi / YouTubePlayerKit

A Swift Package to easily play YouTube videos 📺
https://sventiigi.github.io/YouTubePlayerKit/
MIT License
676 stars 60 forks source link

YoutubePlayer state stuck to .idle #94

Closed HPezz closed 3 weeks ago

HPezz commented 2 months ago

What happened?

Hi, I wanted to first thank you for this simple and intuitive kit!

I'm currently facing an issue while trying to use your YouTubePlayerView with the closure state. The state seems to be stuck in .idle. As a result, the overlay ProgressView() continues to spin, regardless of whether the video is ready or not loading.

I tried to bypass this issue by subscribing to the statePublisher attribute of YouTubePlayer, but it was the same; not one event was received.

Are you experiencing the same issue?

What are the steps to reproduce?

I'm working with XCode v15.4 / iPad iOS 17.5 and only trying to use the example of YoutubePlayerView initialized with the state closure :

import SwiftUI
import YouTubePlayerKit

struct ContentView: View {

    @StateObject
    var youTubePlayer: YouTubePlayer = "https://youtube.com/watch?v=psL_5RIBqnY"

    var body: some View {
        YouTubePlayerView(self.youTubePlayer) { state in
            // Overlay ViewBuilder closure to place an overlay View
            // for the current `YouTubePlayer.State`
            switch state {
            case .idle:
                ProgressView()
            case .ready:
                EmptyView()
            case .error(let error):
                Text(verbatim: "YouTube player couldn't be loaded")
            }
        }
    }

}

What is the expected behavior?

I would like to use the closure state initializer of YouTubePlayerView as expected.

Thank you in advance for your time and consideration.

SvenTiigi commented 1 month ago

Hi @HPezz,

could you please provide more information related to this issue for example a minimal reproducible example project?

I've tested the same code inside the example application of the YouTubePlayerKit and couldn't reproduce the described issue (see attached screencast) with Xcode 15.4 and an iPad Air 11-inch (M2) simulator running iPadOS 17.5

https://github.com/user-attachments/assets/06dedb0d-2d7f-4b0a-93c1-7fa10f47e84f

My blind guess is that if the YouTube Player gets stuck in the idle state, the underlying YouTube Player iFrame API JavaScript may not have been loaded properly since the YouTube Player iFrame API JavaScript onReady callback is the trigger which updates the state (https://developers.google.com/youtube/iframe_api_reference#Events).

https://github.com/SvenTiigi/YouTubePlayerKit/blob/5ade0dfbabaf7456069b4e8f604de261bf6d7c67/Sources/YouTubePlayer.swift#L183-L199

acosmicflamingo commented 1 month ago

While on the topic, should I create a PR to merge this? https://github.com/acosmicflamingo/YouTubePlayerKit/commit/eac25effa3b2239a26778ea21b1c4bf9c7987544

The problem is that once the player state becomes .error, the next time you cue a new video, case .onReady won't ever be accessed but instead it'l go to case .onStateChange, so anything subscribed to playerStateSubject publisher will not ever be told that it's ready again.

SvenTiigi commented 1 month ago

Good find @acosmicflamingo 👍 Yes a PR is very welcome!

acosmicflamingo commented 1 month ago

Done! https://github.com/SvenTiigi/YouTubePlayerKit/pull/98

@SvenTiigi let me know if you want me to modify the PR in any way :)

SvenTiigi commented 3 weeks ago

Closing this issue due to inactivity. Feel free to re-open the issue at any time.