user3129761
user3129761

Reputation: 61

Audio feed back in foreground and background

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];

Upvotes: 0

Views: 172

Answers (1)

Prabhu Natarajan
Prabhu Natarajan

Reputation: 869

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

Upvotes: 1

Related Questions