UIWebView中检测和拦截视频播放 [英] Detecting and intercepting video playback in UIWebView
问题描述
我想拦截 UIWebView 中的点击,然后使用视频的 URL.这怎么可能?我发现了一个有点类似的帖子,它指向了
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.
我正在尝试让代码在 iOS8 中运行
I am trying to get the code working in iOS8
推荐答案
有一种通过侦听 AVPlayerItemBecameCurrentNotification
通知来查找 URL 的黑客方法.当 UIWebView 显示媒体播放器时会触发此通知,并发送 AVPlayerItem
作为通知对象.
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.
例如:
[[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).
这篇关于UIWebView中检测和拦截视频播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!