19

I have an exercise app which needs to play sound. I use AVAudioPlayer to play the sound. But when the audio starts to play, the background music from another app (radio streaming app) is shutdown.

How can I make it not interrupt the background music? Because I'd like the user to hear music while doing exercise.

Thank you.

8vius
  • 5,786
  • 14
  • 74
  • 136
user1519520
  • 191
  • 2
  • 5
  • 1
    possible duplicate of [iPhone AVAudioPlayer stopping background music](http://stackoverflow.com/questions/1672602/iphone-avaudioplayer-stopping-background-music) – benzado Jul 12 '12 at 02:38
  • you need to use a different type of audio session – bkbeachlabs Jul 12 '12 at 03:00

5 Answers5

26

You can use the following code where you use AVAudioPlayer:

    // Set AudioSession
    NSError *sessionError = nil;
   [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
   [[AVAudioSession sharedInstance] setActive:YES error:&sessionError];

If you wish to set a delegate, you can add this line of code:

   [[AVAudioSession sharedInstance] setDelegate:self];
Zoyt
  • 4,809
  • 6
  • 33
  • 45
Sujith Thankachan
  • 3,508
  • 2
  • 20
  • 25
  • Do you really need to set the delegate. It should be enough to set the category. – Johan Karlsson Jan 08 '13 at 12:12
  • I have tested the code and only a call to setCategory is needed. Changed the sample code accordingly. – Johan Karlsson Jan 08 '13 at 13:12
  • When audio from other apps played in background, my app would crash with the reason: 'required condition is false: _engine->IsRunning()'. This solved that issue. – vikzilla Sep 07 '15 at 07:26
  • Is there any way to prevent stopping other audio that's playing when my app is opened if I'm using the playback category? – vikzilla Feb 16 '16 at 05:57
  • @vikzilla check this answer http://stackoverflow.com/a/25152431. It may help you. – Sujith Thankachan Feb 26 '16 at 09:10
  • I open my app , music from background app stopped. below solutions is not working for me [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil]; guys any suggestion help? above solution is working when we do not use any AVAudioPlayer or AudioServicesCreateSystemSoundID – Ankur Patel Jan 03 '19 at 07:20
7

In my case, I wanted the background audio to be played at a lower volume, as the sound played by my app is more like an alert. I use this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // prevents our app from stopping other audio,
    // e.g. music, podcats, etc. that may be playing when launched
    // our audio will be played at a higher volume
    // and the background audio will "duck" until done
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryMultiRoute 
                                     withOptions:AVAudioSessionCategoryOptionDuckOthers
                                           error:nil];
}
charles
  • 11,212
  • 3
  • 31
  • 46
6

Based on SujithPt's Answer here is the same thing in swift:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
Sujith Thankachan
  • 3,508
  • 2
  • 20
  • 25
inVINCEable
  • 2,167
  • 3
  • 25
  • 49
4

For Swift 4.2

import AVFoundation

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    do {
        try AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default, options: AVAudioSession.CategoryOptions.mixWithOthers)
    } catch let error {
        print(error.localizedDescription)
    }

    return true
}
Aleksey Shevchenko
  • 1,159
  • 1
  • 11
  • 23
1

For Swift 2.2, add this anywhere before you play or preparetoplay:

_ = try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168