如何以编程方式制作 UIWebView pause/play 视频内容

How do I make UIWebView pause/play video content programmatically

我正在让我的应用程序通过 UIWebView 加载 YouTube 视频,但需要能够让它在特定时间暂停和播放。我已经查看了此处的所有答案,其中 none 个有效。我试过这个:

webView.stringByEvaluatingJavaScriptFromString("var videos = document.querySelectorAll(\"video\"); for (var i = videos.length - 1; i >= 0; i--) { videos[i].pause(); };")

但这什么也做不了。如果重要的话,我正在通过这个加载视频:

webView.allowsInlineMediaPlayback = true
webView.mediaPlaybackRequiresUserAction = false    

let embededHTML = "<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width='0' height='0' src='http://www.youtube.com/embed/\(videoID)?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'></body></html>"

webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().resourceURL)

作为 not_a_bot 链接,使用 ytplayer.pauseVideo()ytplayer.playVideo() 效果很好。

webView.stringByEvaluatingJavaScriptFromString("ytplayer.pauseVideo()")

webView.stringByEvaluatingJavaScriptFromString("ytplayer.playVideo()")