MPMoviePlayer无法从superview中删除

时间:2012-07-06 09:25:51

标签: ios xcode cocoa mpmovieplayercontroller mpmovieplayer

我使用以下代码在MPMoviePlayerController中播放视频文件

  NSString* filePath = [[NSBundle mainBundle] pathForResource:@"one" ofType:@"mp4"];
    NSURL*  url = [NSURL fileURLWithPath:filePath];

    _movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [_movie.view setFrame:self.view.bounds];
    [self.view addSubview:_movie.view];
    _movie.fullscreen=YES;
    _movie.controlStyle=MPMovieControlStyleFullscreen;
    [_movie prepareToPlay];
    [_movie play];

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(close:)name:MPMoviePlayerPlaybackDidFinishNotification object:_movie];

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

    int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];

    if(reason == MPMoviePlaybackStateStopped) {

        NSLog(@"Stop");

    }
    else if (reason == MPMovieFinishReasonPlaybackEnded) {
        NSLog(@"Playback Ended ");
    }
    else if (reason == MPMovieFinishReasonUserExited) {

        NSLog(@"Exited");
        [_movie.view removeFromSuperview];
    }
    else if (reason == MPMovieFinishReasonPlaybackError) {
        //error
        NSLog(@"Error");
    }    
}

我能够获得通知,并且电影播放器​​不会从超级视图中删除。

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

请尝试以下说明:

当我听MPMoviePlayerWillExitFullscreenNotification时。

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(doneButtonClick:) 
                                             name:MPMoviePlayerWillExitFullscreenNotification 
                                           object:_movie];

选择器方法:

-(void)doneButtonClick:(NSNotification*)aNotification{
  [_movie.view removeFromSuperview];
}

(或)

在下面的教程中使用mpmovieplayerviewcontroller的更好方法

http://mobiledevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html