WKWebView嵌入的视频在发布后继续播放声音

问题描述 投票:3回答:2

我正在实施一项用于通过网络视图播放视频文件的服务。我从UIWebView迁移到WKWebView,并尝试使用它播放Youtube和Coub视频。几乎一切正常,在iOS 8中没有崩溃,但是在释放WKWebView并将其从屏幕上删除之后,视频声音会持续播放一段时间(在某些情况下大约为45秒)。

我找不到停止视频声音的方法。我试图用媒体播放器捕获系统通知,但没有成功。

有什么方法可以在WKWebView中停止声音或视频吗?

WKWebview配置是下一个:

//javascript for configurate video viewport (not full screen)
NSString *jScript = [NSString stringWithFormat:@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=%d, height=%d, initial-scale=1, maximum-scale=1'); document.getElementsByTagName('head')[0].appendChild(meta);", (int)VIEW_WIDTH, (int)VIEW_HEIGHT ];

WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];

WKWebViewConfiguration *config    = [WKWebViewConfiguration new];
config.mediaPlaybackAllowsAirPlay = YES;
config.userContentController      = wkUController;

_wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT) configuration:config];
[_wkWebView setBackgroundColor:RGB(0x0a0a0a)];
[_wkWebView setNavigationDelegate:self];
[self insertSubview:_wkWebView atIndex:0];
ios video youtube ios8 wkwebview
2个回答
7
投票

您可以加载空白页[NSURL URLWithString:@"about:blank"]

虽然可能有更好的解决方案

UPDATE:已在iOS 8.3中修复


0
投票

我建议使用这种方法:

https://stackoverflow.com/a/44829559/6007974

注入JS代码对我来说就像是魅​​力!

let script = "var vids = document.getElementsByTagName('video'); for( var i = 0; i < vids.length; i++ ){vids.item(i).pause()}"
self.webView.evaluateJavaScript(script, completionHandler:nil)
© www.soinside.com 2019 - 2024. All rights reserved.