iOS 带有 YouTube v3 API 和 youtube-ios-player-helper 的应用无法自动播放视频

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

我在使用 Google/YouTube 提供的 youtube-ios-player-helper pod 自动播放视频时遇到问题。这是我的应用程序的相关部分(iOS 10,Swift 3):

使用上面的代码,当我按下 "big red button" 时,帮助库成功加载视频并全屏播放它们,但我想在进入视图后直接自动播放视频。有没有办法做到这一点?

基于此 github 自动驾驶仪不适用于 iOS 播放器,作为解决方法尝试这个:

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

有关更多信息,请查看这些线程:

像这样遵守 YTPlayerViewDelegate 协议:

self.youtubePlayer.delegate = self

现在使用此委托方法在播放器准备就绪时自动播放视频:

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

在我的情况下效果很好。