JeffB6688
JeffB6688

Reputation: 3890

MPMoviePlayerController won't play during screen-lock in ios5

My app used the MPMoviePlayerController to play podcasts. In ios4, I was able to circumvent the lock-screen halting the play of podcasts with the following code:

OSStatus audioInitStat = AudioSessionInitialize (NULL,NULL,NULL,NULL);
if (audioInitStat != kAudioSessionNoError) {
    printf("AudioSession Failed to Initialize\n");
}
else {
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    audioInitStat = AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,    sizeof(sessionCategory), &sessionCategory);
}

It may have been dumb luck that it worked before, but the fact is that now with the introduction of ios5, the movie player stops with screen lock when it did not in ios4. Does anyone know how to allow the movie player to play during screen-lock. I do not want to use:

[[ UIApplication sharedApplication ] setIdleTimerDisabled: YES ];

because that will just kill the battery (i.e. I want to allow the screen to dim). Any ideas would be most appreciated. If I can't use the movie player, do you know if I can use any other audio capabilities of ios to play podcasts?

Upvotes: 1

Views: 1477

Answers (2)

Hivebrain
Hivebrain

Reputation: 784

In iOS 5.x you need to set two keys in your info.plist file.

First add a row to the file with the Key "UIApplicationExitsOnSuspend" make the value a Boolean and set it to "NO"

Next add a row with a key named "UIBackgroundModes"
This will be an Array and you want the first item to have a value of "audio"

You might also want to set your audio session to AVAudioSessionCategoryPlayback for good measure:

    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

This will work for audio files played back using MPMoviePlayerController but not video.

Upvotes: 1

JeffB6688
JeffB6688

Reputation: 3890

After much digging, I found a partial solution:

Go to your info.plist for your app.

Add the key 'Required background modes' and set the value to 'App plays audio' Add the key 'Application does run in background' and set the value to 'YES'

I have no idea where this is documented, but it works.

This is only a partial solution. With this, audio can play in the background, but you can't press the home button to access other apps. The app will terminate when you press the Home button.

Upvotes: 0

Related Questions