tech4242
tech4242

Reputation: 2468

iOS app with YouTube v3 API and youtube-ios-player-helper can't autoplay videos

I am having a problem autoplaying videos with the youtube-ios-player-helper pod provided by Google/YouTube. Here is the relevant part of my app (iOS 10, Swift 3):

With the code above the helper library successfully loads the videos and plays them in fullscreen when I press the "big red button" but I want to autoplay the videos directly after I segue into the view. Is there a way to do this?

Upvotes: 6

Views: 7211

Answers (2)

atulkhatri
atulkhatri

Reputation: 11343

Conform to YTPlayerViewDelegate protocol like this:

self.youtubePlayer.delegate = self

Now use this delegate method to play video automatically when player is ready:

extension ViewController: YTPlayerViewDelegate {
    func playerViewDidBecomeReady(_ playerView: YTPlayerView) {
        self.youtubePlayer.playVideo()
    }
}

It works perfectly in my case.

Upvotes: 12

KENdi
KENdi

Reputation: 7771

Based on this github the autopilot doesn't work with the iOS player, as a workaround try this one:

(void)playerViewDidBecomeReady:(YTPlayerView *)playerView{ [[NSNotificationCenter defaultCenter] postNotificationName:@"Playback started" object:self]; [self.playerView playVideo]; }

For more information, check these threads:

Upvotes: 2

Related Questions