0

I'm currently trying to implement a system like the alarm one to alert the phone when an event occurred (in my case a bluetooth event). I want this alert to occur even if the phone is in silent and in background.

I create a local notification but i can't get sound played if the phone is in silent mode (which seems to be normal since we put the phone in silent).

So i tried to manage the sound by myself and i'm struggling with playing sound in background. So far i implement the "App plays audio or streams audio/video using AirPlay" key in my plist and i'm using this code.

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
BOOL result = [audioSession setActive:YES error:&error];
if ( ! result && error) {
    NSLog(@"Error For AudioSession Activation: %@", error);
}

error = nil;
result = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
if ( ! result && error) {
    NSLog(@"Error For AudioSession Category: %@", error);
}

if (player == nil) {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bell" ofType:@"wav"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *err  = nil;
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
    if(err)
    {
        NSLog(@"Error Player == %@", err);
    }
    else {
        NSLog(@"Play ready");
    }
}

[player prepareToPlay];
[player setVolume:1.0];

if([player play])
{
    NSLog(@"YAY sound");
}
else {
    NSLog(@"Error sound");
}

The sound works great in foreground, even in silent mode, but i got no sound at all in background. Any ideas ?

Thanks

EDIT: Finally i got it working with the above code. The only missing point is that i was trying to play the sound in somewhat appeared to be a different thread, when i play it right in my bluetooth event and not my function call it's working.

M to the K
  • 1,576
  • 3
  • 17
  • 27
  • 1
    Did you tried to use `applicationDidEnterBackground:` method ? – Himanshu Joshi Mar 05 '14 at 09:23
  • This method is called when the app goes in background to perform some long operation (like saving). In my case the notification can came long time after this (but my app could perform some operations since it's a bluetooth event) – M to the K Mar 05 '14 at 10:09

3 Answers3

0

An app that plays audio continuously (even while the app is running in the background) can register as a background audio app by including the UIBackgroundModes key (with the value audio) in its Info.plist file. Apps that include this key must play audible content to the user while in the background.

Apple reference "Playing Background Audio"

Ensuring That Audio Continues When the Screen Locks

Found here

Community
  • 1
  • 1
Javier Cancio
  • 1,418
  • 12
  • 18
0

I'm not sure if your problem is that you have not configure correctly the audio session output. I had similar problem while playing .wav in my app. I only listened them with earphones. This method helped me:

- (void) configureAVAudioSession {
    //get your app's audioSession singleton object
    AVAudioSession *session = [AVAudioSession sharedInstance];

    //error handling
    BOOL success;
    NSError* error;

    //set the audioSession category.
    //Needs to be Record or PlayAndRecord to use audioRouteOverride:
    success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
                             error:&error];

    if (!success)  NSLog(@"AVAudioSession error setting category:%@",error);

    //set the audioSession override
    success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
                                         error:&error];
    if (!success)  NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);

    //activate the audio session
    success = [session setActive:YES error:&error];
    if (!success) NSLog(@"AVAudioSession error activating: %@",error);
    else NSLog(@"audioSession active"); }

Try it out!

EnriMR
  • 3,924
  • 5
  • 39
  • 59
0

You need to make couple of changes in plist file.

for enabling sound when the app enter's Background.

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