在 WKWebView 中,我想获取在 iFrame 中播放的 vimeo 视频的播放时间。
我已经确认我可以使用以下代码播放 vimeo 视频,但我不知道如何获取视频已播放的秒数。
当前代码(iFrame)
@IBOutlet weak var vimeoWebView: WKWebView!
func showVimeo(iFrameURL: URL) {
let html = "<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes\"/></head><body><div><iframe src=\(iFrameURL) width=350 height=\(vimeoWebView.bounds.height) frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div></body><style>* { margin: 0; padding: 0; }html, body { width: 100%; height: 100%; }</style></html>"
vimeoWebView.loadHTMLString(html, baseURL: URL(string: "https://"))
vimeoWebView.contentMode = .scaleAspectFit
}
AVPlayer
NotificationCenter.default.addObserver(self, selector: #selector(didFinished), name: .AVPlayerItemDidPlayToEndTime, object: player?.currentItem)
func addPeriodicTimeObserver() {
let time = CMTime(seconds: 0.1, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
timeObserverToken = player?.addPeriodicTimeObserver(forInterval: time, queue: .main) { [weak self] time in
guard let time = self?.player?.currentTime() else { return }
print(time) // I want to get a value similar to this in an iframe implementation
}
}