Search code examples
iosswiftopentokcallkit

Issue with Opentok and CallKit


I am trying to use Opentok and CallKit for audio and video calling purpose, I have followed this demo provided by OpenTok https://github.com/opentok/CallKit/tree/master/CallKitDemo

The issue I am facing is when I am doing audio call to another person it starts speaker instead of Built-in-Receiver. I tried their demo and it also has the same issue.

Then I tried https://developer.apple.com/library/content/samplecode/Speakerbox/Introduction/Intro.html#//apple_ref/doc/uid/TP40017290 which is provided by Apple, but after implementing the AudioController, the new call starts with Built-in-Speaker and it automatically switched to Speaker and there is no way to make output change.


Solution

  • I need to implement a custom audio driver with setting up the AVAudioSession

             var audioOptions: UInt {
                if isSpeaker {
                    return AVAudioSessionCategoryOptions.defaultToSpeaker.rawValue |
                        AVAudioSessionCategoryOptions.mixWithOthers.rawValue |
                        AVAudioSessionCategoryOptions.allowBluetooth.rawValue |
                        AVAudioSessionCategoryOptions.allowAirPlay.rawValue
    
                } else {
                    return AVAudioSessionCategoryOptions.mixWithOthers.rawValue |
                        AVAudioSessionCategoryOptions.allowBluetooth.rawValue |
                        AVAudioSessionCategoryOptions.allowAirPlay.rawValue |
                        AVAudioSessionCategoryOptions.duckOthers.rawValue
                }
            }
            if isSpeaker {
                try session.setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeVoiceChat, options: AVAudioSessionCategoryOptions(rawValue: audioOptions))
            } else {
                try session.setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeVoiceChat, options: AVAudioSessionCategoryOptions(rawValue: audioOptions))
            }