4

I'm trying to automatically play a sound file (that is not part of my app bundle and is not a notification sound) upon receiving a remote notification. I want this to happen whether the app is in the foreground or background when the notification is received.

I'm using the Amazing Audio Engine as a wrapper around the core audio libraries. In my App Delegate's didReceiveRemoteNotification I create an Audio Controller and add AEAudioFilePlayer to it like so:

     NSURL *file = [NSURL fileURLWithPath:sourceFilePath];
     AEAudioFilePlayer *notificationPlayer = [AEAudioFilePlayer audioFilePlayerWithURL:file
                                             audioController:_notificationController
                                                       error:NULL];
    notificationPlayer.completionBlock = ^{
    // Remove self from channel list after playback is complete
    [_notificationController removeChannels:[NSArray 
                     arrayWithObjects:notificationPlayer,nil]] ;

    };

    [_notificationController addChannels:[NSArray 
                            arrayWithObjects:notificationPlayer,nil]];

However, the sound does not play! My logs show the following errors:

    2014-08-18 06:21:28.003 MyApp[394:60b] TAAE: Setting audio session category to         AVAudioSessionCategoryPlayback
    2014-08-18 06:21:28.019 MyApp[394:60b] Couldn't activate audio session: Error                 Domain=NSOSStatusErrorDomain Code=561015905 "The operation couldn’t be completed. (OSStatus error 561015905.)"
    2014-08-18 06:21:28.024 MyApp[394:60b] TAAE: Audio session initialized (audio route                 '<AVAudioSessionRouteDescription: 0x178006720, 
    inputs = (null); 
    outputs = (
        "<AVAudioSessionPortDescription: 0x1780067f0, type = Speaker; name = Speaker; UID         = Speaker; selectedDataSource = (null)>"
    )>') 
    2014-08-18 06:21:28.034 MyApp[394:60b] 06:21:28.033 ERROR:     [0x1938e42a0] >aurioc> 783: failed: '!pla' (enable 2, outf< 2 ch,  44100 Hz, Int16, non-inter> inf< 2 ch,      0 Hz, Float32, non-inter>)
    2014-08-18 06:21:28.037 MyApp[394:60b] 06:21:28.037 ERROR:     [0x1938e42a0] >aurioc> 783: failed: '!pla' (enable 2, outf< 2 ch,  44100 Hz, Int16, non-inter> inf< 2 ch,      0 Hz, Float32, non-inter>)

Upon looking up the !pla error, I found this reference: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html

which indicates that this error type can occur if the app’s Information property list does not permit audio use, or if the app is in the background and using a category which does not allow background audio.

But my plist contains the audio as a background mode, my audio session category is AVAudioSessionCategoryPlayback and my category options are set to AVAudioSessionCategoryOptionMixWithOthers.

How do I get my sound to play when a remote notification is received?

Cray
  • 41
  • 1
  • 3

2 Answers2

5

If you call AVAudioSession:setActive:error: and AVAudioSession:setCatagory:withOptions:error: and create your AVPlayer before going into the background, you can then play your sound. For example:

    // Make sure this is run BEFORE entering background.
    [[AVAudioSession sharedInstance] setActive:YES error:&error];
    if(error != nil){
        NSLog(@"ERROR: %@", error.localizedDescription);
    }
    else {
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
        if(error != nil){
            NSLog(@"ERROR: %@", error.localizedDescription);
        }
        else {
            self._player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.urlOfFileToPlay error:&error];  
            if(error != nil){
                [[AVAudioSession sharedInstance] setActive:NO error:&error];
                NSLog(@"ERROR: %@", error.localizedDescription);
            }
        }
    }
    //You can then do this in application:didReceiveRemoteNotification:fetchCompletionHandler:
    [backgroundPlayer.player play]; 

Of course I am assuming that you already have the Capabilities for Background Modes set for Background fetch and Remote notifications.

I will also warn that if you call AVAudioSession:setActive:error: too early, other applications might change things on you.

Also, if you set your application NOT to run in the background (Info.plist: <key>UIApplicationExitsOnSuspend</key> <true/>), when it receives a "Notification" (local or remote) and you have activated an AVAudioSession, like the above code, it will play the sound in the notification payload regardless of if the application is set to silent or vibrate mode, which is actually how alarms do it. Of course this will not work if you need to poll the server in response to a remote notification. See here.

Popmedic
  • 1,791
  • 1
  • 16
  • 21
4

You cannot activate your audio session in the background, and you cannot initiate sound playback in the background. Background audio simply means that you can keep playing at the time your app goes into the background; but once your background sound has been interrupted, or if your app wasn't playing when you went into the background, you have no ability to make a sound.

And this makes sense, because it would be terrible if apps the user isn't using, and may not even be aware of, could start making noise from out of nowhere.

The usual thing is either to come to the foreground or to hand the system an immediate local notification that has a sound.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Got it, that makes sense. Could you clarify what you mean when you say "come to the foreground or to hand an immediate local notification". I presume neither of those routes would still allow me to playback automatically; is that true? – Cray Aug 18 '14 at 18:29
  • The system will play the local notification sound for you, if the user has not denied permission for this. That's the best you can do from the background. This is how timer-and-alert apps work. If you can get the user to respond to the local notification by bringing you to the foreground, now you can do anything you like. – matt Aug 18 '14 at 18:45
  • This is not true: Tile.app seems to do it, so do a lot of other applications. Please see my answer below. – Popmedic May 04 '16 at 16:09
  • This answer is not true. The reality is that background apps cannot start audio session that don't mix with the foreground app's audio session because that would interrupt the audio of the app currently used by the user. But you can activate audio session in background only with category options: MixWithOthers or DuckOthers (which is a method of mixing). Mixing categories doesn't require to interrupt other apps audio sessions. – Marcin Kapusta Jan 24 '17 at 17:06
  • It's obvious that playing audio background is possible as GPS apps are doing it. – Rafouille Aug 09 '18 at 08:19
  • @Rafouille What I said was correct in 2014 when I said it. – matt Aug 09 '18 at 15:49
  • @matt it was not true when you wrote this, "set your application NOT to run in the background" described in my answer has worked since iOS 1.0 – Popmedic Aug 08 '19 at 14:10
  • @Popmedic It is pointless to talk about `UIApplicationExitsOnSuspend` because most apps don't do that, it's irrelevant to the question, and starting in iOS 13 it will be outlawed. – matt Aug 08 '19 at 14:13