I have a YouTube video embedded in a UIWebView
. It plays just fine. When I segue to a different View Controller without pausing the video, however, it continues to play the audio. How do I stop it?
Code:
func loadYouTubeVideo() {
let youTubeLink: String = "https://www.youtube.com/embed/ac_om5HCjvg"
let width = 300
let height = 200
let frame = 50
let html:NSString = "<iframe width=\(width) height=\(height) src=\(youTubeLink) frameborder=\(frame) allowfullscreen></iframe>"
self.youTubeWebView.loadHTMLString(html as String, baseURL: nil)
}
I need a Swift answer. I've tried using Objective-C, but it doesn't translate unless I'm doing it wrong:
let script = "var videos = document.querySelectorAll(\"video\"); for (var i = videos.length - 1; i >= 0; i--) { videos[i].pause(); };"
youTubeWebView.stringByEvaluatingJavaScriptFromString(script)