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?