Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioPlayer stops playing on screen lock even though the category is AVAudioSessionCategoryPlayback

My app uses AVAudioPlayer to play through audio playlists. I haven't changed the code between iOS4.3 and iOS5. However, the audio on iOS5 pauses on a screen lock even though I've set the category to be AVAudioSessionCategoryPlayback.

I've tested the code on iOS4.3 and iOS5 devices and this problem occurs on the iOS5 device.

Has anyone else faced this problem? I'm at a loss since I don't even know where to start debugging this issue. My debugging started and ended with checking the AVAudioSessionCategory setting.

The code where I'm doing it is:

[[AVAudioSession sharedInstance] setDelegate: self];
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];

Edit:

I have already implemented and handled audioPlayerBeginInterruption

Edit #2: Solved! Thanks to Rhythmic Fistman's Answer

Here's what was happening and what I did. iOS5 changes now push your app to the background even when the screen gets locked. This means that you need to enable background audio in your app, and make whatever view/viewcontroller handling that audio to be the first responder.

Step 1: Enable background audio in your app

Step 2: Enable remote events and become the first responder on the viewcontroller / view responsible for handling your audio playlist

NOTE: Make sure to test this on the device. The simulator will make it seem like the code did not work.

like image 465
Sid Avatar asked Nov 10 '11 19:11

Sid


2 Answers

Screenlock has changed in iOS5, it now sends your app to the background, so if you want your audio to continue you must add "audio" as a UIBackGroundModes to your Info.plist file.

The closest thing I can find to documentation of this are a few Apple devforum posts (login required).

like image 193
Rhythmic Fistman Avatar answered Nov 15 '22 23:11

Rhythmic Fistman


I'm not very used with audio, but did you look about interuptions ? I've read something that said (in my memories) that the audio could be stopped when en interruption happens, and it's your job to make it play again.

But it's just something coming from my memory. Does that help ?

like image 29
Oliver Avatar answered Nov 15 '22 23:11

Oliver