Can audio play simultaneously with Spotify in iOS?

Hey everyone, I’m working on an iOS app and I’ve hit a roadblock. My client wants the app to play sounds at the same time as Spotify is playing music. I’m not sure if this is even possible in Swift. Has anyone done something like this before? I’m really curious about how the audio layers would work together. Would the app sounds interfere with Spotify playback? Any tips or advice would be super helpful. I’ve been scratching my head over this for a while now!

yo harry, i’ve messed around with this before. it’s totally doable! u gotta use AVAudioSession and set it to .ambient category. that way ur app sounds can play nice with spotify. remember it works best for small sounds, not full-on music tracks. good luck with ur project man!

I’ve actually tackled a similar challenge in one of my recent projects. It is indeed possible to play audio simultaneously with Spotify on iOS, but it requires some careful implementation.

The key is to use AVAudioSession correctly. You’ll want to set the audio session category to AVAudioSessionCategoryAmbient. This allows your app’s audio to mix with other audio sources.

Here’s a snippet of code that worked for me:

do {
    try AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default)
    try AVAudioSession.sharedInstance().setActive(true)
} catch {
    print("Failed to set audio session category.")
}

Remember, this approach works best for non-intrusive sounds like UI effects or background ambiance. For more complex audio mixing, you might need to dive deeper into AVAudioEngine.

Hope this helps point you in the right direction!