iOS AudioSessionSetActive() blocking main thread?

后端 未结 2 862
走了就别回头了
走了就别回头了 2020-12-31 11:49

in my iOS app, I\'m trying to implement \"ducking\": while my app plays a short \"command-like\" sound, any background music should be lowered in volume. Having finished pla

2条回答
  •  灰色年华
    2020-12-31 12:26

    A Swift 5 solution to this:

    // Create a background serial queue to call AVAudioPlayer.setActive() on
    queue = DispatchQueue(label: "backgroundAudio", qos: .userInitiated, attributes: [], autoreleaseFrequency: .inherit, target: nil)
    
    // ... sometime later, activate or deactivate the audio instance
    queue.async {
        do {
            try AVAudioSession.sharedInstance().setActive(false, options: [/* my options */])
        } catch {
            print("AVAudioSession.sharedInstance().setActive(false) failed: \(error)")
        }
    }
    
    

提交回复
热议问题