ios - 我如何使用 Youtube iOS API 寻找、转到某一秒?
<p><p>我在这里玩过 <code>YouTube-Player-iOS-Helper</code>:<a href="https://github.com/youtube/youtube-ios-player-helper" rel="noreferrer noopener nofollow">https://github.com/youtube/youtube-ios-player-helper</a> </p>
<p>但我不知道如何从某一秒开始嵌入到 iOS App 中的视频,有什么想法吗?</p>
<p>如你所见,我试过了</p>
<ol>
<li>添加一个HTML5键值对<code>"start": 100</code>,但是会崩溃</li>
<li>并尝试在视频加载后进行搜索,<code>pv.seekToSeconds(100, allowSeekAhead: true)</code>但也没有效果</li>
</ol>
<p>===</p>
<pre><code>class ViewController: UIViewController {
@IBOutlet weak var pv: YTPlayerView!
override func viewDidLoad() {
super.viewDidLoad()
pv.loadWithVideoId("M7lc1UVf-VE", playerVars: ["controls": 0, "playsinline": 1, "autohide": 1, "showinfo": 0, "modestbranding": 1]) //, "start": 100
NSNotificationCenter.defaultCenter().addObserver(self, selector: "receivedPlaybackStartedNotification:", name: "Playback started", object: nil)
}
func receivedPlaybackStartedNotification(notification: NSNotification) {
pv.seekToSeconds(100, allowSeekAhead: true)
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您需要等待 <code>playerViewDidBecomeReady</code> 委托(delegate)被调用。</p>
<p>在 Swift 中,就是:</p>
<pre><code>class MyViewVc: UIViewController, YTPlayerViewDelegate {
@IBOutlet weak var ytPlayer: YTPlayerView!
override func viewDidLoad() {
super.viewDidLoad()
self.ytPlayer.delegate = self
self.ytPlayer.load(withVideoId: "M7lc1UVf-VE")
}
func playerViewDidBecomeReady(_ playerView: YTPlayerView) {
self.ytPlayer.seek(toSeconds: 100, allowSeekAhead: true)
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 我如何使用 Youtube iOS API 寻找、转到某一秒?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/26827202/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/26827202/
</a>
</p>
页:
[1]