iosswiftavaudiosessionlinphonecallkit

Audio not working when incoming call is answered during screen is locked - swift


I am buidling an app using VOIP service. Now i integrated my app with CallKit in order to handle incoming call request.

When the app stays in foreground or background they are working fine when answered call. But the problem is that when the screen is locked and i tried to answer the call but unfortunately I can't hear audio for both side even i unlocked the screen.

How to solve this issue?

This is how incoming call reports:

func reportIncomingCall(uuid: UUID, handle: String, hasVideo: Bool = false, completion: ((NSError?) -> Void)?) {
    // 1.
    print("This is UUID === ", uuid)
    configureAudioSession()
    let update = CXCallUpdate()
    update.remoteHandle = CXHandle(type: .phoneNumber, value: handle)
    update.hasVideo = hasVideo


    provider.reportNewIncomingCall(with: uuid, update: update) { error in


        if error == nil {
            // 3.
            self.configureAudioSession()
            let call = CallKitCallInit(uuid: uuid, handle: handle)
            self.callKitManager.add(call: call)
            lastCallUUID = uuid
            print("UUID === ", uuid)
        } else {

        }

        // 4.
        completion?(error as NSError?)
    }


}

This is how i set audio

func configureAudioSession() {
    let session = AVAudioSession.sharedInstance()
    do{
        try session.setCategory(AVAudioSessionCategoryPlayAndRecord)

    } catch {
        print("========== Error in setting category \(error.localizedDescription)")
    }
    do {
        try session.setMode(AVAudioSessionModeVoiceChat)
    } catch {
        print("========= Error in setting mode \(error.localizedDescription)")
    }
    do {
        try session.setPreferredSampleRate(44100.0)
    } catch {
        print("======== Error setting rate \(error.localizedDescription)")
    }
    do {
        try session.setPreferredIOBufferDuration(0.005)
    } catch {
        print("======== Error IOBufferDuration \(error.localizedDescription)")
    }
    do {
        try session.setActive(true)
    } catch {
        print("========== Error starting session \(error.localizedDescription)")
    }
}

When i answered the call when screen is locked i could see that the error that it throwed in configureAudioSession() function.

Why it not able to set audio when the screen is locked?


Solution

  • I did by adding these lines of code

    func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
        // 1.
        guard let call = callKitManager.callWithUUID(uuid: action.callUUID) else {
            action.fail()
            return
        }
        // 2.
        configureAudioSession()
        // 3.
        call.answer()
        // 4.
        action.fulfill()
    }
    

    This is configureAudioSession

    func configureAudioSession() {
        let session = AVAudioSession.sharedInstance()
    
        do{
            try session.setCategory(AVAudioSession.Category.playAndRecord,
                                    mode: AVAudioSession.Mode.voiceChat,
                                     options: [])
    
        } catch {
            print("========== Error in setting category \(error.localizedDescription)")
        }
    
        do {
            try session.setPreferredSampleRate(44100.0)
        } catch {
            print("======== Error setting rate \(error.localizedDescription)")
        }
        do {
            try session.setPreferredIOBufferDuration(0.005)
        } catch {
            print("======== Error IOBufferDuration \(error.localizedDescription)")
        }
        do {
            try session.setActive(true)
        } catch {
            print("========== Error starting session \(error.localizedDescription)")
        }
    }