iosobjective-canimationuiviewcontrollermovieplayer

Cannot present MoviePlayerView animation while a dismissMoviePlayerView animation is in progress


I have an app that presents images and/or movies in sequence. The problem is, I cannot dismiss one movie player and then present another. If I try, I get the message "Warning: Attempt to present MPMoviePlayerViewController on MyViewController while a presentation is in progress!" Unlike other animated present/dismiss methods, there is no completion handler, nor a non-animated version of present/dismiss.

Here is a simplified version of my code:

-(void) play
{
    [[window rootViewController] presentMoviePlayerViewControllerAnimated:player];
    [[_player moviePlayer] play];
}

-(void) videoNotification:(NSNotification *) notification
{
    if([notification.name isEqualToString:MPMoviePlayerPlaybackDidFinishNotification])
    {
        [[window rootViewController] dismissMoviePlayerViewControllerAnimated];
        [_canvasManager showNextCanvas]; //this calls play on the next canvas
    }
}

Any thoughts/hints on how to achieve my goal?


Solution

  • After looking at dismissViewControllerAnimated:completion:, I was able to create a completion handler by creating a subclass of MPMoviePlayerViewController and overriding -(void)viewDidDisappear. viewDidDisappear gets called at the end of dismissMoviePlayerViewControllerAnimated.

    @interface MyMoviePlayerViewController : MPMoviePlayerViewController
    {
        void (^_viewDidDisappearCallback)(void);
    }
    
    - (void)setViewDidDisappearCallback:(void (^)(void))callback;
    
    @end    
    
    @implementation MyMoviePlayerViewController
    
    - (id) initWithContentURL:videoPath
    {
        self = [super initWithContentURL:videoPath];
        _viewDidDisappearCallback = nil;    
        return self;
    }
    
    - (void)setViewDidDisappearCallback:(void (^)(void))callback
    {
        _viewDidDisappearCallback = [callback copy];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];    
        if(_viewDidDisappearCallback != nil)
            _viewDidDisappearCallback();
    }
    
    @end