检测 AVPlayerViewController 完成按钮点击?

Posted

技术标签:

【中文标题】检测 AVPlayerViewController 完成按钮点击?【英文标题】:detect AVPlayerViewController Done button click? 【发布时间】:2016-10-08 09:30:12 【问题描述】:

我正在为 ios 中的AVPlayer 编写插件。我需要知道用户何时单击AVPlayerViewController 中的 Done 按钮(我想知道用户何时关闭视频)并且我无法访问AVPlayerViewController 对象。我检查了事件,发现 AVPlayer 中只有 rate 属性设置为 0 但在暂停情况下,速率也设置为 0 。我如何弄清楚这两种情况? 谢谢大家。

【问题讨论】:

solution 为我工作。 Done button click event in AVPlayerViewController的可能重复 【参考方案1】:

我在窗口模式下开发播放器时遇到了这个问题。这是currently impossible 没有技巧。因此,我使用 KVO 来观察contentOverlayView,这实际上是AVPlayerViewController 的全尺寸视图。代码有点复杂。在下面的示例中,playerView 属性是来自视图控制器上的 xib/storyboard 的视图(见附件)。

#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

static NSString * const kBoundsProperty = @"bounds";
static void * kBoundsContext = &kBoundsContext;

@interface ViewController ()

@property (nonatomic, strong) AVPlayerViewController *playerViewController;
// View for windowed mode.
@property (weak, nonatomic) IBOutlet UIView *playerView;

@end

@implementation ViewController

- (void)viewDidLoad 
    [super viewDidLoad];

    self.playerViewController = [[AVPlayerViewController alloc] init];


- (void)viewDidAppear:(BOOL)animated 
    [super viewDidAppear:animated];

    // Because in -viewDidLoad frame is unknown.
    [self loadPlayerView];


- (void)loadPlayerView 
    NSURL *videoURL = [NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
    AVPlayer *player = [[AVPlayer alloc] initWithURL:videoURL];
    self.playerViewController.player = player;

    [player play];

    [self addChildViewController:self.playerViewController];
    [self.playerView addSubview:self.playerViewController.view];

    // MARK: I would recommend to use constraints instead of frame.
    self.playerViewController.view.frame = self.playerView.bounds;

    [self.playerViewController.contentOverlayView addObserver:self forKeyPath:kBoundsProperty options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:kBoundsContext];


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context 
    if (context == kBoundsContext) 
        CGRect oldBounds = [change[NSKeyValueChangeOldKey] CGRectValue];
        CGRect newBounds = [change[NSKeyValueChangeNewKey] CGRectValue];

        BOOL wasFullscreen = CGRectEqualToRect(oldBounds, [UIScreen mainScreen].bounds);
        BOOL isFullscreen  = CGRectEqualToRect(newBounds, [UIScreen mainScreen].bounds);
        if (isFullscreen && !wasFullscreen) 
            if (CGRectEqualToRect(oldBounds, CGRectMake(0.0, 0.0, newBounds.size.height, newBounds.size.width))) 
                NSLog(@"Rotated fullscreen");
             else 
                NSLog(@"Entered fullscreen");
                dispatch_async(dispatch_get_main_queue(), ^
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"DidEnterInFullscreen" object:nil];
                );
            
         else if (!isFullscreen && wasFullscreen) 
            NSLog(@"Exited fullscreen");
            dispatch_async(dispatch_get_main_queue(), ^
                [[NSNotificationCenter defaultCenter] postNotificationName:@"DidExitFromFullscreen" object:nil];
            );
        
    


@end

【讨论】:

以上是关于检测 AVPlayerViewController 完成按钮点击?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Swift 中使用 AVPlayerViewController 检测全屏模式?

在 swift 中,如何在 AVPlayerViewController 中播放视频时检测触摸

在 AVPlayerViewController 中点击播放按钮时执行操作

iOS:AVPlayerViewController 视图未删除

AVPlayerViewController 不按 url 播放视频

iOS - 在 AVPlayerViewController 之上添加控件