How to stop MPMusicPlayerController from enabling screen locking

前端 未结 3 1512
逝去的感伤
逝去的感伤 2021-02-06 02:58

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

相关标签:
3条回答
  • 2021-02-06 03:45

    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.

    0 讨论(0)
  • 2021-02-06 03:48

    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");}
    
    0 讨论(0)
  • 2021-02-06 03:51

    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.

    0 讨论(0)
提交回复
热议问题