AVPlayer判断是否开始播放

加载视频的时候加一个菊花框,然后开始播放了就去掉菊花框:

  • 设置一个block,在播放开始的时候执行。在block内发一个notification
__block AVPlayer* blockPlayer = avPlayer;  
        __block id obs;

        obs = [avPlayer addBoundaryTimeObserverForTimes:
               @[[NSValue valueWithCMTime:CMTimeMake(1, 3)]]
                                                  queue:NULL
                                             usingBlock:^{

                                                 // Raise a notificaiton when playback has started
                                                 [[NSNotificationCenter defaultCenter]
                                                  postNotificationName:@"PlaybackStartedNotification"
                                                  object:url];

                                                 // Remove the boundary time observer

                                                 [blockPlayer removeTimeObserver:obs];
                                             }
               ];
  • 注册通知的观察者
[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(receivePlaybackStartedNotification:)
        name:@"PlaybackStartedNotification"
      object:nil];
  • 添加收到通知后的处理方法
-(void) receivePlaybackStartedNotification:(NSNotification *) notification
{
    if ([[notification name] 
            isEqualToString:@"PlaybackStartedNotification"]) {

        NSURL *url = [notification object];
        NSLog(@"PlaybackStartedNotification %@", url);
        //开始播放移除菊花框
        [SVProgressHUD dismiss];
    }
}