简体   繁体   中英

Add activity indicator in AVPLayerViewController ? (Note: When video is in fullscreen mode)

在此处输入图像描述如何在全屏模式下在 AVPLayerViewController 的中心添加活动指示器?

You can add your custom indicator view in centre of AVPLayerViewController by adding custom view on main key window.

UIApplication.shared.keyWindow?.addSubview(your custom indicator view)

You can set centre of your custom indicator view as below.

activity.center = CGPoint.init(x: UIScreen.main.bounds.size.width/2.0, y: UIScreen.main.bounds.height/2.0)

add this code when user press play button

if(playerViewController.view.subviews.count != 0)
{
  UIView *AVTouchIgnoringView = playerViewController.view.subviews[0].subviews.lastObject;
  activityIndicatorBuffer.center = playerViewController.view.center;
  [AVTouchIgnoringView addSubview:activityIndicatorView];
  [AVTouchIgnoringView bringSubviewToFront:activityIndicatorView];
}

and Don't forget to add below methods

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [playerViewController addObserver:self forKeyPath:@"videoBounds" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
-(void)viewDidDisappear:(BOOL)animated
{
     [super viewDidDisappear:animated];
     [playerViewController removeObserver:self forKeyPath:@"videoBounds"];
}
-(void)observeValueForKeyPath:(NSString )keyPath ofObject:(id)object change:(NSDictionary )change context:(void *)context
{
     if ([keyPath isEqualToString:@"videoBounds"])
     {
        float height = playerViewController.contentOverlayView.bounds.size.height;
        float width = playerViewController.contentOverlayView.bounds.size.width;
        if (height == SCREEN_HEIGHT && width == SCREEN_WIDTH)
        {
            activityIndicatorBuffer.center = playerViewController.contentOverlayView.center;
        }
        else
        {
            activityIndicatorBuffer.center = playerViewController.view.center;    
        }
     }
}

and don't forget to start animating when buffering. click here this link for check AVPlayer is buffering

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM