无法使用removeFromSuperView删除MPMoviePlayerController

时间:2012-10-16 06:54:04

标签: objective-c ios ipad

我需要从View中删除MPMoviePlayerController。 我试过这个。

[moviePlayerController stop];
[moviePlayerController.view removeFromSuperview];

视频停止,但视图未被删除。我猜[moviePlayerController.view removeFromSuperview];不起作用。可能是什么原因 ?任何这个问题的解决方案..?

感谢。

6 个答案:

答案 0 :(得分:1)

这是一个已知的问题,你正在使用ARC,然后你必须将播放器添加到你的.h,因为如果你在本地声明它仍然会被释放。

@property (nonatomic, strong) MPMoviePlayerController* controller;

添加视图:

self.controller = [[MPMoviePlayerController alloc] initWithContentURL:YOURVIDEOURL];

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

self.controller.controlStyle = MPMovieControlStyleDefault;
self.controller.shouldAutoplay = YES;

[self.view addSubview:self.controller.view];
[self.controller setFullscreen:YES animated:YES];

然后删除视图:

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

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{

    [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];

}

MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
 removeObserver:self
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:player];

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

答案 1 :(得分:1)

这个问题通常是因为播放器被取消分配而发生的。解决方案是在.h中使用“strong”属性声明播放器实例。

@property (nonatomic,strong) MPMoviePlayerController* mpController;

答案 2 :(得分:0)

不完全确定,但由于我遇到因自动解除分配而导致电影播放器​​无法显示的问题,我想你可以设置moviePlayerController = nil;

不完全确定视图是否会消失,但值得一试!

答案 3 :(得分:0)

尝试dismissViewController:animated:那可能会有用。

答案 4 :(得分:0)

[moviePlayerController stop];
[moviePlayerController setContentURL:nil];
[moviePlayerController.view removeFromSuperview];

这在我的项目中运行良好

答案 5 :(得分:0)

对我来说,我尝试了所有这些: [moviePlayer stop]; [moviePlayer setContentURL:nil]; [moviePlayer.view removeFromSuperview]; moviePlayer = nil;

没有任何效果。我发现它必须由于我的MPMoviePlayerController进入全屏。修复?

        [moviePlayer setFullscreen:NO animated:YES];