AVPlayer 基础用法

  

          转载地址:     http://www.jianshu.com/p/44e7cd7af180    


#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property (nonatomic,strong) AVPlayer  *player;
@property (nonatomic,strong) AVPlayerItem *playerItem;

@property (nonatomic,assign) CGFloat totalMovieDuration;

@end

@implementation ViewController


- (AVPlayer *)player{
    if (_player == nil) {
        _player = [AVPlayer playerWithPlayerItem:self.playerItem];
    }
    return _player;
}


- (AVPlayerItem *)playerItem{
    if (_playerItem == nil) {
        
      NSURL *url = [NSURL URLWithString:@"http://baobab.wdjcdn.com/14562919706254.mp4"];
        
//        NSURL *url = [[NSBundle mainBundle] URLForResource:@"minion_01" withExtension:@"mp4"];
        
        _playerItem = [[AVPlayerItem alloc]initWithURL:url];
        
        
        // 通过KVO形式,来知道 AVPlayerItem(资源类)的状态和进度
         
        // 缓存
        [_playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
        
        // 状态
        [_playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
        
    }
    return _playerItem;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 这个主要是用来处理显示视频的作用, 渲染作用。
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    playerLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:playerLayer];
    
    // 有所有Layer的所有属性值。
    playerLayer.borderColor = [UIColor redColor].CGColor;
    playerLayer.borderWidth = 2.0;
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
          // 开始播放
          [_player play];
        
    });
    
}



- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
    AVPlayerItem *item = (AVPlayerItem *)object;
    
    NSLog(@"keypath ---- > %@", keyPath);
    
    
    if ([keyPath isEqualToString:@"status"]) {
        /**
         
         *     AVPlayerItemStatusUnknown,播放源未知
         
         *     AVPlayerItemStatusReadyToPlay,播放源准备好
         
         *     AVPlayerItemStatusFailed播放源失败
         
         */
        
        AVPlayerStatus status= [[change objectForKey:@"new"]intValue];
        if (status == AVPlayerItemStatusReadyToPlay) { // 和资源对接成功后,就可以知道了资源的时间等信息。
            
            //转化时间的格式
            CMTime totalTime = item.duration;
            
            _totalMovieDuration= (CGFloat)totalTime.value/ totalTime.timescale;
            
            NSDate *d = [NSDate dateWithTimeIntervalSince1970:_totalMovieDuration];
            
            NSDateFormatter*formatter = [[NSDateFormatter alloc]init];
            
            if(_totalMovieDuration/3600>=1) {
                
                [formatter setDateFormat:@"HH:mm:ss"];

            }else{
                
                [formatter setDateFormat:@"mm:ss"];
                
            }
            NSString *showtime = [formatter stringFromDate:d];
            NSLog(@"这个视频的时间---->%@", showtime);
        }
        
        
    }else if([keyPath isEqualToString:@"loadedTimeRanges"]){ // 缓存
       
        NSArray*array = item.loadedTimeRanges;
        NSLog(@"array = %@", array);
        
        //  本次缓冲时间范围
        
        CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];
        
        float startSeconds =CMTimeGetSeconds(timeRange.start);
        
        float durationSeconds =CMTimeGetSeconds(timeRange.duration);
        
        //  缓冲总长度
        
        NSTimeInterval totalBuffer = startSeconds + durationSeconds;
        
        NSLog(@"共缓冲:%.2f",totalBuffer); // 这个缓存是以时间为单位的。
        
        //
        CGFloat currentProgress =  (totalBuffer *1.0/_totalMovieDuration)*100;
        
        
        NSLog(@"当前的进度是---->%f",currentProgress); // 进度
        
    }
    
}






@end



      

    


        


  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值