我创建一个AVPlayerViewController&在自定义UIViewController的viewDidAppear方法中附加的AVPlayer,但是当我按完成按钮时,我的自定义视图控制器将自动关闭.
我想拦截这个动作,以便我自己解开Segue,但我不知道该怎么做?我找到了MPMoviePlayerViewController但不是AVPlayerViewController ..
我找到的代码MPMoviePlayerViewController在下面..
- (void)playVideo:(NSString*)aVideoUrl { // Initialize the movie player view controller with a video URL string MPMoviePlayerViewController *playerVC = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]] autorelease]; // Remove the movie player view controller from the "playback did finish" notification observers [[NSNotificationCenter defaultCenter] removeObserver:playerVC name:MPMoviePlayerPlaybackDidFinishNotification object:playerVC.moviePlayer]; // Register this class as an observer instead [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerVC.moviePlayer]; // Set the modal transition style of your choice playerVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; // Present the movie player view controller [self presentModalViewController:playerVC animated:YES]; // Start playback [playerVC.moviePlayer prepareToPlay]; [playerVC.moviePlayer play]; } - (void)movieFinishedCallback:(NSNotification*)aNotification { // Obtain the reason why the movie playback finished NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; // Dismiss the view controller ONLY when the reason is not "playback ended" if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded) { MPMoviePlayerController *moviePlayer = [aNotification object]; // Remove this class from the observers [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; // Dismiss the view controller [self dismissModalViewControllerAnimated:YES]; }
我问苹果这个问题,他们回答如下:
感谢您联系Apple开发人员技术支持(DTS).我们的工程师已经审查了您的请求,并得出结论,鉴于目前的运输系统配置,没有任何支持的方式来实现所需的功能.
解决方法
我对AVPlayerViewController进行子类化,并从viewWillDisappear发布通知,以指示关闭AVPlayerViewController.
- (void) viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] postNotificationName:kPlayerViewDismissedNotification object:nil]; [super viewWillDisappear:animated]; }
这可能不是100%正确的(因为它会失败,如果你有另一个视图显示在AVPlayerViewController),但它为我工作,因为AVPlayerViewController总是在堆栈的顶部.