猿问
下载APP

如何检测AV播放器并从WKWebView获取当前视频的网址?

如何检测AV播放器并从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")}

回复网址:

https://r2---sn-po4g5uxa-5hql.googlevideo.com/videoplayback?txp=5531432&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn% 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

它的视频网址不是网页网址,请帮我解决这个问题。谢谢。


弑天下
浏览 95回答 3
3回答

慕粉4167745

这是一种黑客行为,但这是我发现实现这一目标的唯一方法。首先将自己设置为WKWebView导航委托:self.webView?.navigationDelegate = self现在听取所有导航更改,并保存请求的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)     }现在,您只需要知道新屏幕何时可见,并使用您保存的URL(了解新可见屏幕的视频URL)。您可以通过收听UIWindowDidBecomeVisibleNotification通知来执行此操作:NotificationCenter.default.addObserver(self, selector: #selector(windowDidBecomeVisibleNotification(notif:)), name: NSNotification.Name("UIWindowDidBecomeVisibleNotification"), object: nil)然后检查导航窗口是否不是您的窗口,这意味着新的屏幕确实打开了:@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")             }         }     }

元芳怎么了

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

ibeautiful

使用Swift您可以使用以下代码从webview的网址获取html内容let&nbsp;docString&nbsp;=&nbsp;webView.stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML")这个案例你会得到整个html内容,然后在html字符串中查找href链接let&nbsp;regex&nbsp;=&nbsp;try!&nbsp;NSRegularExpression(pattern:&nbsp;"<a[^>]+href=\"(.*?)\"[^>]*>")let&nbsp;range&nbsp;=&nbsp;NSMakeRange(0,&nbsp;docString.characters.count)let&nbsp;matches&nbsp;=&nbsp;regex.matches(in:&nbsp;docString,&nbsp;range:&nbsp;range)for&nbsp;match&nbsp;in&nbsp;matches&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;htmlLessString&nbsp;=&nbsp;(docString&nbsp;as&nbsp;NSString).substring(with:&nbsp;match.rangeAt(1)) &nbsp;&nbsp;&nbsp;&nbsp;print(htmlLessString)}检查它是否是youtube url使用正则表达式:“@ https?://(www。)?youtube.com/。[^ \ s。,”\'] + @ i“另一种实现这一目标的方法创造性思考!你可以打个电话来获取网址。使用php,.net等网络语言似乎很容易。使用PHP获取网页内所有网址的代码(使用适合您的任何语言)$url="http://wwww.somewhere.com";$data=file_get_contents($url);$data&nbsp;=&nbsp;strip_tags($data,"<a>");$d&nbsp;=&nbsp;preg_split("/<\/a>/",$data);foreach&nbsp;(&nbsp;$d&nbsp;as&nbsp;$k=>$u&nbsp;){ &nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;strpos($u,&nbsp;"<a&nbsp;href=")&nbsp;!==&nbsp;FALSE&nbsp;){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$u&nbsp;=&nbsp;preg_replace("/.*<a\s+href=\"/sm","",$u); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$u&nbsp;=&nbsp;preg_replace("/\".*/","",$u); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;$u."\n"; &nbsp;&nbsp;&nbsp;&nbsp;}}如果它是youtube网址,逐个检查。$sText&nbsp;=&nbsp;&nbsp;"Check&nbsp;out&nbsp;my&nbsp;latest&nbsp;video&nbsp;here&nbsp;http://www.youtube.com/?123";preg_match_all('@https?://(www\.)?youtube.com/.[^\s.,"\']+@i',&nbsp;$sText,&nbsp;$aMatches);var_dump($aMatches);如果您想检查示例应用程序是否使用相同的方法,请获取Web调试代理并进行挖掘上述许多解释都来自其他网站。我希望它总结你的需要!快乐的编码!
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答