Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer Pause and play issues when app goes background

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).

like image 516
Chandan Shetty SP Avatar asked Oct 30 '12 06:10

Chandan Shetty SP


2 Answers

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];
}
like image 79
Paresh Navadiya Avatar answered Oct 20 '22 19:10

Paresh Navadiya


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

like image 31
IOS Rocks Avatar answered Oct 20 '22 19:10

IOS Rocks