我正在实现一个播放视频文件的服务,使用webview。我从UIWebView迁移到了WKWebView,并尝试使用它来播放Youtube和Coub视频。几乎一切正常,在iOS 8中没有崩溃,但在WKWebView被释放并从屏幕上移除后,视频声音会继续播放一段时间(在某些情况下约为45秒)。
我找不到停止视频声音的方法。我尝试使用媒体播放器捕获系统通知,但没有成功。
有没有办法停止WKWebView中的声音或视频?
WKWebview配置如下:
我找不到停止视频声音的方法。我尝试使用媒体播放器捕获系统通知,但没有成功。
有没有办法停止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];
viewDidDisappear
中调用效果最好。 - Nick