MPMoviePlayerController在点击“完成”后未移除视图

7

我正在创建一个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
    }
}

当点击“完成”按钮后,视频的画面从屏幕中消失了,但是控件没有从屏幕中移除,视图也没有从屏幕中移除。在"//用户点击完成按钮"处会执行用于将视图从父视图中移除的代码,我通过添加日志进行了检查,但是控件没有从屏幕中移除,视图也没有从屏幕中移除。我做错了什么?
编辑:
如果我使用MPMoviePlayerViewController,那么它甚至不需要等待我按下“完成”按钮。一旦视频播放完毕,它就会自动删除视图。但我不想要这样。
编辑:
如果我删除“[mPlayer setFullscreen:YES animated:YES]”,然后点击“完成”后,视图会完全被移除。但是视频不会全屏显示,状态栏变灰,这也不是我想要的。

你采取了许多步骤来描述你不想要的,但至少对我来说,这仍然没有真正说明你想要实现什么。 - Till
控件没有从屏幕上移除,玩家视图也没有从屏幕上移除。 - Anand
1
尝试这个解决方案:https://dev59.com/K1fUa4cB1Zd3GeqPJp1j#6142685 - Till
@Anand,我在使用MPmoviePlayerController时遇到了同样的问题。你有解决方法吗?如果有,请告诉我...谢谢。 - Kamar Shad
2个回答

10
下面的代码对我有用,希望能对您有所帮助。
-(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];  
}

请Sirji在代码中添加一些其他人可以轻松理解的内容,以便了解您所做的事情以及问题所在。您为克服问题所做的努力是什么?无论如何,感谢您提供的解决方案 :) - MKJParekh
下次我会注意的..!! :) - Sirji

0
- (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]);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接