屏幕锁定期间接听来电时音频不起作用-迅速

时间:2018-12-30 09:55:23

标签: ios swift avaudiosession linphone callkit

我正在使用VOIP服务来构建应用程序。现在,我将我的应用程序与CallKit集成在一起,以便处理来电请求。

当应用程序处于前台或后台时,在接听电话时它们可以正常工作。但是问题在于,当屏幕锁定并且我试图接听电话时,不幸的是,即使我解锁了屏幕,我也听不到双方的声音。

如何解决此问题?

这是来电报告的方式:

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?)
    }


}

这就是我设置音频的方式

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)")
    }
}

当屏幕锁定时我接听电话时,我可以看到它在configureAudioSession()函数中引发的错误。

为什么在屏幕锁定时无法设置音频?

2 个答案:

答案 0 :(得分:0)

我通过添加以下代码行完成了

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()
}

这是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)")
    }
}

答案 1 :(得分:-1)

在配置音频会话时,您可以尝试使用AVAudioSessionModeVoiceChat而不是AVAudioSessionModeMoviePlayback模式。