How can I allow background music to continue playing while my app still plays its sounds while using Swift

后端 未结 11 826
误落风尘
误落风尘 2020-12-04 11:57

I created an app and I am attempting to allow the user to continue to listen to their music while playing my game, but whenever they hit \"play\" and the ingame sounds occur

11条回答
  •  自闭症患者
    2020-12-04 12:48

    Here's what I am using for Swift 2.0:

    let sess = AVAudioSession.sharedInstance()
    if sess.otherAudioPlaying {
        _ = try? sess.setCategory(AVAudioSessionCategoryAmbient, withOptions: .DuckOthers)
        _ = try? sess.setActive(true, withOptions: [])
    }
    

    Please note that you can replace .DuckOthers with [] if you don't want to lower background music and instead play on top to it.

提交回复
热议问题