Hugues Duvillier
Hugues Duvillier

Reputation: 487

Keep NSTimer going while showing video

I know there is a lot of questions about running timer in background, for instance this one : how to keep running NSTimer when app goes in background , whose answer I will certainly use if I get nothing else, but my problem is not really when the application goes in background (that is, if I understand correctly what that means).

So here it is, I have a view controller with a timer that I use as a countdown, from this view controller it is possible to see videos. I use AVPlayerViewController for that part. :

AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];

Therefore I have a AVPlayerViewController above the one owning the timer and the timer stops counting until I leave this AVPlayerViewController. I would like it to keep down counting, is there a way to do that without writing current time in a file and so on ?

Thanks for your help

Upvotes: 1

Views: 71

Answers (1)

Midhun MP
Midhun MP

Reputation: 107121

There can be several reasons for such a behaviour, but a common thing I've noticed (saw several times in my career) is invalidating the timer in the viewWillDisappear or viewDidDisappear methods and starting it on the viewWillAppear and viewDidAppear methods. If you want to run a timer until the view controller is dismissed, then you should start the timer in the viewDidLoad method and invalidate it before you dismiss the view controller (or when you get the desired result, but never try to invalidate the timer in the dealloc method)

Upvotes: 2

Related Questions