简体   繁体   中英

Mixing Audio on ios

I'm trying to mix audio sources such that there is no audio ducking. The use case I'm trying to solve is having an audio call ongoing in the background while also playing music and/or listening to app audio, without any audio ducking. Audio ducking is when the audio is lowered and distorted slightly to help with the listening of another audio session. Can I have unducked audio and audio mixing (or iOS for that matter)?

I have tried the following code. I have also tried wrapping it in a DispatchQueue.main.async closure, but still no luck.


// I have also tried this code with this block uncommented
// do {
//     try audioSession.setActive(false, options: .notifyOthersOnDeactivation)
//
// } catch let activationError {
//     print(activationError)
//     print(activationError.localizedDescription)
// }


let audioSession = AVAudioSession.sharedInstance()
do {
    try audioSession.setCategory(
        AVAudioSession.Category.playAndRecord,
        mode: AVAudioSession.Mode.voiceChat,
        options: [.allowBluetoothA2DP, .allowBluetooth, .mixWithOthers, .allowAirPlay]
    )
} catch let setCategoryError {
    print(setCategoryError)
    print(setCategoryError.localizedDescription)
}

do {
    try audioSession.setActive(true, options: .notifyOthersOnDeactivation)

} catch let activationError {
    print(activationError)
    print(activationError.localizedDescription)
}

The answer appears to be that the AVAudioSession.Mode is automatically setting some options under the hood. Apple seems to have made the design decision that the .voiceChat mode should always duck others for the best experience. This is similarly true in the .videoChat mode, both of which are the obvious modes to use with an Agora call. Using those modes seems to automatically set the .duckOthers flag.

I was able to solve this issue by setting the mode to .videoRecording , which is designed for apps that are recording audio and video, and therefore doesn't automatically set .duckOthers . Because this isn't actually what you're doing, it's definitely a workaround, and might have other side effects.

Given what you're doing with the AVAudioSession, you may also want to prevent Agora from changing it out from under you. You can call agoraKit.setAudioSessionOperationRestriction(.all) to prevent Agora from altering the global AVAudioSession without your permission, which may be necessary to make this work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM