0
votes

I have an app that plays audio.

I'm trying to get all "edge cases" taken care of before app release.. no matter how slight.

As you may know... apps that play audio need to tell the IOS system how they intend to behave. Does their audio interrupt other apps? Do they mix with other apps? etc. etc.

So, the app needs to have its AVAudioSession code set up correctly... which I did according to Apple's docs:

import UIKit
import AVFoundation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

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

        // Override point for customization after application launch.

        let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(AVAudioSessionCategoryPlayback)
        } catch {
            print("Setting category to AVAudioSessionCategoryPlayback failed.")
        }

        do {
            try audioSession.setActive(true)
        } catch {

            print("Setting AudioSession active failed.")

        }
        // Other project setup
        return true
    }

}

Imagine the following test:

1) Turn on bluetooth and connect to a bluetooth speaker.

2) Load a known-good app that plays audio while backgrounded, like Spotify. Play a song/audio and background it.

3) While the audio from APP B is still playing, load APP A (my app) and press any of the three buttons.

So... after executing step 3, what happens?

Well, that depends on what you are using... for example:

  • If you're just using the phone speaker (not an external Bluetooth speaker), everything works fine, as expected.

  • If you are using, for example, an ION Tailgater iPA77 Bluetooth speaker, everything works fine as expected.

  • BUT, if you are using, for example, an MTX MUDHSB-B Bluetooth speaker, then NOTHING HAPPENS when you press any of the three buttons... until you continue waiting and/or pressing buttons for a seemingly arbitrary amount of time (about 30 seconds)... then the sound will start to come through and behave normally.

How is it that one Bluetooth speaker would behave differently than another (presumably with respect to AVAudioSession)? Bluetooth is bluetooth, right? I guess not. Can anyone shed light on this?

Thanks.

2
Sound like a problem with the speaker somehow... did you try the same scenario with 2 apps that are not yours? For example Spotify and Apple Music?Jan
Yes thanks... I just tested on that speaker with other apps... seems youtube (of course) works correctly but other non-rocket scientist apps that I have do the same thing as mine... so it's not my app specifically. Not sure whether to delete question... if you put this as an answer I will accept.Nerdy Bunz

2 Answers

1
votes

Sound like a problem with the speaker somehow... did you try the same scenario with 2 apps that are not yours? For example Spotify and Apple Music?

0
votes

You need to clear the AVAudioSessionCategoryOption to AVAudioSessionCategoryOptionMixWithOthers

If you check at the documentation, at the bottom you see this:

If this option is cleared, activating your session interrupts other audio sessions. If this option is set, your app's audio is mixed with audio playing in background apps (such as the Music app).

Another option, based on your code, is that you are not activating the audio session after setting the category:

audioSession.setActive(true)