Thursday, December 2, 2010

How to Continue Music Play after Screen Auto-Lock

Your application is playing music - until screen auto-lock stops it.

This happens, because by default your application is using AVAudioSessionCategorySoloAmbient audio session category.

You could disable auto-lock, but that would decrease battery life. Might be better to continue music playing, regardless of auto-lock:
#import <AVFoundation/AVFoundation.h>

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:NULL];
Now your application works in same as iPhone music player (iPod). Please read more technical info about Audio Session Programming at Apple iOS Reference Library.

4 comments:

  1. Is this still the same with ios 5?

    ReplyDelete
  2. That feature was never released. Some testers thought that audio effects in chat application were annoying :) So cannot test that anymore, sorry to say! Should work, but got to try it first.

    ReplyDelete
    Replies
    1. So does it actually work? I tried here in my app and It doesn't seem to work =(

      Delete
  3. Well, it did work pre-iOS5. Haven't tried playing music since then - except previous project should have tested for that! Got to tell my collegue, who's still doing maintenance for old project, while busy in the new one :)

    Btw iOS5 changed the meaning of screen-saver activation. Now it puts apps to background and should stop music playing - unless your app is requesting "audio" background task capability. Check your info.plist and whether UIBackgroundModes is audio? Can't recall now if you got to restart music playing at applicationDidEnterBackground...

    ReplyDelete