using the CallKit to hook into the LiveKit connect...
# ask-ai
m
using the CallKit to hook into the LiveKit connection, but when establishing the audio session, it always seems to connect to the Built-in Speaker rather than the Built-in Receiver. is there a way to ensure LiveKit uses the Built-in Receiver of the iphone?
t
Thanks for reaching out, Nate. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I understand you'd like to ensure LiveKit uses the Built-in Receiver (earpiece) instead of the Built-in Speaker on iPhone when using CallKit. Let me guide you through the steps to properly manage the
AVAudioSession
configuration to achieve this: 1. Disable LiveKit’s Automatic Audio Session Configuration: To start, prevent LiveKit from automatically configuring the audio session by adding this early in your app lifecycle:
AudioManager.shared.audioSession.isAutomaticConfigurationEnabled = false
2. Prevent Premature Audio Engine Start: Next, make sure the SDK’s internal audio engine doesn’t start automatically by using:
try AudioManager.shared.setEngineAvailability(.none)
3. Configure AVAudioSession in CallKit’s
didActivate
Callback:
Then, inside the
provider(_:didActivate:)
method, set the audio session category and mode specifically for voice calls: `func provider(_ provider: CXProvider, didActivate session: AVAudioSession) { do { try session.setCategory(.playAndRecord, mode: .voiceChat, options: [.mixWithOthers]) try AudioManager.shared.setEngineAvailability(.default) } catch { print("Error configuring audio session: \(error)") } }`This
.voiceChat
mode is key, as it routes audio to the built-in receiver by default. 4. Revert Engine Availability When Call Ends: Finally, in the
provider(_:didDeactivate:)
method, disable the engine to gracefully release audio resources:
Copy code
func provider(_ provider: CXProvider, didDeactivate session: AVAudioSession) {
    do {
        try AudioManager.shared.setEngineAvailability(.none)
    } catch {
        print("Error deactivating audio engine: \(error)")
    }
}
If you'd like to see a complete implementation example, I highly recommend checking out the CallKit + PushKit example in the LiveKit Swift SDK example collection. It’s a great resource to help you get everything set up smoothly. Sources: README.md