应用变为有效后,AVPlayer视频无法恢复

时间:2016-07-30 06:04:18

标签: ios objective-c avplayer avplayerviewcontroller

AVPlayer视频没有从它离开的地方恢复,而是视频从头开始。这是我的代码。任何人都可以帮助我?

-(void)appEnteredForeground:(NSNotification*)notification {
    if(playerViewController.player.status == AVPlayerStatusReadyToPlay && 
       playerViewController.player.currentItem.status == AVPlayerItemStatusReadyToPlay) {
          total_duration = self.playerViewController.player.currentItem.duration;
          [self.playerViewController.player seekToTime:currentTime];
          [_playerViewController.player play];
    }
}    

-(void)appEnteredBackground:(NSNotification*)notification {
    [playerViewController.player pause];
    currentTime = [playerViewController.player currentTime];
    [playerViewController.player seekToTime:currentTime];
}

1 个答案:

答案 0 :(得分:0)

1.它寻求时间问题,当你要求它寻求时间时,它不能完成,因为它已经在后台。当你进入背景时,你为什么要寻找时间?暂停应该做得很好。删除以下行。

[playerViewController.player seekToTime:currentTime];

2.你最好暂停辞职主动通知而不是背景通知。因为在按下主页按钮后几秒钟内会触发最后一个

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(becomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignActive) name:UIApplicationWillResignActiveNotification object:nil];
}

- (void)becomeActive {
    NSLog(@"active");
    if(playerViewController.player.status == AVPlayerStatusReadyToPlay && 
       playerViewController.player.currentItem.status == AVPlayerItemStatusReadyToPlay) {
          total_duration = self.playerViewController.player.currentItem.duration;
          [self.playerViewController.player seekToTime:currentTime];
          [_playerViewController.player play];
    }
}

- (void)resignActive {
    NSLog(@"resign active");
    [playerViewController.player pause];
    currentTime = [playerViewController.player currentTime];

}