0

我正在尝试创建一个在视频停止播放后显示信息页面的应用程序。我查看了此处发布的类似问题并尝试实现它,但它仍然不起作用。我在 movieFinishedCallback 方法中放置了一个 NSLog,但从未出现过,所以我猜它甚至没有被调用。有人可以帮我弄清楚吗?

这是我的实现代码...

-(IBAction)playvideo {


    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:@"sample" ofType:@"mov"]];

    MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
                                                     initWithContentURL:url];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(movieFinishedCallback:)                                                 
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:playercontroller];



    [self presentMoviePlayerViewControllerAnimated:playercontroller];

    playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;


    [playercontroller.moviePlayer play];


    playercontroller = nil;

} 


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

  NSLog (@"The video ended");

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


    View2 *second =[[View2 alloc] initWithNibName:nil bundle:nil];

    [self presentModalViewController:second animated:YES];

}
4

2 回答 2

0

我认为您可能会在电影完成后被回调,但在呈现 View2 时失败。

View2 是 UIViewController 的子类吗?我看不出如何通过将 nil 传递给 initWithNibName: 来获得有效结果。我建议在 movieFinishedCallback 中使用 NSLog 来证明你到达那里。然后 NSLog(@"second = %@", second); 我的猜测是零。

解决方法是使用有效的笔尖或从情节提要中加载真实的视图控制器......

// @"MainStoryboard" should be the name of the storyboard containing your VC
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

// @"SomeViewController" has to be set on the attributes inspector of the VC
// in the storyboard
SomeViewController *newVC = [storyboard instantiateViewControllerWithIdentifier:@"SomeViewController"];
[self presentModalViewController:newVC animated:YES];
于 2012-07-31T23:44:20.780 回答
0

终于成功了!我不知道这是否与 MPMoviePlayerViewController 不响应 NSNotificationCenter 的事实有关,但我使用 MPMoviePlayerController 来显示我的视频,注册了 movieFinishedCallback 的通知,然后使用 movieFinishedCallback 方法切换视图。有关视频播放,请参阅本教程http://www.techotopia.com/index.php/Video_Playback_from_within_an_iOS_5_iPhone_Application。唯一的问题是,现在视频不会旋转到横向!

于 2012-08-02T16:10:54.663 回答