0

这是我的代码

- (void)viewDidLoad
{
...
 NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mov"];
NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
_moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
_moviePlayerController.controlStyle=MPMovieControlStyleDefault;
[_moviePlayerController play];

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieIsOver:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];
}

- (void)movieIsOver:(NSNotification *)notification
{
    NSLog(@"movie is over");
   [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.moviePlayerController.view removeFromSuperview];//moviePlayerController is MPMoviePlayerController
}

当电影播放到最后时,我看不到“电影结束”日志,并且没有删除 moviePlayerController.view。我不知道为什么。

编辑: MPMoviePlayerPlaybackDidFinishNotification 运作良好。我看到“电影结束”日志。问题是moviePlayerController.view 没有被删除。

4

2 回答 2

1

我找到了解决方案:添加

_moviePlayerController.fullscreen = NO;

在从超级视图中删除视图之前

于 2013-07-11T10:31:57.667 回答
0

而不是添加 _moviePlayerController.view 作为子视图

[self.view addSubview:_moviePlayerController.view];

您可以像这样呈现 _moviePlayerController:

[self presentMoviePlayerViewControllerAnimated: _moviePlayerController];

在movieIsOver方法中,您只需解雇

[self dismissMoviePlayerViewControllerAnimated];

我希望它的帮助

于 2013-07-12T04:59:59.140 回答