views:

384

answers:

5

I have an application that requires the iPhone screen to remain active (or not, depending on user choice). I've done this by disabling the application idle timer, which works fine and dandy until I start playing media via the MPMusicPlayerController. Due to a bug in the SDK, this then reenables the idle timer with no apparent way to disable it again.

My app flow is:

  1. App starts
  2. Screen stays on
  3. <...time passes...>
  4. Play audio file
  5. Idle timer kicks in
  6. Screen turns off

I have an empty audio file playing in the background to stop the phone going into deep sleep, but I'd really like to keep the screen unlocked too.

Has anyone managed to figure out a workaround for this?

A: 

You should simply turn off the idle timer. What I usually do in a viewcontroller that needs to stay 'awake' is this:

- (void) viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setIdleTimerDisabled: YES];
}

- (void) viewWillDisappear: (BOOL) animated
{
    [[UIApplication sharedApplication] setIdleTimerDisabled: NO];
}

This will make sure the screen will not get locked due to user inactivity.

St3fan
St3fan, I've done this on app startup, verified by the fact the phone doesnt sleep until music starts playing. As I stated, as soon as the MPMusicPlayerController activates, it reenables the idle timer and you can't disable it again
MrWiggles
A: 

Did you guys ever find a fix for this? Like it was presented, disabling the idletimer works until music or other media starts to play.

Ernie Svehla
Afraid not. I haven't upgraded to 3.1.2 yet so it's possible it has been fixed there?
MrWiggles
A: 

I found a solution to this problem. Invoke a method that disables the idleTimer in about 5 seconds after you start playing the music. It's a bit of a hack, but it is a workaround.

[[SoundEngine mainEngine] playMusic];

[self performSelector:@selector(setIdleTimeDisabled) withObject:nil afterDelay:5.0];

- (void) setIdleTimeDisabled {
[UIApplication sharedApplication].idleTimerDisabled = YES;
NSLog(@"Setting idleTimer to TRUE");}
astar
A: 

astar - this does not fix the problem for me. Once I start playing music, I can't seem to be able to successfully set idleTimerDisabled to true. I've tried calling it immediately, and also using your 5-second delay method. The screen still goes dark when the user's lock timer setting kicks in. Did this really fix it for you?

gbach
+1  A: 

I had a similiar problem, and found a fix for it. The fix might work for you too:

I call a method periodically (every 10 seconds), which sets idleTimerDisabled first to NO, then to YES.

- (void)calledEveryTenSeconds
{
    [UIApplication sharedApplication].idleTimerDisabled = NO;
    [UIApplication sharedApplication].idleTimerDisabled = YES;
}

Only setting to YES alone does not fix the problem. It seems the property has to change first to be recognized by UIApplication.

My problem was, that the screen kept turning dark as soon as I switched music tracks on the iPod player via the headphone remote. My guess is, that this is the same issue as you are experiencing.

henning77