您的位置:首页 > 其它

AVPlayer 基础用法

2016-07-25 16:09 309 查看
  

          转载地址:     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


      

    

        

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  AVPlayer 基础用法