Playing audio while iPhone is locked

2 Comments 23rd JAN 2009 | Posted by Ivan Kalaica in iPhone dev

Playing audio while iPhone is locked

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.

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • NewsVine
  • Sphinn
  • Tumblr
  • Posterous

To post code in comments, place your code inside [code] and [/code] tags.

There are 2 comments (Add Yours +)

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

  • Ivan Kalaica Says

    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

Leave a Comment

Please wrap all source codes with [code][/code] tags.
Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009