如何在UIWebview中获取当前播放视频的网址

时间:2014-10-02 05:15:50

标签: ios uiwebview youtube uiwebviewdelegate

有没有办法获取当前播放视频的链接。我正在加载 m.youtube.com 。对于某些视频,它甚至没有进入代表。我也试过使用NStimer。但是对于某些视频来说,它不是点击的网址

2 个答案:

答案 0 :(得分:11)

通过监听AVPlayerItemBecameCurrentNotification通知,有一种愚蠢的方法。当UIWebView显示媒体播放器时会触发此通知,并发送AVPlayerItem作为通知的对象。

例如:

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

适用于任何视频(和音频)。但是,我注意到你提到了YouTube - 如果有能力下载YouTube视频全部,那么值得指出Apple WILL 拒绝你的应用,因为它&# 39; s反对YouTube's Terms of Service

答案 1 :(得分:0)

这是快速版本:

///为当前播放的歌曲添加NSNotificationcenter

<test-1 class="ts">1
</test-1>
<test-2 class="ts">2
</test-2>
<test-3 class="ts">3
</test-3>
<style> 
[class*="ts"] {
  background: #ffff00;
}
</style>

//从通知中心获取路径

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