1

I need to continue the playback even when the user locks the phone. I've seen some solution over the internet, but none of them seem to work, such as:

AudioSessionSetActive(true);
    // Set up audio session, to prevent iPhone from deep sleeping, while playing sounds
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty (
                             kAudioSessionProperty_AudioCategory,
                             sizeof (sessionCategory),
                             &sessionCategory
                             );

I'm inlcuding AVFoundation, AudioToolbox, and MediaPlayer.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
gabriel_vincent
  • 1,230
  • 3
  • 16
  • 35

3 Answers3

5

I figured out how to do this.

First of all, include these frameworks to your project: AudioToolbox, CoreAudio, MediaPlayer and AVFoundation. Import them all to the viewController where your player will be placed. After you allocated and started playing the audio, insert the following code:

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);
AudioSessionSetActive(true);

And finally, go to your app Info.plist file and add a row named UIBackgroundModes. The new row will be an array and will contain 1 item, the item 0. To this you just set the value as audio. And you're done! Enjoy you're background audio playing app!

gabriel_vincent
  • 1,230
  • 3
  • 16
  • 35
0

Refer following Links:

How to Continue Music Play after Screen Auto-Lock

Playing audio while iPhone is locked

how to play music using AVAudioPlayer when screen os locked

Try :

// Set up audio session, to prevent iPhone from deep sleeping, while playing sounds
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;

AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);

AudioSessionSetActive(true);
Community
  • 1
  • 1
Chetan Bhalara
  • 10,326
  • 6
  • 32
  • 51
  • I've already tried all these links before and I still can't get it to work. – gabriel_vincent Jan 19 '12 at 13:38
  • @gabriel_vincent try // Set up audio session, to prevent iPhone from deep sleeping, while playing sounds UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback; AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory); AudioSessionSetActive(true); – Chetan Bhalara Jan 19 '12 at 14:08
  • Tried it too. Didn't work. I'm placing it after allocating the audioPlayer and before playing the audio. Is it wrong? – gabriel_vincent Jan 19 '12 at 14:13
  • That's all: http://i.imgur.com/V8jlh.png I'm sorry it's a screenshot, but it was too long toa Stack Overflow's Comment – gabriel_vincent Jan 19 '12 at 14:18
0

If you have a playlist for example and want to play the next song while in background mode or lock mode add this line of code on your viewDidLoad:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

This makes the app supports remote control events.

Andre Cytryn
  • 2,506
  • 4
  • 28
  • 43