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.

2
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Ivan Kalaica

Senior OSX and iOS SDK Developer

At Inchoo, Ivan Kalaica is a Senior OSX and iOS SDK developer.

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.

    Cheers,
    Ivan

Add Your Comment

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