WKWebView嵌入视频发布后一直播放声音 [英] WKWebView embed video keeps playing sound after release
问题描述
我正在实施一项使用 web 视图播放视频文件的服务.我从 UIWebView 迁移到 WKWebView,并尝试用它播放 Youtube 和 Coub 视频.几乎一切正常,在 iOS 8 中没有崩溃,但是在 WKWebView 释放并从屏幕上移除后,视频声音会持续播放一段时间(在某些情况下大约 45 秒).
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 everything is okay, no crashes in iOS 8, but after WKWebView released and removed from screen, video sound keeps playing for a while (in some cases about 45 seconds).
我找不到停止视频声音的方法.我试图用媒体播放器捕捉系统通知,但没有成功.
I can't find the way to stop sound of video. I tried to catch system notifications with a media player, but no success.
WKWebView 有没有办法停止声音或视频?
Is there any way to stop sound or video in WKWebView?
WKWebview 配置如下:
WKWebview configuration is next:
//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];
推荐答案
可以加载空白页 [NSURL URLWithString:@"about:blank"]
不过可能有更好的解决方案
There is probably better solution though
更新:在 iOS 8.3 中修复
UPDATE: fixed in iOS 8.3
这篇关于WKWebView嵌入视频发布后一直播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!