在UIWebView中检测和拦截视频播放

And*_* Ho 4 objective-c video-streaming uiwebview ios uiwebviewdelegate

我想拦截UIWebView中的单击,然后使用视频的URL.这怎么可能?我发现了一个有点类似的帖子,它指向了

webView:shouldStartLoadWithRequest:navigationType:
Run Code Online (Sandbox Code Playgroud)

代表.我似乎无法通过此委托获取视频的加载网址.

我试图让代码在iOS8中运行

tta*_*rik 9

有一种通过监听AVPlayerItemBecameCurrentNotification通知来查找URL的hacky方法.当UIWebView显示媒体播放器时会触发此通知,并将其AVPlayerItem作为通知的对象发送.

例如:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemBecameCurrent:)
                                             name:@"AVPlayerItemBecameCurrentNotification"
                                           object:nil];


-(void)playerItemBecameCurrent:(NSNotification*)notification {
    AVPlayerItem *playerItem = [notification object];
    if(playerItem == nil) return;
    // Break down the AVPlayerItem to get to the path
    AVURLAsset *asset = (AVURLAsset*)[playerItem asset];
    NSURL *url = [asset URL];
    NSString *path = [url absoluteString];
}
Run Code Online (Sandbox Code Playgroud)

这适用于任何视频(和音频).然而,在媒体播放器加载后触发此功能并不值得,因此您无法阻止播放器在此时启动(如果这是您的意图).

  • 有人用WKWebView有更新的解决方案吗? (4认同)
  • @Valdimar我使用了.UIWindowDidResignKey通知。通过执行此操作,我无法获得AVPlayerItem的实例,但是可以使用此方法使视频全屏显示。从WKWebView / UIWebview全屏显示时,AVPlayerViewController会创建一个额外的UIWindow实例,因此您可以在原始UIWindow实例上使用“ makeKeyAndVisible”来压缩视频。 (2认同)