Reputation: 6527
I am making my app load a youtube video through a UIWebView but need to be able to have it pause and play at certain times. I have looked at all the answers here and none of them are working. I have tried this:
webView.stringByEvaluatingJavaScriptFromString("var videos = document.querySelectorAll(\"video\"); for (var i = videos.length - 1; i >= 0; i--) { videos[i].pause(); };")
But that just does nothing. If it matters, I am loading the video by this:
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)
Upvotes: 3
Views: 2991