Detecting and intercepting video playback in UIWebView

 ̄綄美尐妖づ 提交于 2019-12-05 11:49:46

There is a hacky way of finding the URL 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 it is worth nothing that this is fired after the media player has loaded, so you can't stop the player from launching at this point (if that was your intention).

If the above solution doesn't work for anyone then try listening to AVPlayerItemNewAccessLogEntry rather than AVPlayerItemBecameCurrentNotification. AVPlayerItemNewAccessLogEntry seems to be called afterwards and is where I was able to set the info.

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