How to get the url of currently playing video in UIWebview

三世轮回 提交于 2019-11-29 04:06:06

问题


Is there any way to get the link of currently playing video.I am loading m.youtube.com .For some videos it is not even entering the delegates.I tried using a NStimer as well.But for some videos it is not the clicked url


回答1:


There is a hacky way of doing it by listening for the AVPlayerItemBecameCurrentNotification notification. This notification is fired when a UIWebView shows the media player, and it sends an AVPlayerItem as the notification's object.

For example:

[[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];
}

This works for any video (and audio). However, I noticed you mentioned YouTube - it's worth pointing out Apple WILL reject your app if it has the ability to download YouTube videos AT ALL, because it's against YouTube's Terms of Service.




回答2:


This one is for swift version:

//Add NSNotificationcenter for current playing song

override func viewDidAppear(_ animated: Bool) {
    NotificationCenter.default.addObserver(self, selector:#selector(playerItemBecameCurrent), name:NSNotification.Name(rawValue: "AVPlayerItemBecameCurrentNotification"), object:nil)
}

//Fetch path from notificationcenter

@objc func playerItemBecameCurrent(notification:NSNotification){
    let playerItem:AVPlayerItem = notification.object as! AVPlayerItem
    let asset:AVURLAsset=playerItem.asset as! AVURLAsset
    let url = asset.url
    let path = url.absoluteString
    print(path)

}


来源:https://stackoverflow.com/questions/26154823/how-to-get-the-url-of-currently-playing-video-in-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!