简体   繁体   中英

Why the avplayer time is incorrect in iOS

My company need a video app , so i start working yesterday .

You can see this code of 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 ;
        }
    }
}

You can see this code . the method 'addProgressObserver' can get playing time :

weakSelf.playerProgressDuration = current ;

the KVO method can listen a property (the property's name is 'status') , and get the video's total time :

self.playerTotalDuration = playerItem.duration.value  / playerItem.duration.timescale * 1.0 ;

but , bulid this code and running ipad 3 (ios 8.1) , I can find a issue :

Video playback is completed , but the self.playerProgressDuration cannot equal to self.playerTotalDuration !

So I add a notification : AVPlayerItemDidPlayToEndTimeNotification , but the notification can not be executed !

Question : 1、Video playback is completed ,why the self.playerProgressDuration cannot equal to self.playerTotalDuration ? 2、Video playback is completed ,Why the notification does not be executed ?

Please help me , Thanks.

Answer : 1、please use double or int_64 ; 2、because the notification's object exist error ! please set nil or AVPlayerItem to notification's object , can not set AVPlayer to notification's object !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM