MPMoviePlayerController не удаляет представление при нажатии кнопки «Готово». - PullRequest
7 голосов
/ 05 марта 2012

Я создаю объект MPMoviePlayerController и транслирую видео в полноэкранном режиме.

Я использую UIViewController для отображения вида фильма.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //http://www.youtube.com/watch?feature=player_detailpage&v=ebeQaznNcmE
    NSURL *url = [NSURL URLWithString:@"http://a1408.g.akamai.net/5/1408/1388/2005110405/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_mpeg4.mp4"];
    MPMoviePlayerController *mPlayer = [[MPMoviePlayerController alloc]initWithContentURL:url]; 
    mPlayer.view.frame = gMainView.frame;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(moviePlayBackDidFinish:)
                                            name:MPMoviePlayerPlaybackDidFinishNotification
                                            object:mPlayer];
    mPlayer.shouldAutoplay = YES;
    mPlayer.controlStyle = MPMovieControlStyleFullscreen;
    [gMainView addSubview:mPlayer.view];
    [mPlayer prepareToPlay];
    [mPlayer setFullscreen:YES animated:YES];
    [mPlayer play];
}


- (void)moviePlayBackDidFinish:(NSNotification*)notification {
    int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    if (reason == MPMovieFinishReasonPlaybackEnded) {
        //movie finished playing
    }
    else if (reason == MPMovieFinishReasonUserExited) {
        //user hit the done button
        MPMoviePlayerController *moviePlayer = [notification object];

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

        if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
            [moviePlayer.view removeFromSuperview];
        }
        [moviePlayer release];
    }
    else if (reason == MPMovieFinishReasonPlaybackError) {
        //error
    }
}

При нажатии кнопки «Готово» видеоизображение удаляется с экрана, но элементы управления не удаляются с экрана и вид не удаляется с экрана.

Элемент управления приходит к «// пользователь нажал кнопку« Готово »». Он выполняет код для удаления представления из суперпредставления, я проверил, добавив журналы, , но элементы управления не удаляются с экрана и представление не удаляется с экрана . Что я делаю не так?

EDIT:

Если я использую MPMoviePlayerViewController, тогда я даже не жду, когда я нажму «Готово». Как только видео завершено, оно автоматически удаляет вид. Но я этого не хочу.

EDIT:

Если я удаляю «[mPlayer setFullscreen: YES animated: YES]», то при нажатии на «Готово» вид полностью удаляется. Но видео не отображается в полноэкранном режиме, и строка состояния становится серой, что опять-таки не то, что мне нужно.

Ответы [ 2 ]

10 голосов
/ 06 марта 2012

Приведенный ниже код работает для меня, надеюсь, он вам тоже поможет.

-(IBAction)playVedio:(id)sender{
mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

    [[mp moviePlayer] prepareToPlay];
    [[mp moviePlayer] setUseApplicationAudioSession:NO];
    [[mp moviePlayer] setShouldAutoplay:YES];
    [[mp moviePlayer] setControlStyle:2];
    [[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [self presentMoviePlayerViewControllerAnimated:mp];

}

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

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

    [mp.moviePlayer stop];
    mp = nil;
    [mp release];
    [self dismissMoviePlayerViewControllerAnimated];  
}
0 голосов
/ 31 марта 2015
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    id presentedViewController = [window.rootViewController presentedViewController];
    NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;

    if (window && [className isEqualToString:@"AVFullScreenViewController"]) {

        return UIInterfaceOrientationMaskAll;

    } else {

        UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

        if(UIInterfaceOrientationIsPortrait(interfaceOrientation))

        {

        }

        else if(UIInterfaceOrientationIsLandscape(interfaceOrientation))

        {


        }

        return UIInterfaceOrientationMaskPortrait;

        CGRect frame = [UIScreen mainScreen].applicationFrame;
        CGSize size = frame.size;
        NSLog(@"%@", [NSString stringWithFormat:@"Rotation: %s [w=%f, h=%f]",
                      UIInterfaceOrientationIsPortrait(interfaceOrientation) ? "Portrait" : "Landscape",
                      size.width, size.height]);
    }
}
...