![](/img/trans.png)
[英]iOS 7 AVPlayer AVPlayerItem duration incorrect in iOS 7
[英]Why the avplayer time is incorrect in iOS
我的公司需要一個視頻應用程序,所以我昨天開始工作。
您可以看到avplayer的以下代碼:
// to get palying time
-(void)addProgressObserver{
__weak typeof(self) weakSelf = self ;
//這里設置每秒執行一次
[self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
double current=weakSelf.player.currentItem.duration.value/weakSelf.player.currentItem.duration.timescale*1.0;
weakSelf.playerProgressDuration = current ;
}];
}
// to get total time
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
AVPlayerItem *playerItem=object;
if ([keyPath isEqualToString:@"status"]) {
AVPlayerStatus status= [[change objectForKey:@"new"] intValue];
if(status==AVPlayerStatusReadyToPlay){
self.playerTotalDuration = playerItem.duration.value / playerItem.duration.timescale * 1.0 ;
}
}
}
您可以看到此代碼。 方法“ addProgressObserver”可以獲取播放時間:
weakSelf.playerProgressDuration = current ;
KVO方法可以偵聽某個屬性(該屬性的名稱為'status'),並獲取視頻的總時間:
self.playerTotalDuration = playerItem.duration.value / playerItem.duration.timescale * 1.0 ;
但是,建立這段代碼並運行ipad 3(ios 8.1),我可以發現一個問題:
視頻播放已完成,但是self.playerProgressDuration不能等於self.playerTotalDuration!
所以我添加了一個通知:AVPlayerItemDidPlayToEndTimeNotification,但是該通知無法執行!
問題:1,視頻播放完畢,為什么self.playerProgressDuration不能等於self.playerTotalDuration? 2,視頻播放完畢,為什么不執行通知?
請幫助我,謝謝。
答:1,請使用double或int_64; 2,由於通知對象存在錯誤! 請將nil或AVPlayerItem設置為通知對象,不能將AVPlayer設置為通知對象!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.