13
votes

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?

6
Anyone found this issue in iOS 9/10?Itachi

6 Answers

40
votes

Do integrate following code to sort out the problem.

    -(void)viewWillDisappear:(BOOL)animated
    {
        [webView loadHTMLString:nil baseURL:nil];
    }
17
votes

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];
2
votes

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

2
votes

Use the following javascript in WKWebview to pause the video player in swift

wkWebView.evaluateJavaScript("var videos = document.querySelectorAll(\"video\"); for (var i = videos.length - 1; i >= 0; i--) { videos[i].pause(); };", completionHandler: nil)

to stop the video player use:

wkWebView.loadHTMLString("", baseURL: nil)
1
votes

In Swift method :

override public func viewDidDisappear(animated: Bool) {
    super.viewDidAppear(animated)
    self.webView.loadHTMLString("", baseURL: nil)
}

thanks @alloc_iNit

1
votes

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]