Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to dismiss YouTubePlayerViewController when video ends? #113

Open
tobedev opened this issue Jan 3, 2025 · 2 comments
Open

How to dismiss YouTubePlayerViewController when video ends? #113

tobedev opened this issue Jan 3, 2025 · 2 comments
Labels
question Further information is requested

Comments

@tobedev
Copy link

tobedev commented Jan 3, 2025

Am using UIKit and having a hard time trying to auto. dismiss the YouTubePlayerViewController when video ends, as there's no delegate methods implemented, and the only way documented is by using '.objectWillChange.sink { }' which is SwiftUI only I think?

Any hints/input would be greatly appreciated.
Thank you.

@SvenTiigi
Copy link
Owner

Hi @tobedev,

You can use the playbackStateSubject, an instance of PassthroughSubject from the Combine framework, which is compatible with both UIKit and SwiftUI.

let cancellable = youTubePlayerViewController
    .player
    .playbackStateSubject
    .sink { [weak youTubePlayerViewController] playbackState in
        guard playbackState == .ended else {
            return
        }
        youTubePlayerViewController?.dismiss(animated: true)
    }
    
self.present(youTubePlayerViewController, animated: true)

Note

Please ensure that you retain the AnyCancellable from the sink operation in a stored property or a set of cancellables, otherwise, the subscription will be cancelled.

@SvenTiigi SvenTiigi added the question Further information is requested label Jan 3, 2025
@Jean-PhilippeDESCAMPS
Copy link

Hello, in your example project, I have added the following code:

cancellable = self.youTubePlayer.playbackStatePublisher.sink(receiveValue: { statePublisher in
                print("\(statePublisher)")
            })

However, nothing happens at the end of the video.
Do you know how to fix that ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants