Stop video in UIWebView

北城以北 提交于 2019-11-30 12:47:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!