Properly displaying and dismissing fullscreen MPMoviePlayerController in iOS 3.2 (iPad)

前端 未结 2 1282
暖寄归人
暖寄归人 2020-12-12 18:31

I\'m having lots of trouble displaying a fullscreen movie in my iPad app and then allowing the user to dismiss it with either the Done button or the \"un-fullscreen\" button

2条回答
  •  粉色の甜心
    2020-12-12 19:05

    Here are how the events -> notifications work:

    • User presses 'Done' button

      • MPMoviePlayerWillExitFullscreenNotification
      • MPMoviePlayerDidExitFullscreenNotification
    • User presses 'Leave fullscreen' button on transport

      • MPMoviePlayerWillExitFullscreenNotification
      • MPMoviePlayerDidExitFullscreenNotification
      • Note that playback does not stop
    • Movie reaches end

      • MPMoviePlayerPlaybackDidFinishNotification with the MPMoviePlayerPlaybackDidFinishReasonUserInfoKey set to MPMovieFinishReasonPlaybackEnded
      • If you call setFullscreen:NO animated:YES on your MoviePlayerController instance from this notification, you'll then get the WillExit and DidExit notifications.
      • Note that you don't get the PlaybackDidFinish notification when the user presses the Done or Leave Fullscreen buttons.

    So, typically, if you want to get rid of the MoviePlayer's view, you need to put [self.moviePlayerController.view removeFromSuperview] in the DidExitFullscreen notification handler. WillExitFullscreen is too soon.

    Here's my code:

    - (void)willEnterFullscreen:(NSNotification*)notification {
        NSLog(@"willEnterFullscreen");
    }
    
    - (void)enteredFullscreen:(NSNotification*)notification {
        NSLog(@"enteredFullscreen");
    }
    
    - (void)willExitFullscreen:(NSNotification*)notification {
        NSLog(@"willExitFullscreen");
    }
    
    - (void)exitedFullscreen:(NSNotification*)notification {
        NSLog(@"exitedFullscreen");
        [self.movieController.view removeFromSuperview];
        self.movieController = nil;
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    - (void)playbackFinished:(NSNotification*)notification {
        NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
        switch ([reason intValue]) {
            case MPMovieFinishReasonPlaybackEnded:
                NSLog(@"playbackFinished. Reason: Playback Ended");         
                    break;
            case MPMovieFinishReasonPlaybackError:
                NSLog(@"playbackFinished. Reason: Playback Error");
                    break;
            case MPMovieFinishReasonUserExited:
                NSLog(@"playbackFinished. Reason: User Exited");
                    break;
            default:
                break;
        }
        [self.movieController setFullscreen:NO animated:YES];
    }
    
    - (void)showMovie {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
        NSURL* movieURL =  [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"tron" ofType:@"mov"]];
        self.movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
        self.movieController.view.frame = self.view.frame;
        [self.view addSubview:movieController.view];
        [self.movieController setFullscreen:YES animated:YES];
        [self.movieController play];
    }
    

提交回复
热议问题