Swift callkit 有时在接到电话后无法激活扬声器仅限来电

问题描述

在关注@marco 评论后,我更新了如下代码,但仍然无法正常工作,有时无法启用扬声器

在报告新呼叫/用户接受呼叫之前,我调用了以下两种方法

configureAudioSessionToDefaultSpeaker()
func configureAudioSessionToDefaultSpeaker() {
    let session = AVAudioSession.sharedInstance()
        do {
            try session.setCategory(AVAudioSession.Category.playAndRecord,mode: .default)
            try session.setActive(true)
            try session.setMode(AVAudioSession.Mode.voiceChat)
            try session.setPreferredSampleRate(44100.0)
            try session.setPreferredioBufferDuration(0.005)
        } catch {
            print("Failed to configure `AVAudioSession`: \(error)")
        }
}

我更新了更多代码

func startCallWithPhoneNumber(call : CallInfoModel) {
        
        configureAudioSessionToDefaultSpeaker()
        currentCall = call
        if let unwrappedCurrentCall = currentCall {
            let handle = CXHandle.init(type: .generic,value: unwrappedCurrentCall.Callerdisplay ?? UNKNowN)
            let startCallAction = CXStartCallAction.init(call: unwrappedCurrentCall.uuid,handle: handle)
            let transaction = CXTransaction.init()
            transaction.addAction(startCallAction)
            requestTransaction(transaction: transaction)
            
            self.provider?.reportOutgoingCall(with: startCallAction.callUUID,startedConnectingAt: nil)
            
        }
    }
 func provider(_ provider: CXProvider,perform action: CXAnswerCallAction) {
        
            
        configureAudioSessionToDefaultSpeaker()

        delegate?.callDidAnswer()
        action.fulfill()
        currentCall?.isAccepted = true
        let sb = UIStoryboard(name: "main",bundle: nil)
        let vc = sb.instantiateViewController(withIdentifier: "SingleCallVC") as! SingleCallVC
        vc.modalPresentationStyle = .fullScreen
        vc.callObj = currentCall
        vc.isIncoming = true
        let appDelegate = AppDelegate.shared
        appDelegate.window?.rootViewController?.present(vc,animated: true,completion: nil)
        
    }

我的通话几乎正常,但有时无法启用扬声器。我阅读了很多文件,但没有任何对我有用。有人能给我一些建议吗?谢谢。

解决方法

您要配置 AudioSession 两次。 RTCAudioSession 它是 AVAudioSession 的代理。您应该只进行一种配置以避免出现意外结果。 RTCAudioSession 应该公开 AVAudioSession 的所有方法,因此您应该能够在 configureRtcAudioSession() 中进行所有您想要的配置并消除 configureAudioSessionToDefaultSpeaker(),反之亦然。我不确定它是否会解决您的问题,但至少它应该有助于避免意外行为。

,

我已成功使用以下方法启用扬声器。

let audioQueue = DispatchQueue(label: "audio")    

func setSpeaker(_ isEnabled: Bool) {
    audioQueue.async {
        defer {
            AVAudioSession.sharedInstance().unlockForConfiguration()
        }
        
        AVAudioSession.sharedInstance().lockForConfiguration()
        do {
            try AVAudioSession.sharedInstance().overrideOutputAudioPort(isEnabled ? .speaker : .none)
        } catch {
            debugPrint(error.localizedDescription)
        }
    }
}

// Enables the audio speaker.
setSpeaker(true)

// Disables the audio speaker.
setSpeaker(false)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...