2

我正在尝试制作一部无需点击“完成”按钮即可自行播放的电影。我认为这是最近的 iOS 6 问题,因为我完全按照教程进行操作,并且当我插入 NSLogs 时,NSNotification 和 removeFromSuperview 正在被识别,但是电影一旦结束就会停留在那里。这是我的代码,请帮助:

    - (IBAction)playMovie:(id)sender
{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:@"RomneyFlipSequence1" ofType:@"mov"]];
    _moviePlayer =
    [[MPMoviePlayerController alloc]
     initWithContentURL:url];

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

    _moviePlayer.controlStyle = MPMovieControlStyleDefault;
    _moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:_moviePlayer.view];
    [_moviePlayer setFullscreen:YES animated:NO];
}

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

    MPMoviePlayerController *player = [notification object];

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

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

2 回答 2

1

您使播放器进入全屏模式。

[_moviePlayer setFullscreen:YES animated:NO];

在 SO 中阅读其他解决方案时,似乎当您按下“完成”时,播放器首先退出全屏,然后引发通知。阅读此答案后,添加

[_moviePlayer setFullscreen:NO animated:YES];

在您的removeFromSuperview电话将解决您的问题之前。

如果上述方法不起作用,另外您可以尝试先停止播放器,以便完整代码为

-(void)removePlayer:(MPMoviePlayerController *)player{

    NSLog(@"Playback Finished");
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:_moviePlayer];

    [_moviePlayer stop]; // <-- May not be needed
    if ([_moviePlayer respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [_moviePlayer setFullscreen:NO animated:YES];
        [_moviePlayer.view removeFromSuperview];

    }

    _moviePlayer=nil;



}

我对通知“完成”和“PlayBackFinished”使用相同的方法

希望这可以帮助。

于 2013-05-18T18:02:36.947 回答
0

您已添加_moviePlayer.view为视图,但您正在尝试删除player.view

我认为这会做到:

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

MPMoviePlayerController *player = [notification object];

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

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

}

于 2012-10-10T06:36:06.090 回答