从AVFullScreenViewController iOS 8获取媒体链接

我想在某些内容开始播放时尝试获取视频链接(例如任何YouTube视频).

首先我抓住视频开始播放

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

然后延迟尝试获取链接

-(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 – 假的类,它看起来像这样

@interface CUPlayerController : UIViewController

@property(nonatomic,retain) id playerControllerProxy;
@property(nonatomic,retain) id player;
@property(nonatomic,retain) id item;

- (id)contentChapters;

@end

一切都还行,直到这一行

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

球员总是零.也许有更简单的方式来获得媒体链接

解决方法

首先,我想专注于播放AVPlayerItem的AVPlayer. AVPlayerItem对象携带对该资产的AVAsset对象和表示设置的引用.当您使用AVPlayer的playerWithURL:方法时,它会自动创建由资产支持的AVPlayerItem,该资产是名为AVURLAsset的AVAsset的子类. AVURLAsset具有URL属性.因此,如果您使用它,您可以获得当前播放项目的NSURL.以下是获取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];
}

我认为这只是你在代码中如何适应这个问题的问题.希望这可以帮助.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...