如何检测AVplayer并从WKWebView获取当前视频的url? [英] How to detect AVplayer and get url of current video from WKWebView?

查看:346
本文介绍了如何检测AVplayer并从WKWebView获取当前视频的url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码从 UIWebView 中提取网址:它工作正常,但是,用于 WKWebView 的同一代码已无法正常工作.谁能帮我?在WKWebView中播放的视频不是全屏播放,而是Inlineplacyback.

I'm using below code to extract url from UIWebView: it is working fine but, this same code using for WKWebView it's not working anymore. Can anyone help me? The video playing in WKWebView is Inlineplacyback not in fullscreen.

我的代码是:

 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")
}

响应网址:

这是视频URL ,而不是网页URL,因此,请帮助我如何获取此URL. 谢谢.

It's video URL not Webpage URL so, please help me how to get this. Thanks.

推荐答案

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

This is kind of a hack, but the only way I found to accomplish this.

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

self.webView?.navigationDelegate = self

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

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).

Now you only need to know when does the new screen become visible, and use the URL you saved (To know the video URL of the new visible screen).

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

You can do this via listening to UIWindowDidBecomeVisibleNotification notification:

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")
            }
        }
    }

这篇关于如何检测AVplayer并从WKWebView获取当前视频的url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆