7

It's working correctly if the app just become inactive/active (when some alert came or by double clicking on home button)

AVPlayer *player;

- (void)applicationWillResignActive:(UIApplication *)application
{
    [player pause];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [player play];
}

If the app goes to background (by clicking home button) and comes back, it's not playing from paused position instead of that it is playing from different point (sometimes from start, sometimes from middle).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chandan Shetty SP
  • 5,087
  • 6
  • 42
  • 63

2 Answers2

22

Follow below steps:

1) Firstly add float *time in appDelegate.h file.

2) Take current time in applicationWillResignActive

3) Add below methods

ApplicationWillResignActive method pause player and save current time of player

- (void)applicationWillResignActive:(UIApplication *)application
{
   [player pause];
   time = player.currentTime
}

Now in applicationDidBecomeActive add seekToTime

- (void)applicationDidBecomeActive:(UIApplication *)application
{
  [player seekToTime:time toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
  [player play];
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • 2
    Earlier i was doing the same seekToTime didn't work for me, apple is doing some optimization while seeking. Instead of that we have to use [_videoPlayer seekToTime:_pausedTime toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero]; to work correctly – Chandan Shetty SP Oct 30 '12 at 10:43
1

You should call the this methods before application is going to background. [player pause]; or [player stop];

IOS Rocks
  • 2,127
  • 2
  • 21
  • 24