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

2

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
Mandalay answered 22/9, 2016 at 14:36 Comment(5)
Hi! did the autoplay works with you?Cluny
@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?Mandalay
@Sadly I have the same problem :(Cluny
@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 weeksMandalay
can you please take a look on #40551414 and help me if you have an idea about it?Cluny
L
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.

Lubet answered 28/2, 2017 at 12:6 Comment(2)
sorry for the belated reply - I am currently not working on the app but this seems like a cool solution; marking as answered :)Mandalay
No problem...thank you for accepting the answer, it will work for sure :)Lubet
H
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:

Halfblood answered 25/9, 2016 at 13:59 Comment(2)
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.Mandalay
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.Halfblood

© 2022 - 2024 — McMap. All rights reserved.