Get media link with AVFullScreenViewController iOS 8

I'm trying to get links to a video when something starts playing (like any video on YouTube).

First I will catch when the video starts playing

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoStarted:) name:@"UIWindowDidBecomeVisibleNotification" object:nil]; 

then with a delay try to get the link

 -(void)videoStarted:(NSNotification *)notification { NSLog(@"notification dic = %@", [notification userInfo]); [self performSelector:@selector(detectModal) withObject:nil afterDelay:2.5f]; } -(void)detectModal { CUViewController *controller = (CUViewController *)[appDelegate.window rootViewController].presentedViewController; NSLog(@"Presented modal = %@", [appDelegate.window rootViewController].presentedViewController); if(controller && [controller respondsToSelector:@selector(item)]) { id currentItem = [controller item]; if(currentItem && [currentItem respondsToSelector:@selector(asset)]) { AVURLAsset *asset = (AVURLAsset *)[currentItem asset]; if([asset respondsToSelector:@selector(URL)] && asset.URL) [self newLinkDetected:[[asset URL] absoluteString]]; NSLog(@"asset find = %@", asset); } } else { for (UIWindow *window in [UIApplication sharedApplication].windows) { if ([window.rootViewController.presentedViewController isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) { controller = (CUViewController *)window.rootViewController.presentedViewController; for(int i = 0; i < [controller.view.subviews count]; i++) { UIView *topView = [controller.view.subviews objectAtIndex:i]; NSLog(@"top view = %@", topView); for(int j = 0; j < [topView.subviews count]; j++) { UIView *subView = [topView.subviews objectAtIndex:j]; NSLog(@"sub view = %@", subView); for (int k = 0; k < [subView.subviews count]; k++) { CUPlayerView *subsubView = (CUPlayerView *)[subView.subviews objectAtIndex:k]; NSLog(@"sub sub view = %@ class = %@", subsubView, NSStringFromClass([subsubView class])); if([NSStringFromClass([subsubView class]) isEqualToString:@"AVVideoLayerView"]) { NSLog(@"player view controller = %@", subsubView.playerController); CUPlayerController *pController = subsubView.playerController; NSLog(@"item = %@", [pController player]); CUPlayerController *proxyPlayer = pController.playerControllerProxy; if(proxyPlayer) { AVPlayer *player = [proxyPlayer player]; NSLog(@"find player = %@ chapters = %@", player, proxyPlayer.contentChapters); break; } } } } } } } } } 

CUViewController, CUPlayerView, CUPlayerController - fakes it and looks like this

 @interface CUPlayerController : UIViewController @property(nonatomic, retain) id playerControllerProxy; @property(nonatomic, retain) id player; @property(nonatomic, retain) id item; - (id)contentChapters; @end 

all right up to this line

 NSLog(@"find player = %@ chapters = %@", player, proxyPlayer.contentChapters); 
Player

always zero. Maybe there is an easier way to get a link to the media?

+7
ios objective-c avplayer
source share
1 answer

First, I would like to focus on your AVPlayer that plays AVPlayerItem. The AVPlayerItem object contains a reference to the AVASset object and presentation parameters for this resource. When you use the playerWithURL: AVPlayer method, it automatically creates AVPlayerItem, a supported asset, which is a subclass of AVAsset called AVURLAsset. AVURLAsset has a URL property. Therefore, if you use this, you can get the NSURL of the current element. Here is an example of a function for obtaining a URL:

 -(NSURL *)urlOfCurrentlyPlayingInPlayer:(AVPlayer *)player{ // get current asset AVAsset *currentPlayerAsset = player.currentItem.asset; // make sure the current asset is an AVURLAsset if (![currentPlayerAsset isKindOfClass:AVURLAsset.class]) return nil; // return the NSURL return [(AVURLAsset *)currentPlayerAsset URL]; } 

I think this is just a question of how you fit this thing around your code. Hope this helps.

0
source share

All Articles