我正在使我的应用程序通过UIWebView加载youtube视频,但需要能够在某些时间暂停和播放。我看了这里所有的答案,没有一个是有效的。我试过了:
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()")