使用系统的AVplayer做点播播放器,断网播放出错后player的duration和playableDuration获取不到,写了get方法获取不到时返回0,然后就掉坑里了。
获取播放时长的get方法
- (NSTimeInterval)duration {
return isnan(vodPlay.hlsPlayer.duration)?0:vodPlay.hlsPlayer.duration;
}
return 0;
}
获取缓存进度的get方法
- (NSTimeInterval)playableDuration {
return isnan(vodPlay.hlsPlayer.playableDuration)?0:vodPlay.hlsPlayer.playableDuration;
}
return 0;
}
因为返回了0,所以播放出错时,totalTime,导致进度条被重置到了0的位置,并没办法滑动。因为
self.vodSlider.maximumValue = totalTime;
self.vodSlider.minimumValue = 0.0;
设置了进度条的最大值是播放时长度,出错后maximumValue被设置成了0,就出现了进度条滑块被重置到0的位置,并无法滑动了的问题。
由于totalTime成了0,playableDuration也成了0,
[self.vodSlider.progressView setProgress:playable/totalTime animated:NO];
playable/totalTime分子分母都为0,导致了不可预知的问题,滑块的缓存进度值就会是个不确定的值,导致缓存显示出问题。
设置滑竿和时间的方法
- (void)setProgressTime:(CGFloat)currentTime totalTime:(CGFloat)totalTime playableValue:(CGFloat)playable
{
if (!self.vodSlider.isDragging) {
//总时长
self.vodTotalTimeLabel.text = [VHPlayerSkinTool timeFormat:totalTime];
//更新当前播放时间
self.vodCurrentTimeLabel.text = [VHPlayerSkinTool timeFormat:currentTime];
//全屏显示时间
self.vodfullScreenTimeLabel.text = [NSString stringWithFormat:@"%@/%@",self.vodCurrentTimeLabel.text,self.vodTotalTimeLabel.text];
// 更新slider
self.vodSlider.maximumValue = totalTime;
self.vodSlider.minimumValue = 0.0;
self.vodSlider.value = currentTime;
}
//更新缓存时长
[self.vodSlider.progressView setProgress:playable/totalTime animated:NO];
}
改成
- (void)setProgressTime:(CGFloat)currentTime totalTime:(CGFloat)totalTime playableValue:(CGFloat)playable
{
if (!self.vodSlider.isDragging && currentTime > 0 && totalTime > 0) {
//总时长
self.vodTotalTimeLabel.text = [VHPlayerSkinTool timeFormat:totalTime];
//更新当前播放时间
self.vodCurrentTimeLabel.text = [VHPlayerSkinTool timeFormat:currentTime];
//全屏显示时间
self.vodfullScreenTimeLabel.text = [NSString stringWithFormat:@"%@/%@",self.vodCurrentTimeLabel.text,self.vodTotalTimeLabel.text];
// 更新slider
self.vodSlider.maximumValue = totalTime;
self.vodSlider.minimumValue = 0.0;
self.vodSlider.value = currentTime;
}
//NSLog(@"******* playable %f %f %f",playable,currentTime,totalTime);
//更新缓存时长,播放出错时,播放器返回的playable和totalTime是0。
if (playable > 0 && totalTime > 0) {
[self.vodSlider.progressView setProgress:playable/totalTime animated:NO];
}
}