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):
- ChannelVideosViewController 将视频缩略图显示为带有 UITapGestureRecognizer 的 UIView,后者又转至我的 PlayerViewController 并传递来自 API 调用的 videoId
一个PlayerViewController如下:
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])
}
使用上面的代码,当我按下 "big red button" 时,帮助库成功加载视频并全屏播放它们,但我想在进入视图后直接自动播放视频。有没有办法做到这一点?
来自 YouTube docs 的 "autoplay":1
似乎不适合 iOS.
youtubePlayerView.playVideo()
什么都不做
基于此 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()
}
}
在我的情况下效果很好。
我在使用 Google/YouTube 提供的 youtube-ios-player-helper
pod 自动播放视频时遇到问题。这是我的应用程序的相关部分(iOS 10,Swift 3):
- ChannelVideosViewController 将视频缩略图显示为带有 UITapGestureRecognizer 的 UIView,后者又转至我的 PlayerViewController 并传递来自 API 调用的 videoId
一个PlayerViewController如下:
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]) }
使用上面的代码,当我按下 "big red button" 时,帮助库成功加载视频并全屏播放它们,但我想在进入视图后直接自动播放视频。有没有办法做到这一点?
-
来自 YouTube docs 的
"autoplay":1
似乎不适合 iOS.youtubePlayerView.playVideo()
什么都不做
基于此 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()
}
}
在我的情况下效果很好。