0

我正在开发 WebRTC 应用程序,通话时一切正常,但我们想在通话响铃时运行拨号声音,直到对方接听电话

我尝试运行音频(wav 文件),但它播放了一秒钟,然后我觉得它被静音了

我想这是因为覆盖了 AVAudioSession

这是我在通话开始后播放拨号声音的代码

func startDialSound() {
        do {
            let session = AVAudioSession.sharedInstance()
            try session.setCategory(.playAndRecord, options: [.duckOthers])
            try session.setActive(true, options: .notifyOthersOnDeactivation)
            
            let path = Bundle.main.path(forResource: "dial.wav", ofType: nil)!
            let url = URL(fileURLWithPath: path)

            self.dialPlayer = try AVAudioPlayer(contentsOf: url)
            self.dialPlayer?.prepareToPlay()
            self.dialPlayer?.numberOfLoops = -1
            self.dialPlayer?.volume = 1
            dialPlayer?.play()
        } catch {}
    }

所以请你帮我解决这个问题吗?

等待回复

谢谢

4

1 回答 1

0
  1. 配置RTCAudioSession为使用手动音频:RTCAudioSession.sharedInstance().useManualAudio = true
  2. 如果您使用 CallKit 配置AudioSession来调用func provider(_ provider: CXProvider, perform action: CXStartCallAction)
static func configureAudioSessionForCall() {
    do {
        let session = AVAudioSession.sharedInstance()
        try session.setCategory(.playAndRecord, mode: .voiceChat)
        try session.setPreferredIOBufferDuration(Constants.preferredIOBufferDuration)
        try session.setPreferredSampleRate(Constants.preferredSampleRate)
    } catch {
        //Handle error
    }
}
  1. 如果您使用 CallKit,请通知 RTCAudioSession 会话激活并在func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession)调用时启用 WebRTC 音频:
func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
    RTCAudioSession.sharedInstance().audioSessionDidActivate(audioSession)
    if RTCAudioSession.sharedInstance().useManualAudio {
        RTCAudioSession.sharedInstance().isAudioEnabled = true
    }
}
  1. 当您通过信号接口收到某种“呼叫”事件时 - 开始播放您的音频文件:
func startPlayRingBackTone() {
    guard let fileURL = Bundle.main.url(forResource: "dial", withExtension: "wav") else { return }
    stopPlayRingBackTone()
    do {
        ringBackTonePlayer = try AVAudioPlayer(contentsOf: fileURL)
        ringBackTonePlayer?.numberOfLoops = -1
        ringBackTonePlayer?.prepareToPlay()
        ringBackTonePlayer?.play()
    } catch  {
        //Handle error
    }
}
    
func stopPlayRingBackTone() {
    guard let player = ringBackTonePlayer else { return }
    player.stop()
    ringBackTonePlayer = nil
}
于 2021-09-01T15:18:33.627 回答