Search code examples
objective-cvideonsnotification

why isn't removeFromSuperview removing my movie subview? Xcode


I'm trying to get a movie that plays to dismiss on its own without having to hit the "Done" button. I think it's a recent iOS 6 problem since I'm following a tutorial exactly and when I insert NSLogs the NSNotification and removeFromSuperview are being recognized but the movie stays there once it has ended. Here's my code, please help:

    - (IBAction)playMovie:(id)sender
{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:@"RomneyFlipSequence1" ofType:@"mov"]];
    _moviePlayer =
    [[MPMoviePlayerController alloc]
     initWithContentURL:url];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:_moviePlayer];

    _moviePlayer.controlStyle = MPMovieControlStyleDefault;
    _moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:_moviePlayer.view];
    [_moviePlayer setFullscreen:YES animated:NO];
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {

    MPMoviePlayerController *player = [notification object];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:player];

    if ([player
         respondsToSelector:@selector(setFullscreen:animated:)])
    {
    [player.view removeFromSuperview];
    }
}
@end

Solution

  • You entered the player into fullscreen mode.

    [_moviePlayer setFullscreen:YES animated:NO];
    

    Reading other solutions in SO it seems that when you press "Done" the player is taken out of fullscreen first, and then the notification is thrown. After reading this answer, adding

    [_moviePlayer setFullscreen:NO animated:YES];
    

    before your removeFromSuperview call will solve your problem.

    If the above doesn't work, in addition you may try stoping the player first so the full code will be

    -(void)removePlayer:(MPMoviePlayerController *)player{
    
        NSLog(@"Playback Finished");
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:_moviePlayer];
    
        [_moviePlayer stop]; // <-- May not be needed
        if ([_moviePlayer respondsToSelector:@selector(setFullscreen:animated:)])
        {
            [_moviePlayer setFullscreen:NO animated:YES];
            [_moviePlayer.view removeFromSuperview];
    
        }
    
        _moviePlayer=nil;
    
    
    
    }
    

    I use the same method for both notifications "Done" and "PlayBackFinished"

    Hope this helps.