Playing audio while iPhone is locked

Featured Image

One more feature that Apple has provided to iDevelopers that is very useful. If you are building something like audio player iphone application and if you want player to play even if iPhone is locked then you must use this code below:

UInt32 category = kAudioSessionCategory_MediaPlayback;    //1
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (category), &category);   //2
AudioSessionSetActive (true);     //3

Code explanation :

1. Set the audio session category – kAudioSessionCategory_MediaPlayback.
2. Will ensure our audio playback contiues when the device is locked.
3. Activate the audio session immmediately before playback starts.

Just put this code in play method definition in implementation of your player class.
And that is all.


Care to rate this post?


Ivan Kalaica

Ex. Inchooer

At Inchoo, Ivan Kalaica was an iOS SDK developer. He worked with us from 2008. to 2012.

Other posts from this author

Discussion 2 Comments

Add Comment
  1. I am sorry – but where exactly do you place this? Can’t figure it out. Thanks.

  2. Ivan Kalaica

    Hello Sean,

    You can put it where ever you want in code implementation. For example in constructor of some class (-(id)init…), in method where you create instance of AVAudioPlayer or in method where you trigger play method of same instance.


Add Your Comment

Please wrap all source codes with [code][/code] tags.