在代码中检查应用程序是否在前台运行,如果不是,则避免调用AVPlayerRateDidChangeReasonAppBackgrounded。下面是一个可能的解决方案:
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *playerItem = [notification object];
[playerItem seekToTime:kCMTimeZero];
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) {
// Avoid calling AVPlayerRateDidChangeReasonAppBackgrounded when app is not in background
self.player.rate = 1.0;
}
}
在此示例中,我们检查应用程序是否处于活动状态,如果是,则将AVPlayer的速率设置为1.0。只有当应用程序在后台运行时,AVPlayerRateDidChangeReasonAppBackgrounded才会调用。