WKWebView embed video keeps playing sound after release

前端 未结 3 1861

I\'m implementing a service for playing video files with a webview. I migrated from UIWebView to WKWebView, and trying to play Youtube and Coub videos with it. Almost everyt

相关标签:
3条回答
  • 2021-01-03 02:22

    You can load blank page [NSURL URLWithString:@"about:blank"]

    There is probably better solution though

    UPDATE: fixed in iOS 8.3

    0 讨论(0)
  • 2021-01-03 02:22

    I'd recommend stopping video with JS code. For example

    let stopVideoScript = "var videos = document.getElementsByTagName('video'); for( var i = 0; i < videos.length; i++ ){videos.item(i).pause()}"
    self.webView.evaluateJavaScript(stopVideoScript, completionHandler:nil)
    
    0 讨论(0)
  • 2021-01-03 02:42

    I had a scenario where I had a video playing on the WKWebView, and below the video, was a list of related videos. If you press one of those related videos, the main video player won't reload the video, even though I had made new load request to the webView with the correct link inside. The only thing which worked for me was to clear all the webView cache before loading the new video:

    private func removeWebViewCache(completion: @escaping () -> ()) {
        
        let dataStore = WKWebsiteDataStore.default()
        dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
            dataStore.removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: records, completionHandler: completion)
        }
    }
    

    Then, you just call this function first, and then, when it finishes, call your new load request, or simply do any other action that you need:

    self.removeWebViewCache { [weak self] in
        guard let self = self else { return }
        //self.loadNewVideo()
        ...
    }
    
    0 讨论(0)
提交回复
热议问题