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
Asked
Active
Viewed 4,348 times
2 Answers
13
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.

ttarik
- 3,824
- 1
- 32
- 51
-
Thank you ,Its working.Still I am not able to get the current url corresponding to the video.Is there any way ? – Jeff Oct 02 '14 at 06:07
-
@Jeff What do you mean? My example code above shows getting the URL as a NSURL and NSString. This is the URL of the video that is being played. – ttarik Oct 02 '14 at 06:10
-
I am getting encoded url.I am asking about the youtube link of the vide – Jeff Oct 02 '14 at 06:15
-
When you click on a video page, `shouldStartLoadWithRequest:` is called just like every other web page. The request contains the URL, which will be something like `http://m.youtube.com/?#/watch?v=(videoid)` – ttarik Oct 02 '14 at 06:18
-
for some videos it's not entering in this delegate.that is my problem – Jeff Oct 02 '14 at 06:22
-
@Jeff Can you give any more information about that? I tested it just then and every video page that I tested called `shouldStartLoadWithRequest:` – ttarik Oct 02 '14 at 06:27
-
Sorry dude, I am not able to give the URL because of the privacy of the app.I am sorry for that.For some videos it is not entering.I think if you click several links, you can reproduce he same – Jeff Oct 02 '14 at 06:37
-
any solution for the dilemma ? – Jeff Oct 02 '14 at 06:57
-
@ev0lution I am trying to just update the video volume using a similar approach you mentioned, do you think it is legit to do so? I mean, compliant with Apple and Youtube's terms? – RainCast May 19 '16 at 21:22
-
@ev0lution, my post is here in case you'd like to post your opinion: http://stackoverflow.com/questions/37290108/how-to-play-youtube-video-in-uiwebview-muted/37315071#37315071 – RainCast May 19 '16 at 21:23
-
Not working now in WKWebView can you help me please . – Virani Vivek Mar 28 '19 at 09:24
-
https://stackoverflow.com/questions/55377677/how-to-detect-avplayer-and-get-url-of-current-video-from-wkwebview my question is this help me . – Virani Vivek Mar 30 '19 at 12:44
0
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)
}

khusboo suhasini
- 293
- 2
- 16
-
1Is this not work in WKWebView please any suggestion how to do that in WKWebView ? – Virani Vivek Apr 03 '19 at 06:36