How to control MPMoviePlayerController playback progress without killing the battery?

I have a media player application that plays music using MPMoviePlayerController. I need to update the user interface based on the playback position. The best I can say is that I cannot actively get this information from a player with a callback or something else, and I basically need to interview him myself.

So I thought that I would use a simple timer that will start every second. The code is:

Somewhere in the installation code:

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updatePlaybackProgressFromTimer:) userInfo:nil repeats:YES]; 

And then:

 - (void) updatePlaybackProgressFromTimer:(NSTimer *)timer { if (([UIApplication sharedApplication].applicationState == UIApplicationStateActive) && (player.playbackState == MPMoviePlaybackStatePlaying)) { CGFloat progress = player.currentPlaybackTime / player.duration; // do something useful with this info } 

}

The timer starts every second, even if the application is in the background. First, the method sees if the application is active and the player is playing, and then updates the user interface.

Is there some time to time to use the timer every second this way? Should I be more diligent and try to demolish the timer when entering the background and activate it when the application is activated? I'm sure some battery effects, but realistic, how serious is it? Or are there any other recommended ways to do such things?

+6
source share
1 answer

I can’t imagine that using NSTimer will significantly affect battery life - unless the work done when it works does not affect battery life. A timer is simply added to the current run loop:

A timer is not a real-time mechanism; it works only when one of the launch cycle modes to which the timer has been added is working and can check if the timers have passed.

NSTimer class reference

According to the documentation, you should pause any timers when your application is about to leave its active status:

In response to this change, your application should do the following in its applicationWillResignActive: method:

  • Stop timers and other periodic tasks .
  • Stop all running metadata requests.
  • Do not start new tasks.
  • Pause movie playback (unless you play AirPlay).
  • Enter the pause state if your application is a game.
  • Cancel the frame rate of OpenGL ES with throttle.
  • Suspend send queues or operation queues that execute non-critical code. (You can continue to process network requests and other time-sensitive background tasks, while inactive.)

When your application returns to active state, its applicationDidBecomeActive: method should cancel any of the steps accepted in the applicationWillResignActive: method. Thus, on reboot, your application should restart the timers , resume sending the queue, and re-enable the OpenGL ES frame rate. However, games should not resume automatically; they must remain paused until the user decides to resume them.

IOS Application Programming Guide

+7
source

Source: https://habr.com/ru/post/917346/


All Articles