0

I have to give audio feedback when my app is in foreground and also in background. But when the app enters background audio feedback is not heard. In info.plist I have set background mode to App plays audio or streams audio/video using AirPlay and used the following but the audioPlayerDidFinishPlaying delegate is not called when app enters background and audio is not heard.

 AVAudioPlayer *sound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
        sound.delegate = self;
        ///Fixed the issue No audible feedback when main audio is silent
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        [[AVAudioSession sharedInstance] setActive: YES error: nil];
        // This is necessary if you want to play a sequence of songs, otherwise your app will be
        // killed after the first one finishes.
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

        [soundQueue addObject:sound];
codercat
  • 22,873
  • 9
  • 61
  • 85
  • Possible duplictate http://stackoverflow.com/questions/10429204/how-to-handle-background-audio-playing-while-ios-device-is-locked-or-on-another – iCoder Jan 21 '14 at 07:15

1 Answers1

1

You need to make couple of changes in plist file.

1) Set Required background mode to App plays audio

2) set Application does not run in background to YES.

NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

Then, you need to write these much code in AppDelegate

Now, you can easily run audio while phone screen locks or in background.

It works fine for me. :)

for more please refer to Playing Audio in background mode and Audio Session Programming Guide

Prabhu Natarajan
  • 869
  • 1
  • 9
  • 13