问题
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