赞
踩
// 声明两个对象
@property (nonatomic,strong)AVPlayer *player;//播放器对象
@property (nonatomic,strong)AVPlayerItem *currentPlayerItem;
// 初始化播放器
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:FullImageUrl(self.messageViewCover.userModel.video_url)];
self.currentPlayerItem = playerItem;
self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
AVPlayerLayer *avLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
avLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
avLayer.frame = self.videoImage.bounds;
[self.videoImage.layer addSublayer:avLayer];
// 播放完成通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(runLoopTheMovie:) name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem];
//1.注册观察者,监测播放器属性
//观察Status属性,可以在加载成功之后得到视频的长度
[self.player.currentItem addObserver:self forKeyPath:@“status” options:NSKeyValueObservingOptionNew context:nil];
//观察loadedTimeRanges,可以获取缓存进度,实现缓冲进度条
[self.player.currentItem addObserver:self forKeyPath:@“loadedTimeRanges” options:NSKeyValueObservingOptionNew context:nil];
// 播放
[self.player play];
//循环播放用到这个方法
//2.添加属性观察
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。