2

I have audio, fetch and remote-notification set in UIBackgroundModes and I successfully receive remote notifications with my app in the background (not active) via:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

I have the following in my: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:

self.audioPlayer = [[AVPlayer alloc] init];

NSError *sessionError = nil;
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive:YES error:&activationError];
if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&sessionError]) {
    NSLog(@"[AppDelegate] Failed to setup audio session: %@", sessionError);
}

And in - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler I have the following:

            NSLog(@"Playing url: %@", filePath);

            AVPlayerItem * currentItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:filePath]];

            [self.audioPlayer replaceCurrentItemWithPlayerItem:currentItem];
            [self.audioPlayer play];

I see this code execute via NSLog but no sound is produced. Actually, if the app receives a notification within a few seconds of going to the background, audio does play, or ie. the first time it gets a notification audio plays, but never after that.

Can an app in iOS 7 initiate audio output asynchronously like this, from the background, ie. after it has been asleep and not produced any audio for some time?

1 Answer 1

0

You cannot initiate audio in the background. The only thing the audio background mode allows you to do is to continue producing sound as the app goes from the foreground to the background.

And this is a perfectly reasonable rule. It would be terrible if any app that happened to be running in the background could suddenly start producing sound out of the device whenever it likes, to the mystification and annoyance of the user!

However, if your app is capable of receiving remote events, and if it has produced sound so that it is the remote event target, then, with audio background mode, it can go on being the remote event target and thus can produce sound in the background, as long as no other app becomes the remote event target in the meantime.

The most reliable way to produce a sound while in the background is by attaching the sound to a local notification.

2
  • Thanks. Can a local notification play a sound resource from the app's NSDocumentDirectory or only compiled-in resources?
    – mrblog
    Jan 7, 2014 at 4:57
  • 1
    Must be in app bundle, and you can see why: the local notification might fire days from now, so we need to guarantee that the sound file will still be there. Also, it must be a system alert type of sound (i.e. aiff/wav), because no sound decoding resources are going to be brought into play for you. And of course the sound must be short.
    – matt
    Jan 7, 2014 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.