从 UIWebView 中的 AVPlayer 获取视频 URL [英] Get video URL from AVPlayer in UIWebView

查看:26
本文介绍了从 UIWebView 中的 AVPlayer 获取视频 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此方法检测从 UIWebView 播放的当前视频的 URL:

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) ->布尔值{NotificationCenter.default.addObserver(self, selector: #selector(playerItemBecameCurrent(notification:)), name: NSNotification.Name("AVPlayerItemBecameCurrentNotification"), object: nil)//urlTextField.text = webView.request?.url?.absoluteString返回真}func playerItemBecameCurrent(通知:通知){让 playerItem: AVPlayerItem?=notification.object 作为?AVPlayerItem如果 playerItem == nil {返回}//分解 AVPlayerItem 到路径让资产:AVURLAsset?= (playerItem?.asset as?AVURLAsset)让网址:网址?= 资产?.url让路径:字符串?= url?.absoluteString打印(路径!)}

此代码有效,但视频 URL 不包含任何视频文件扩展名:例如,这里是 YouTube 和 Vimeo 视频 ULR:

https://r5---sn-ab5l6nzr.googlevideo.com/videoplayback?mime=video%2Fmp4&requiressl=yes&clen=27257208&mn=sn-ab5l6nzr&mm=31&mv=m&mt=1499534990&key=yt6&ms=au&source=youtube&ip=107.182.226.163&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwnd%2Cinitcwndbps%2Cipsm%2Ciptime%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&安培; initcwndbps = 3565000和ID =邻ACG22m-TtwyC8tSG_AHUJk3qOPvhUm1X_-qRjy07pjIx&安培; EI = -hZhWZXcJ_aV8gS2tbCQDg&安培; LMT = 1497638262979584&安培; ratebypass = YES&安培; GIR = YES&安培; PL =25安培;到期= 1499556698&安培; DUR = 316.093&安培;签名= 94C8ED3E4AF64A7AC9F1B7E226454823B236CA55.C18F5F5D9D314B73EDD01DFC4EA3CEA1621AC6C7&安培; ipbits = 0&安培; ITAG = 18&安培; CPN = Wb3RPNfJRcFx5P-X和C = MWEB&安培; cver = 1.20170706&安培; PTK = TiTV2010%2Buser&安培; OID = 1nI2Jk44kRu4HLE6kGDUrA&安培;ptchn=E2VhHmwp3Y_wZvgz_LJvfg&pltype=内容

和 Vimeo :

<预> <代码> https://36skyfiregce-vimeo.akamaized.net/exp=1499539063~acl=%2F222646835%2F%2A~hmac=dc3caeb64501f537e2795ee8e121aed75a7eed9ba4e1b921fe92c757fe06b314/222646835/video/778109097,778109103,778109102,778109101/master.m3u8?f=破折号

解决方案

您可以像这样获取 MIME 类型和原始数据:

func playerItemBecameCurrent(notification:Notification) {守卫让 playerItem = notification.object as?AVPlayerItem,让资产 = playerItem.asset 作为?AVURLAsset 其他 {return}让 url = asset.url变量响应:URLResponse?= 零var request = URLRequest(url: url)request.httpMethod = "HEAD"尝试?NSURLConnection.sendSynchronousRequest(请求,返回:&响应)打印(响应?.mimeType)//不是最好的方法//让数据=尝试?数据(contentsOf:asset.url)}

然后您可以选择与 MIME 类型匹配的适当扩展名.您可以通过调用 AVURLAsset.audiovisualMIMETypes()

获取所有支持的类型

I am trying to detect URL of current video playing from UIWebView, with this method :

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
    {

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

        //urlTextField.text = webView.request?.url?.absoluteString

        return true
    }



  func playerItemBecameCurrent(notification:Notification)  {

        let playerItem: AVPlayerItem? = notification.object as? AVPlayerItem
        if playerItem == nil {
            return
        }
        // Break down the AVPlayerItem to get to the path
        let asset: AVURLAsset? = (playerItem?.asset as? AVURLAsset)
        let url: URL? = asset?.url
        let path: String? = url?.absoluteString
        print(path!)

    }

This code works but the video URL doesn't contain any video file extension: for example here is YouTube and Vimeo video ULRs :

https://r5---sn-ab5l6nzr.googlevideo.com/videoplayback?mime=video%2Fmp4&requiressl=yes&clen=27257208&mn=sn-ab5l6nzr&mm=31&mv=m&mt=1499534990&key=yt6&ms=au&source=youtube&ip=107.182.226.163&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&initcwndbps=3565000&id=o-ACG22m-TtwyC8tSG_AHUJk3qOPvhUm1X_-qRjy07pjIx&ei=-hZhWZXcJ_aV8gS2tbCQDg&lmt=1497638262979584&ratebypass=yes&gir=yes&pl=25&expire=1499556698&dur=316.093&signature=94C8ED3E4AF64A7AC9F1B7E226454823B236CA55.C18F5F5D9D314B73EDD01DFC4EA3CEA1621AC6C7&ipbits=0&itag=18&cpn=Wb3RPNfJRcFx5P-x&c=MWEB&cver=1.20170706&ptk=TiTV2010%2Buser&oid=1nI2Jk44kRu4HLE6kGDUrA&ptchn=E2VhHmwp3Y_wZvgz_LJvfg&pltype=content

and Vimeo :

https://36skyfiregce-vimeo.akamaized.net/exp=1499539063~acl=%2F222646835%2F%2A~hmac=dc3caeb64501f537e2795ee8e121aed75a7eed9ba4e1b921fe92c757fe06b314/222646835/video/778109097,778109103,778109102,778109101/master.m3u8?f=dash 

解决方案

You can get MIME type and raw data like this:

func playerItemBecameCurrent(notification:Notification)  {
        guard let playerItem = notification.object as? AVPlayerItem,
            let asset = playerItem.asset as? AVURLAsset else {return}
        let url = asset.url
        var response: URLResponse? = nil
        var request = URLRequest(url: url)
        request.httpMethod = "HEAD"
        try? NSURLConnection.sendSynchronousRequest(request, returning: &response)
        print(response?.mimeType)
        //not the best way to do it
        //let data = try? Data(contentsOf: asset.url)
    }

Then you can choose appropriate extension that matches MIME type. You can get all supported types by calling AVURLAsset.audiovisualMIMETypes()

这篇关于从 UIWebView 中的 AVPlayer 获取视频 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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