YasKuraishi / YKMediaPlayerKit

Painlessly and natively play YouTube, Vimeo, and .MP4, .MOV, .MPV, .3GP videos and fetch thumbnails on your iOS devices.
MIT License
348 stars 54 forks source link

Player view not presented , only audio #11

Open engmsaleh opened 9 years ago

engmsaleh commented 9 years ago

I have an app where I need to present mediaplayer from modal viewcontroller which is placed in UITabViewController, when I do so, the player view isn't presented and the audio only work and this warning is presented

Warning: Attempt to present <MPMoviePlayerViewController: 0x16b68d70> on <EventsTabBarController: 0x156dc960> whose view is not in the window hierarchy!
engmsaleh commented 9 years ago

I have figured out the problem, in my case my rootviewcontroller is a UITabbarViewController, but the view controller that is used to fire up the movieplayer is a modalviewcontroller which is presented above the UITabbarController.

The problem is YKMediaPlayerKit is calling the root of the window and not the visible one which cause this issue when this is called

    [[UIApplication sharedApplication].keyWindow.rootViewController presentMoviePlayerViewControllerAnimated:self.player];

My workaround was to change the above line to the following

    [[self topViewController] presentMoviePlayerViewControllerAnimated:self.player];
#pragma mark - Get topMost viewController
- (UIViewController *)topViewController{
    return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
    if (rootViewController.presentedViewController == nil) {
        return rootViewController;
    }

    if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
        UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
        return [self topViewController:lastViewController];
    }

    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
    return [self topViewController:presentedViewController];
}

Which call the presentation on the visible top most view controller

HaKKeBeilHaRRy commented 9 years ago

worked ! thanks a lot