1

在关注@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)
        
    }

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

4

2 回答 2

1

您正在配置 AudioSession 两次。它RTCAudioSessionAVAudioSession. 您应该只进行一种配置以避免意外结果。RTCAudioSession应该暴露所有的方法AVAudioSession,所以你应该能够在里面进行所有你想要的配置configureRtcAudioSession()并消除configureAudioSessionToDefaultSpeaker(),反之亦然。我不确定它是否会解决您的问题,但至少它应该有助于避免意外行为。

于 2021-02-10T09:23:05.447 回答
0

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

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)
于 2021-02-22T22:10:29.173 回答