我們知道,m3u8是分片的洋魂,通過(guò)currentPlaybackTime來(lái)獲取當(dāng)前的播放時(shí)間并不準(zhǔn)確谱仪。
這篇文章主要為解決以下兩個(gè)問(wèn)題:
- 在拖動(dòng)進(jìn)度條的時(shí)候替梨,你會(huì)發(fā)現(xiàn)當(dāng)你拖到10:08仔燕,它可能會(huì)回到10:02這種位置播放裂垦,這很科學(xué)颠毙,但不是我想要的斯入。
- 測(cè)試發(fā)現(xiàn)兩小時(shí)以上視頻在播放過(guò)程中滑到視頻后半段會(huì)回退到一個(gè)莫名其妙的位置,而當(dāng)前播放視頻畫(huà)面確實(shí)是當(dāng)前的時(shí)間點(diǎn)的圖像蛀蜜。
在github官方demo中的refreshMediaControl方法里刻两,它是這么處理的:
- (void)refreshMediaControl {
//duration
NSTimeInterval duration = self.delegatePlayer.duration;
NSInteger intDuration = duration+0.5;
if (intDuration>0) {
self.mediaProgressSlider.maximumValue = duration;
self.currentTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d",(int)(intDuration/60),(int)(intDuration% 60)];
}else{
self.currentTimeLabel.text = @"--:--";
self.mediaProgressSlider.maximumValue = 1.0f;
}
//position
NSTimeInterval position;
if (_isMediaSliderBeingDragged) {
position = self.mediaProgressSlider.value;
}else{
position = self.delegatePlayer.currentPlaybackTime;
}
NSInteger intPosition = position +0.5;
if (intDuration > 0) {
self.mediaProgressSlider.value = position;
}else{
self.mediaProgressSlider.value = 0.0f;
}
self.currentTimeLabel.text =[NSString stringWithFormat:@"%02d:%02d",(int)(intPosition/60),(int)(intPosition% 60)];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(refreshMediaControl) object:nil];
if (!self.overlayPanel.hidden) {
[self performSelector:@selector(refreshMediaControl) withObject:nil afterDelay:0.5];
}
}
注意到它獲取position的方式是通過(guò)self.delegatePlayer.currentPlaybackTime獲取的,稍作修改:
if (_isMediaSliderBeingDragged) {
position = self.mediaProgressSlider.value;
}else{
if (self.delegatePlayer.playbackState == IJKMPMoviePlaybackStatePlaying && self.delegateIndicatorView.hidden) {
self.mediaProgressSlider.value += 0.5;
}
position = self.mediaProgressSlider.value; //self.delegatePlayer.currentPlaybackTime;
}
另外滴某,由于self.mediaProgressSlider.value通過(guò)自身value實(shí)時(shí)改變而不是self.delegatePlayer.currentPlaybackTime磅摹,需要實(shí)時(shí)調(diào)用refreshMediaControl方法,所以需要去除判斷霎奢。
if (!self.overlayPanel.hidden) {
[self performSelector:@selector(refreshMediaControl) withObject:nil afterDelay:0.5];
}
這樣會(huì)導(dǎo)致一種情況户誓,即當(dāng)你重復(fù)點(diǎn)擊屏幕會(huì)反復(fù)調(diào)用hideMediaControl和showMediaControl方法,間接導(dǎo)致self.mediaProgressSlider.value變化太快幕侠,需要注釋掉這兩個(gè)方法里的refreshMediaControl調(diào)用帝美。
不過(guò),這種做法其實(shí)算掩耳盜鈴了晤硕,有時(shí)間可以再研究精準(zhǔn)定位播放時(shí)間悼潭。