从AVFullScreenViewController iOS 8获取媒体链接

时间:2015-07-11 12:05:06

标签: ios objective-c avplayer

我想在某些内容开始播放时尝试获取视频链接(例如任何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 = %@", player, proxyPlayer.contentChapters);

玩家永远是零。也许有更简单的方式来获取媒体链接?

1 个答案:

答案 0 :(得分:0)

首先,我想专注于播放AVPlayerItem的AVPlayer。 AVPlayerItem对象携带对该资产的AVAsset对象和表示设置的引用。当您使用AVPlayer的 playerWithURL:方法时,它会自动创建由资产支持的AVPlayerItem,该资产是名为AVURLAsset的AVAsset的子类。 AVURLAsset具有URL属性。因此,如果您使用它,您可以获得当前播放项目的NSURL。以下是获取网址的示例功能:

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

我认为这只是关于你如何在代码中使用这个东西的问题。希望这会有所帮助。