Swift callkit有时可以';t接到来电后启动扬声器(仅来电)

Swift callkit有时可以';t接到来电后启动扬声器(仅来电),swift,callkit,speaker,Swift,Callkit,Speaker,跟随@Marco comment后,我更新了如下代码,但仍然无法工作,扬声器有时无法启用 在报告新呼叫/用户接受呼叫之前,我调用了以下两种方法: configureAudioSessionToDefaultSpeaker() 我更新了更多代码: func startCallWithPhoneNumber(call : CallInfoModel) { configureAudioSessionToDefaultSpeaker() current

跟随@Marco comment后,我更新了如下代码,但仍然无法工作,扬声器有时无法启用

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

configureAudioSessionToDefaultSpeaker()
我更新了更多代码:

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

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

您正在配置音频会话两次。
RTCAdioSession
AVAudioSession
的代理。您应该只进行一次配置以避免意外结果
RTCAdioSession
应该公开
AVAudioSession
的所有方法,因此您应该能够在
ConfigureRTCAdioSession()
中进行所需的所有配置,并消除
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)

你什么时候调用这两种方法?是否在
函数提供程序(\provider:CXProvider,didActivate audioSession:AVAudioSession)
上?上述函数用于实现,但我对此不做任何操作,我对callkit+webrtc的了解不够,对此我深表歉意,我更新了更多有关调用配置audiosessionhi的代码,第一次im只在diDAcceptCall方法中设置了一次configureAudioSession,但我使40%的调用没有声音
 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)
        
    }
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)