在 iOS 中的应用程序上工作,我正在使用 AVPlayer 播放视频,并且我正在使用以下方法将滑块与 playerItem 当前持续时间同步:
//synchronizing the current time label and slider with the player.
[self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 100) queue:dispatch_get_main_queue() usingBlock:^(CMTime time)
{
CMTime endTime = CMTimeConvertScale (self.player.currentItem.asset.duration, self.player.currentTime.timescale, kCMTimeRoundingMethod_RoundHalfAwayFromZero);
if (CMTimeCompare(endTime, kCMTimeZero) != 0) {
double normalizedTime = (double) self.player.currentTime.value / (double) endTime.value;
self.slider.value = normalizedTime;
}
Float64 currentSeconds = CMTimeGetSeconds(self.player.currentTime);
int mins = currentSeconds/60.0;
int secs = fmodf(currentSeconds, 60.0);
NSString *minsString = mins < 10 ? [NSString stringWithFormat:@"0%d", mins] : [NSString stringWithFormat:@"%d", mins];
NSString *secsString = secs < 10 ? [NSString stringWithFormat:@"0%d", secs] : [NSString stringWithFormat:@"%d", secs];
self.currentTimeLabel.text = [NSString stringWithFormat:@"%@:%@", minsString, secsString];
}];
问题是我在模态视图中播放视频,当我按下按钮关闭视图控制器时,视图控制器关闭但视频继续在后台播放(因为可以听到资产的音频)。我该如何解决?我确信它与我正在使用的队列有关,但我已经尝试过主队列和并发队列,但每次结果都是一样的。