2016-10-08 16 views
6

Tôi đang viết Plugin cho AVPlayer trong ios. Tôi cần biết khi nào người dùng nhấp vào nút Xong trong AVPlayerViewController (Tôi muốn biết khi nào người dùng đóng video) và tôi không truy cập vào đối tượng AVPlayerViewController. Tôi đã kiểm tra sự kiện và chỉ tìm thấy thuộc tính tốc độ trong AVPlayer được đặt thành 0 nhưng trong tình huống tạm dừng cũng là tỷ lệ được đặt thành 0. làm thế nào tôi tìm ra hai tình huống này? cảm ơn tất cả.phát hiện AVPlayerViewController Xong nút bấm?

+0

các [Giải pháp] (http://stackoverflow.com/a/41281453/4593553) làm việc cho tôi. – Jerome

Trả lời

0

Tôi đã gặp sự cố khi tôi phát triển trình phát ở chế độ cửa sổ. Đó là currently impossible không có thủ thuật. Vì vậy, tôi đã sử dụng KVO để quan sát contentOverlayView mà thực sự là một cái nhìn đầy đủ kích thước của AVPlayerViewController. Mã là một chút phức tạp. Trong ví dụ bên dưới, thuộc tính playerView là chế độ xem từ xib/bảng phân cảnh trên bộ điều khiển chế độ xem (xem phần đính kèm).

#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 

Các vấn đề liên quan