addObserver
2016-06-04 21:58
253 查看
[self.myPlayerView.player.currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
#pragma mark- KVO回调方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change
context:(void *)context{
//判断被观察的对象是否为对应的媒体对象,并且观察的属性为status字段
if (object == self.myPlayerView.player.currentItem &&
[keyPath isEqualToString:@"status"])
{
/*
AVPlayerStatusUnknown,
AVPlayerStatusReadyToPlay,
AVPlayerStatusFailed
*/
//判断状态是否转变为可播放状态
if (self.myPlayerView.player.currentItem.status == AVPlayerStatusReadyToPlay)
{
/*
//CMTime专门用于表示电影时间
//CMTimeMake(已经播放到第几帧, 1秒钟内播放的总帧);
CMTimeMake(50, 2);
*/
//总秒数
NSInteger totalSeconds = self.myPlayerView.player.currentItem.duration.value / self.myPlayerView.player.currentItem.duration.timescale;
//设置进度条的最大值
self.progressSlider.maximumValue = totalSeconds;
//设置显示总时间大小的Label内容
self.totalTimeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",totalSeconds/3600,totalSeconds%3600/60,totalSeconds%60];
}
//监测进度
/*
参数1:监测的间隔时间
参数2:使用的队列(GCD内容,暂时置为nil)
参数3:回调block
*/
[self.myPlayerView.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:nil usingBlock:^(CMTime time)
{
//CMTime time :目前播放的进度为time
//设置进度条进度
self.progressSlider.value = time.value / time.timescale;
//设置显示进度秒数的label内容
self.playTimeLabel.text =
[NSString stringWithFormat:@"%02ld:%02ld:%02ld",(NSInteger)self.progressSlider.value/3600,(NSInteger)self.progressSlider.value%3600/60,(NSInteger)self.progressSlider.value%60];
}];
}
}
- (IBAction)progressSliderAction:(UISlider *)sender {
//拖拽进度条改变播放进度
[self.myPlayerView.player.currentItem seekToTime:CMTimeMake(sender.value, 1)];
}
}
#pragma mark- KVO回调方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change
context:(void *)context{
//判断被观察的对象是否为对应的媒体对象,并且观察的属性为status字段
if (object == self.myPlayerView.player.currentItem &&
[keyPath isEqualToString:@"status"])
{
/*
AVPlayerStatusUnknown,
AVPlayerStatusReadyToPlay,
AVPlayerStatusFailed
*/
//判断状态是否转变为可播放状态
if (self.myPlayerView.player.currentItem.status == AVPlayerStatusReadyToPlay)
{
/*
//CMTime专门用于表示电影时间
//CMTimeMake(已经播放到第几帧, 1秒钟内播放的总帧);
CMTimeMake(50, 2);
*/
//总秒数
NSInteger totalSeconds = self.myPlayerView.player.currentItem.duration.value / self.myPlayerView.player.currentItem.duration.timescale;
//设置进度条的最大值
self.progressSlider.maximumValue = totalSeconds;
//设置显示总时间大小的Label内容
self.totalTimeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",totalSeconds/3600,totalSeconds%3600/60,totalSeconds%60];
}
//监测进度
/*
参数1:监测的间隔时间
参数2:使用的队列(GCD内容,暂时置为nil)
参数3:回调block
*/
[self.myPlayerView.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:nil usingBlock:^(CMTime time)
{
//CMTime time :目前播放的进度为time
//设置进度条进度
self.progressSlider.value = time.value / time.timescale;
//设置显示进度秒数的label内容
self.playTimeLabel.text =
[NSString stringWithFormat:@"%02ld:%02ld:%02ld",(NSInteger)self.progressSlider.value/3600,(NSInteger)self.progressSlider.value%3600/60,(NSInteger)self.progressSlider.value%60];
}];
}
}
- (IBAction)progressSliderAction:(UISlider *)sender {
//拖拽进度条改变播放进度
[self.myPlayerView.player.currentItem seekToTime:CMTimeMake(sender.value, 1)];
}
相关文章推荐
- c++第六次循环
- div 水平居中的一种方法
- bool operator==(const Array&)const; 这最后一个const 是做什么用的
- unity android设备上查看log输出
- collectionView(xib)
- 面试题1:类的赋值运算符函数
- 设计模式学习(4)
- 学习笔记之一:学习中好的试题
- Lowest Common Ancestor of a Binary Search Tree
- 游戏编程中的人工智能-遗传算法入门(五)
- Android Studio1.5使用和学习记录
- html列表相关属性
- LeetCode 76. Minimum Window Substring
- Apriori算法的python实现
- Log4j2与JNDI结合
- C++ string类型变量
- JAVA-WEB开发第五讲[2016-06-04]SPRING-MVC环境搭建
- collectionView(手写)
- collectionView(手写)
- 人月神话阅读笔记2