6

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):

  • a ChannelVideosViewController that displays video thumbnails as UIViews with an UITapGestureRecognizer that in turn segues to my PlayerViewController and passes the videoId from the API call
  • a PlayerViewController as follows:

    var youtubePlayerView = YTPlayerView() // player for videos
    var youtubeVideoID = String() // videoId from API passed by ChannelVideosViewController
    
    override func viewDidLoad() {
    // ... skipping UI stuff
    
    view.addSubview(youtubePlayerView)
    youtubePlayerView.load(withVideoId: youtubeVideoID, playerVars: ["autoplay":1,"modestbranding":1,"showinfo":0,"rel":0])
    }
    

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?

  • "autoplay":1 from the YouTube docs doesn't seem to cut it for iOS.
  • youtubePlayerView.playVideo() doesn't do anything
tech4242
  • 2,348
  • 2
  • 23
  • 33
  • Hi! did the autoplay works with you? – Ne AS Nov 10 '16 at 10:12
  • @Llg Hi! Sadly I have gotten a bit sidetracked lately due to other projects but to answer your question - not really; I am able to play videos etc. but autoplay still doesn't work. Do you have a solution for this? Or are you having the same problem as well? – tech4242 Nov 10 '16 at 15:59
  • @Sadly I have the same problem :( – Ne AS Nov 10 '16 at 16:10
  • 1
    @Llg yeah.. maybe somebody can help at some point.. but better yet - I'll try to solve it myself :) I'll be coming back to this issue in the following weeks – tech4242 Nov 11 '16 at 23:00
  • can you please take a look on http://stackoverflow.com/questions/40550914/swift-ytplayerstate-unstarted-and-ytplayerstate-queued-not-working and help me if you have an idea about it? – Ne AS Nov 12 '16 at 13:25

2 Answers2

12

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.

atulkhatri
  • 10,896
  • 3
  • 53
  • 89
2

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:

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • Ok I gave this a try and now that I think about it - I stumbled across this a few days ago. `playerViewDidBecomeReady` is supposed to be a delegate method of YTPlayerView but Xcode can't find it. Maybe they removed it? I also can't post a Notification with the new API without having an NSNotification.Name and "Playback started" is not available - probably due to the missing delegate method of YTPlayerView. – tech4242 Sep 25 '16 at 15:32
  • I'm not sure if they remove it or not. But, you can double check on that by searching it or find an issue about that. – KENdi Sep 25 '16 at 15:35