AVPlayer中想关方法介绍

来源:互联网 发布:godaddy 域名续费 编辑:程序博客网 时间:2024/03/28 22:05

 计算缓存

[self.itemaddObserver:selfforKeyPath:@"loadedTimeRanges"options:NSKeyValueObservingOptionNewcontext:nil];

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context

{

    if (object == self.item) {

        if ([keyPath isEqualToString:@"status"]) {

            

            if (self.play.status == AVPlayerStatusReadyToPlay) {

                

                //self.playState = ZFPlayerStatePlaying;

            } else if (self.play.status ==AVPlayerStatusFailed){

                //[self.maskView.activity startAnimating];

            }

        } else if ([keyPathisEqualToString:@"loadedTimeRanges"]) {

            NSArray *loadedTimeRanges = [[self.playcurrentItem] loadedTimeRanges];

            CMTimeRange timeRange     = [loadedTimeRanges.firstObjectCMTimeRangeValue];//获取缓冲区域

            float startSeconds        = CMTimeGetSeconds(timeRange.start);

            float durationSeconds     = CMTimeGetSeconds(timeRange.duration);

            NSTimeInterval result     = startSeconds + durationSeconds;//计算缓冲总进度

            NSLog(@"%f",result);


            CMTime duration             = self.item.duration;

            CGFloat totalDuration       = CMTimeGetSeconds(duration);

            [self.progresssetProgress:result / totalDuration animated:NO];

        }


    }

}


定时

 __weak typeof (self) weakSelf =self;


 [self.playaddPeriodicTimeObserverForInterval:CMTimeMake(1,1) queue:dispatch_get_main_queue()usingBlock:^(CMTime time) {

        if (self.isDrag) {

            return ;

        }

           CGFloat current = CMTimeGetSeconds(time);

           CGFloat total = CMTimeGetSeconds([weakSelf.item duration]);

           //NSLog(@"%f--%f",current,total);

           [weakSelf.slider setValue:@(current/total)forKey:@"value"];

        

           NSInteger min = (NSInteger)current /60;

           NSInteger sec = (NSInteger)current %60;

           weakSelf.currentLable.text = [NSStringstringWithFormat:@"%02ld:%02ld",(long)min,(long)sec];

        

           NSInteger min1 = (NSInteger)total /60;

           NSInteger sec1 = (NSInteger)total %60;

           weakSelf.totalLable.text = [NSStringstringWithFormat:@"%02ld:%02ld",(long)min1,(long)sec1];

        

    }];


暂停后再播放

[self.playseekToTime:CMTimeMake(current,1) completionHandler:^(BOOL finished) {

        self.isDrag =NO;

        [self.playplay];

    }];



0 0