这就需要在webview页面里面往js里注入音视频播放停止的js代码:
js代码:var videos = document.getElementsByTagName('video');function pauseVideo(){var len = videos.length;for(var i=0;i<len;i++){videos[i].pause();}};var audios = document.getElementsByTagName('audio');function pauseAudio(){var len = audios.length;for(var i=0;i<len;i++){ audios[i].pause();}};
// 注入网页停止播放音乐的js代码
WKWebViewConfiguration *configuretion = [[WKWebViewConfiguration alloc] init];
WKUserScript * pauseJS = [[WKUserScript alloc]initWithSource:[self pauseVideoAndAudioJSCode] injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *userContent = [[WKUserContentController alloc]init];
[userContentaddUserScript:pauseJS];
configuretion.userContentController= userContent;
最后再页面已经消失的地方调用js方法:
mWebView.evaluateJavaScript("pauseVideo()") { (data, error)in }
mWebView.evaluateJavaScript("pauseAudio()") { (data, error)in
网友评论