7

I am playing music in iphone and when I run my application it should not stop background device music and should stop its own music instead background music is getting stop. I want to do same as candy crush app. What should I do to resolve this? Please help me

    var isOtherAudioPlaying = AVAudioSession.sharedInstance().otherAudioPlaying
      println("isOtherAudioPlaying>>> \(isOtherAudioPlaying)")
      if(isOtherAudioPlaying == false)
           {
            audioPlayer1!.play()
           }
Zalak Patel
  • 1,937
  • 3
  • 26
  • 44
  • You need to set your app's audio session category and options appropriately. https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/index.html – Palpatim Jan 28 '15 at 15:39
  • It has been solved by following code:- **AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil) == true** where i am placing my movieplayercontroller to play – Zalak Patel Jan 29 '15 at 08:57

4 Answers4

2

I would suggest checking out Sameer's answer to this for Swift 2.0, just a few months back. Works perfectly with Swift 2.0.

let sess = AVAudioSession.sharedInstance()
if sess.otherAudioPlaying {
    _ = try? sess.setCategory(AVAudioSessionCategoryAmbient, withOptions: []) //
    _ = try? sess.setActive(true, withOptions: [])
}
Community
  • 1
  • 1
David West
  • 1,550
  • 1
  • 18
  • 31
1

Since the default is AVAudioSessionCategorySoloAmbient, you need to set the proper category for your app and activate it. You could use AVAudioSessionCategoryAmbient for mixing audio.

Add this in your AppDelegate.swift:

func applicationDidBecomeActive(application: UIApplication) {
  AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
  AVAudioSession.sharedInstance().setActive(true, error: nil)
  // ...
}

Enjoy!

1

Defer AVAudioSession.sharedInstance().setActive(true, error: nil) till when you need to start playing audio. Starting the session immediately after the app launches stops playback on other apps.

More details: https://developer.apple.com/documentation/avfoundation/avaudiosession

Taiwosam
  • 469
  • 7
  • 13
  • I am facing one issue here as said am setting category at initial `AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: .default)` and when I need to play audio in the app then just before that I am setting `AVAudioSession.sharedInstance().setActive(true)` but still other audio is pausing when opens the app, any idea – Ashwini Salunkhe Jan 11 '23 at 16:38
  • If you want your app's audio to mix with that of other apps, you should consider playing with other options for playback. For example, use `mixWithOthers`: `AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers])` – Taiwosam Jan 12 '23 at 17:08
  • No I don't want the mixing sound, I am expecting my ios app should trump the apple music/other music audio, if audio was initiated in my ios app but not just by opening the app. – Ashwini Salunkhe Jan 13 '23 at 14:12
0

Xcode 10.3
Swift 4.2
Tested in iPhoneX (12.2), iPhone5s(11.0.3)

Functionality -> Play audio in silent mode + with other application audio.

Here is the code,

private func setupAudioSession() {
        //enable other applications music to play while quizView
        let options = AVAudioSession.CategoryOptions.mixWithOthers
        let mode = AVAudioSession.Mode.default
        let category = AVAudioSession.Category.playback
        try? AVAudioSession.sharedInstance().setCategory(category, mode: mode, options: options)
        //---------------------------------------------------------------------------------
        try? AVAudioSession.sharedInstance().setActive(true)
    }

I will suggest you to set this function in AppDelegate -> didFinishLaunchingWithOptions....

Vatsal Shukla
  • 1,274
  • 12
  • 25
  • why did you use mixWithOthers? instead of Ambient? – Emil Jul 04 '21 at 16:00
  • @Emil **AVAudioSessionCategoryOptionMixWithOthers**Controls whether other active audio apps will be interrupted or mixed with when your app's audio session goes active. **MixWithOthers** is only valid with *AVAudioSessionCategoryPlayAndRecord*, *AVAudioSessionCategoryPlayback*, and *AVAudioSessionCategoryMultiRoute*. – Vatsal Shukla Jul 20 '21 at 10:07