问题
In my iPad App I have a modal view (UIViewController with modal presentation style UIModalPresentationPageSheet)
Inside the view is a UIWebView with a HTML page and an embedded YouTube-Video. If I start the video and close the view, the video doesn't stop. The audio continues and you can see a small "play icon" next to the "battery icon" in the status bar.
How can I stop the video?
回答1:
Do integrate following code to sort out the problem.
-(void)viewWillDisappear:(BOOL)animated
{
[webView loadHTMLString:nil baseURL:nil];
}
回答2:
Also you can try to pause video:
NSString *script = @"var videos = document.querySelectorAll(\"video\"); for (var i = videos.length - 1; i >= 0; i--) { videos[i].pause(); };";
[webView stringByEvaluatingJavaScriptFromString:script];
回答3:
In Swift method :
override public func viewDidDisappear(animated: Bool) {
super.viewDidAppear(animated)
self.webView.loadHTMLString("", baseURL: nil)
}
thanks @alloc_iNit
回答4:
Swift 4
// Halt anything in progress
(contentViewController as! WebViewController).webView.loadHTMLString("Yoink://", baseURL: nil)
needed in a windowController's windowShouldClose() method when I close a window
回答5:
I saw there are many suggestions with loading blank page. That is really bad approach.
It will cause your webview to be blank if, let say you push you another view on top of it.
Most simple solution is to reload the view with:
Swift
webView.reload()
Obj-C
[webView reload]
来源:https://stackoverflow.com/questions/7038100/stop-video-in-uiwebview