How to make software UIWebView to pause / play video

I am making my app download YouTube videos via UIWebView but need to be able to pause and play at specific times. I've looked at all the answers here and none of them work. I've tried this:

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

      

But it just doesn't do anything. If it matters, I download the video:

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)

      

+3


source to share


1 answer


Like not_a_bot , using ytplayer.pauseVideo()

and ytplayer.playVideo()

works fine.

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

      



or

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

      

+3


source







All Articles