How to stop MPMusicPlayerController from enabling screen locking
Asked Answered
I

4

9

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?

Isometropia answered 8/2, 2010 at 13:33 Comment(3)
Have you set the idleTimerDisabled to YES?Waite
Yep. The idleTimerDisabled is reset to NO when MPMusicPlayerController kicks on and you can't disable it again.Isometropia
Any luck with this problem? Tried all the solutions but none of them worked.Encomium
U
5

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.

Urge answered 25/7, 2010 at 22:6 Comment(1)
henning77, thanks for this!! I managed to get it to work in my new app using your method. Many people misunderstood the question/problem as a normal way how to disable idletimer. But it is about AFTER PLAYING MPMUSICPLAYER issue. The idleTimerDisabled = NO; is indeed crititcal. It wont work without it.Theretofore
R
1

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.

Redfin answered 8/2, 2010 at 14:2 Comment(1)
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 againIsometropia
C
1

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");}
Caulis answered 11/6, 2010 at 13:55 Comment(0)
H
0
let player = MPMusicPlayerController.applicationMusicPlayer()

player.setQueueWithStoreIDs(["some id"])

player.play()

player.pause()
Hostetler answered 16/3, 2021 at 4:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.