如何检测AVplayer并从WKWebView获取当前视频的URL?

Vir*_*vek 8 ios avplayer swift wkwebview

我正在使用下面的代码从UIWebView提取URL :它工作正常,但是,用于WKWebView的相同代码不再起作用。谁能帮我?在WKWebView中播放的视频不是全屏播放,而是Inlineplacyback。

我的代码是:

 NotificationCenter.default.addObserver(self, selector: #selector(self.playerItemBecameCurrent(_:)), name: NSNotification.Name("AVPlayerItemBecameCurrentNotification"), object: nil)

 @objc func playerItemBecameCurrent(_ sender : NSNotification){
    let playerItem: AVPlayerItem? = sender.object as? AVPlayerItem
    if playerItem == nil {
        print("player item nil")
        return
    }
    // Break down the AVPlayerItem to get to the path
    let asset = playerItem?.asset as? AVURLAsset
    let url: URL? = asset?.url
    let path = url?.absoluteString

    print(path!,"video url")
}
Run Code Online (Sandbox Code Playgroud)

回应网址:

https://r2---sn-po4g5uxa-5hql.googlevideo.com/videoplayback?txp=5531432&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Crypto%2Cmime%2Cmm 2Cms%2Cmv%2Cpcm2%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&IP = 103.37.181.55&ratebypass = YES&ID =邻AM9UWIaxopyYZX4gikGuswG8EMi3dhH_PPBMIqY5cbXj&到期= 1554400796& C = MWEB&fvip = 4&initcwndbps = 481250&ipbits = 0&MIME =视频%2Fmp4&DUR = 60.093&LMT = 1554142002789460&键= yt6&MT = 1554379078&ITAG = 18源= YouTube和GIR = YES&requiressl =是&签名= 6C68366FC249958BB8E95A5D88074FF8BCB99745.DA113E66DD0B46863BAE52DAA3CAB31FD141F0E5&CLEN = 2708520&毫米= 31%2C29&MN = SN-po4g5uxa-5hql%2Csn-cvh7knek&EI = vPGlXPOWHIWD8QOO1KBo&MS = AU%2Crdu&PCM2 =否PL = 24&MV = M&CPN = I9d32bNmeq3kf0jn&cver = 2.20190403&ptk = youtube_none&pltype = contentugc

它是视频URL,而不是网页URL,所以请帮助我。谢谢。

MCM*_*tan 8

这是一种破解,但是我找到了实现此目标的唯一方法。

首先将自己设置为WKWebView导航委托:

self.webView?.navigationDelegate = self
Run Code Online (Sandbox Code Playgroud)

现在听所有导航更改,并保存请求的URL:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if let urlStr = navigationAction.request.url?.absoluteString {
            //Save presented URL
            //Full path can be accessed via self.webview.url
        }

        decisionHandler(.allow)
    }
Run Code Online (Sandbox Code Playgroud)

现在,您只需要知道新屏幕何时可见,并使用您保存的URL(即可知道新可见屏幕的视频URL)。

您可以通过侦听UIWindowDidBecomeVisibleNotification通知来做到这一点:

NotificationCenter.default.addObserver(self, selector: #selector(windowDidBecomeVisibleNotification(notif:)), name: NSNotification.Name("UIWindowDidBecomeVisibleNotification"), object: nil)
Run Code Online (Sandbox Code Playgroud)

然后检查导航窗口是否不是您的窗口,这意味着确实打开了一个新屏幕:

@objc func windowDidBecomeVisibleNotification(notif: Notification) {
        if let isWindow = notif.object as? UIWindow {
            if (isWindow !== self.view.window) {
            print("New window did open, check what is the currect URL")
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


Zap*_*hod 3

您可以尝试在此处所示的内容中注入JS WKWebViewhttps ://paulofierro.com/blog/2015/10/12/listening-for-video-playback-within-a-wkwebview