AVPlayer的使用,带缓冲


#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
{
    AVPlayer *player;
    AVPlayerItem *playerItem;
    
    UIProgressView * progressView;
    UISlider *_slider;
    
    //判断slider是否按下,
    BOOL isOpen;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    //进行初始化创建
    NSURL *url = [NSURL fileURLWithPath:@"/Users/qianfeng01/Downloads/千锋Swift视频教程-1.Swift语言介绍.mp4"];
    playerItem  = [[AVPlayerItem alloc]initWithURL:url];
    //创建player
    player = [[AVPlayer alloc]initWithPlayerItem:playerItem];
    //生成layer层
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
    //设置坐标
    layer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    //把layer层假如到self.view.layer中
    [self.view.layer addSublayer:layer];
    //进行播放
    [player play];
    
    /**以上是基本的播放界面,但是没有前进后退**/
    //观察是否播放,KVO进行观察,观察playerItem.status
    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
    
    //观察缓存现在的进度,KVO进行观察,观察loadedTimeRanges
    [playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
}

//观察是否播放
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"status"]) {
        if (playerItem.status == AVPlayerStatusReadyToPlay) {
            NSLog(@"开始播放");
            //需要开始获取数据,包括播放的总时长,播放的缓存,播放的当时时间
            [self loadData];
        }else{
            NSLog(@"播放失败");
        }
    }else{
        //kvo触发的另外一个属性
        NSArray *array = [playerItem loadedTimeRanges];
        //获取范围i
        CMTimeRange range = [array.firstObject CMTimeRangeValue];
        //从哪儿开始的
        CGFloat start = CMTimeGetSeconds(range.start);
        //缓存了多少
        CGFloat duration = CMTimeGetSeconds(range.duration);
        //一共缓存了多少
        CGFloat allCache = start+duration;
        NSLog(@"缓存了多少数据:%f",allCache);
        
        //设置缓存的百分比
        CMTime allTime = [playerItem duration];
        //转换
        CGFloat time = CMTimeGetSeconds(allTime);
        CGFloat y = allCache/time;
        NSLog(@"缓存百分比:--------%f",y);
        progressView.progress = y;
    }
}


#pragma mark -- 获取播放数据
- (void)loadData{
    
    __weak AVPlayerItem *xx = playerItem;
    __weak UISlider *cc = _slider;
    //第一个参数是每隔多长时间调用一次,在这里设置的是每隔1秒调用一次
    [player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:NULL usingBlock:^(CMTime time) {
        //当前播放时间
        CGFloat current = xx.currentTime.value/xx.currentTime.timescale;
        
        //获取总时长
        CMTime time1 = xx.duration;
        float x = CMTimeGetSeconds(time1);
        NSLog(@"当前播放的秒数------- %f --------%f",current,x);
        
        //设置滑动条进度
        float v = current/x;
        
        //判断slider是否按下,按下去就先别赋值
        if (!isOpen) {
            cc.value = v;
        }
        
    }];

}

#pragma mark --- 创建UI
- (void)createUI{
    progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
    progressView.frame = CGRectMake(0, 460, self.view.frame.size.width, 20);
    
    [self.view addSubview:progressView];
    
    _slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 480, self.view.frame.size.width, 20)];
    [self.view addSubview:_slider];
    
    
    //添加点击事件
    [_slider addTarget:self action:@selector(sliderClick:) forControlEvents:UIControlEventTouchUpInside];
    //抬起来的事件
    [_slider addTarget:self action:@selector(sliderClickUp:) forControlEvents:UIControlEventTouchUpInside];
}

    //添加点击事件
- (void)sliderClick:(UISlider *)slider{
    NSLog(@"添加点击事件");
    isOpen = YES;
}

    //抬起来的事件
- (void)sliderClickUp:(UISlider *)slider{
    NSLog(@"抬起来的事件");
    isOpen = NO;
    
    //从这里开始播放
    CGFloat g = slider.value;
    //获取总时长
    CMTime time1 = playerItem.duration;
    float x = CMTimeGetSeconds(time1);
    //进行播放
    [player seekToTime:CMTimeMake(x * g,1)];
    //播放
    [player play];
    
}


@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值