1

I'm trying to play a sound when my App is in the background. I use AVAudioPlayer, and it works correctly when the app is active, but in the background AVAudioPlayer doesn't play anything and I don't know how can I do.

I have thought that I could use UILocalNotification, but it shows and alert in the lockscreen and I don't want it.


Thanks for your answers. I have another doubt. If I create this audiosession, and I am playing music the system will mix both at the same time?.

How can I do to decrease ipod music volume will my app sound is playing (3 seconds) and increase again when it had finished like most of the running apps in the AppStore. (In the background too)

This is my code in the AppDelegate

//Creamos la sesión de audio
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

And in my view when I want to play the sound:

-(void)avisosound{

   // AudioSessionSetActive(YES);
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.wav", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 0;

    if (audioPlayer == nil){
    }
    else{
        [audioPlayer play];  
    }
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }

But when I play the sound, the ipod music stops.

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • If you want to ask a new question, you are better off creating a new question rather than adding it to an old one. You get better responses by asking a new question. – mahboudz Sep 26 '11 at 07:02
  • See here https://stackoverflow.com/questions/40875485/play-a-sound-on-my-backgrounded-app-when-the-phone-is-in-silent-mode/53168870#53168870 – Andrea Gorrieri Nov 19 '18 at 07:23

4 Answers4

4

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

Then, you need to write the following code in your AppDelegate:

   NSError *setCategoryErr = nil;
   NSError *activationErr  = nil;
   [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
   [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

Now, you can easily run audio while phone screen locks or your app is in the background.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
dhaval rupani
  • 542
  • 5
  • 8
  • Hi, I tried your code but it didn't worked. "Set Application does not run in background to YES" shouldn't it set to NO? and for "Required background mode to App plays audio" what should I put in the the item[0]? – shoujo_sm Sep 04 '13 at 08:05
  • @dhaval rupani I am seeing your answer with start playing sound for 3rd times and everywhere it has checkmarks or upvotes, but I still did not get how do you START playing sound? Please, reveal the beans and make clarifications. – Neil Galiaskarov Jul 13 '14 at 06:00
1

The following tells the system to continue playing the audio even when your iPhone screen locks out:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];

(Most of the time you put this in your didFinishLaunchingWithOptions.)

And to tell the system to play your audio when your app is in the background, add UIBackgroundModes and set it to audio in you plist.

(Any foreground apps that need to play audio will take precedence over these settings and your app's sound playback).

To add "ducking", to lower the volume of other audio while your audio is playing, you need to implement kAudioSessionProperty_OtherMixableAudioShouldDuck. Look for that in the Audio Session programming guide.

mahboudz
  • 39,196
  • 16
  • 97
  • 124
1

To play a sound in the background (assuming your app has set up the proper background modes), your app has to start the sound in the foreground and not stop it. The easiest ways to do this are using the Audio Queue API or the RemoteIO Audio Unit.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
0

There's an article explaining what you can do in the iOS developer library. See http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html

klaustopher
  • 6,702
  • 1
  • 22
  • 25