0

AVPlayer video not resuming from where it left, instead video starts from beginning. Here is my code.Can anyone help me??

-(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];
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
Bharu
  • 159
  • 1
  • 3
  • 13

1 Answers1

0

1.It's seek to time problem, when u ask it to seek to time, it can't be done as it's already in background. Why did u seek to time when u enter background? pause should do just fine. remove the following line.

[playerViewController.player seekToTime:currentTime];

2.you had better pause in resign active notification instead of background notification. because the last one shall be triggered in a few seconds later after pressing the home button

- (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];

}
ronan
  • 1,611
  • 13
  • 20