Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting and intercepting video playback in UIWebView

I would like to intercept a click in an UIWebView and then use the URL of the video. How is this possible? I found a somewhat similar post which pointed met to the

webView:shouldStartLoadWithRequest:navigationType:

delegate. I cant seem to get the loaded url of the video with this delegate.

I am trying to get the code working in iOS8

like image 431
Andrew Ho Avatar asked Dec 26 '22 04:12

Andrew Ho


1 Answers

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

like image 185
ttarik Avatar answered Dec 28 '22 10:12

ttarik