如何拦截AVPlayerViewController中完成按钮的点击? [英] How do I intercept tapping of the done button in AVPlayerViewController?

查看:430
本文介绍了如何拦截AVPlayerViewController中完成按钮的点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 viewDidAppear AVPlayerViewController 和附加的 AVPlayer $ c>自定义 UIViewController 的方法。但是,当我按下完成按钮时,我的自定义视图控制器会自动被解除。



我想拦截此动作以使用我自己的展开Segue,但是我不知道该怎么做。我找到了 MPMoviePlayerViewController 的示例,但没有 AVPlayerViewController



我找到的 MPMoviePlayerViewController 的代码如下:

   - ( void)playVideo:(NSString *)aVideoUrl {
//使用视频URL字符串初始化电影播放器​​视图控制器
MPMoviePlayerViewController * playerVC = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]]自动释放];

//从播放完成通知中删除电影播放器​​视图控制器观察者
[[NSNotificationCenter defaultCenter] removeObserver:playerVC
name:MPMoviePlayerPlaybackDidFinishNotification
object: playerVC.movi​​ePlayer];

//将此类注册为观察者,而不是
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback :)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerVC.movi​​ePlayer];

//设置您选择的模态转换样式
playerVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

//呈现电影播放器​​视图控制器
[self presentModalViewController:playerVC animated:YES];

//开始播放
[playerVC.movi​​ePlayer prepareToPlay];
[playerVC.movi​​ePlayer play];
}

- (无效)movieFinishedCallback:(NSNotification *)aNotification {
//获取电影播放完成的原因
NSNumber * finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

//仅当原因不是回放结束时才解除视图控制器
if([finishReason intValue]!= MPMovieFinishReasonPlaybackEnded){
MPMoviePlayerController * moviePlayer = [aNotification object ]。

//从观察者中移除此类
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];

//关闭视图控制器
[self dismissModalViewControllerAnimated:YES];
}
}

我向Apple询问了这个问题并且他们已经回复了如下:


感谢您联系Apple开发者技术支持(DTS)。我们的
工程师已经审核了您的请求,并得出结论,鉴于
目前正在发货的系统配置,有
无法获得所需功能。



解决方案

Apple没有提供内置方式来处理完成按钮这一事实令人失望。



我不想从AVPlayerViewController继承,因为它不支持Apple,可能会在下一个iOS更新中打开一堆蠕虫。



我的解决方法是每200毫秒启动一次定时器并检查以下情况:

  if(playerVC.player.rate == 0&& 
(playerVC.isBeingDismissed || playerVC.nextResponder == nil)){
//处理用户完成按钮点击并使计时器无效
}

玩家的费率属性为0表示视频不再播放。如果视图控制器被解雇或已经被解雇,我们可以安全地假设用户点击了完成按钮。


I created an AVPlayerViewController and an attached AVPlayer in the viewDidAppear method of a custom UIViewController. However, when I press the "Done" button my custom view controller is dismissed automatically.

I would like to intercept this action in order to use my own unwind Segue, but I'm not sure how to do this. I've found examples for MPMoviePlayerViewController but not AVPlayerViewController.

The code I found for MPMoviePlayerViewController is below:

- (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];
    }
}

I asked Apple about this problem and they've replied as follows:

Thank you for contacting Apple Developer Technical Support (DTS). Our engineers have reviewed your request and have concluded that there is no supported way to achieve the desired functionality given the currently shipping system configurations.

解决方案

The fact that Apple provides no built-in way to handle the Done button is disappointing.

I didn't feel like inheriting from AVPlayerViewController, because it isn't supported by Apple and will probably open up a can of worms in one of the next iOS updates.

My workaround is to have a timer fire every 200 msec and check for the following condition:

if (playerVC.player.rate == 0 &&
   (playerVC.isBeingDismissed || playerVC.nextResponder == nil)) {
  // Handle user Done button click and invalidate timer
}

The player's rate property of 0 indicates that the video is no longer playing. And if the view controller is being dismissed or already dismissed, we can safely assume that the user clicked the Done button.

这篇关于如何拦截AVPlayerViewController中完成按钮的点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆