12

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.

Community
  • 1
  • 1
Sid
  • 9,508
  • 5
  • 39
  • 60

2 Answers2

12

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).

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • Thanks! I missed this... I made the changes to the plist but it still didn't help. However this is a good start to making some progress.... – Sid Nov 11 '11 at 00:12
  • 1
    Turns out, it doesn't work in simulator but works on the device! Thank you! Next step for me is to set up a background task to run through the playlist, because it stops after playing the current track. – Sid Nov 11 '11 at 00:29
  • Once I'm done with this issue, I'm going to come back and edit this question to help the next clueless wanderer :D – Sid Nov 11 '11 at 00:29
  • 1
    And calling `[audioplayer prepareToPlay]` before backgrounding is also critical to background playback. – ZhangChn May 02 '12 at 18:24
1

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 ?

Oliver
  • 23,072
  • 33
  • 138
  • 230
  • Hi Oliver, thank you for your response. I have implemented audioPlayerBeginInterruption, and have set a breakpoint in that method. The breakpoint isn't being hit, so I don't think that's the cause, sadly. :) – Sid Nov 10 '11 at 21:39
  • @Sid : As you want, I can leave my answer there to help other ones to see the progression of your tries, or you can insert it as an edit in your question and then I can delete my answer if you want to let your question marked as "no answer". As you like. – Oliver Nov 10 '11 at 21:43