13

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?

Jack
  • 13,571
  • 6
  • 76
  • 98
Manni
  • 11,108
  • 15
  • 49
  • 67

7 Answers7

42

Do integrate following code to sort out the problem.

    -(void)viewWillDisappear:(BOOL)animated
    {
        [webView loadHTMLString:nil baseURL:nil];
    }
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
17

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];
Alena
  • 1,080
  • 12
  • 20
  • 2
    I prefer this answer to the other since I'd like the user to return to the webpage in the state they left it. (Well, with the video paused instead of playing s fine.) – Fls'Zen Dec 08 '13 at 15:38
  • Generally in applications, videos play from very beginning. It depends upon an application's feature to resume a video or to play from beginning. Though I appreciate Alena's answer. Thanks for sharing. :) – alloc_iNit Mar 19 '15 at 09:07
2

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

slashlos
  • 913
  • 9
  • 17
2

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)
Lijith Vipin
  • 1,870
  • 21
  • 29
1

In Swift method :

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

thanks @alloc_iNit

YanSte
  • 10,661
  • 3
  • 57
  • 53
1

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]
Arif Fikri Abas
  • 836
  • 5
  • 9
  • @Starwave I understand your concern, that's quite a fair point, I guess it depends on what's best for your product, in my case, I dont want user to be presented with blank page when they tap back/close the view presented on top. cheers :) – Arif Fikri Abas Apr 25 '19 at 03:14
0

And if you need to have it in the same subclass instead of the vc.

override func willMove(toWindow newWindow: UIWindow?) {
    if newWindow == nil {
        webView.evaluateJavaScript("var videos = document.querySelectorAll(\"video\"); for (var i = videos.length - 1; i >= 0; i--) { videos[i].pause(); };", completionHandler: nil)
    }
}
Pau Ballada
  • 1,491
  • 14
  • 13