WebRTC Call want to run dial sound

568 Views Asked by At

I am working on WebRTC application and everything is doing fine on call but we want to run dial sound when call is ringing until other side accpet the call

i try to run audio (wav file) but it is play for a sec and then i feel it is muted

i guess it is because overriding of AVAudioSession

here is my code to play dial sound after call is starting

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

so please can you help me to fix that?

waiting responses

thanks

1

There are 1 best solutions below

0
On
  1. Configure RTCAudioSession to use manual audio: RTCAudioSession.sharedInstance().useManualAudio = true
  2. If you use CallKit configure AudioSession for call in the 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. If you use CallKit, notify RTCAudioSession about the session activation and enable WebRTC audio when func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) called:
func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
    RTCAudioSession.sharedInstance().audioSessionDidActivate(audioSession)
    if RTCAudioSession.sharedInstance().useManualAudio {
        RTCAudioSession.sharedInstance().isAudioEnabled = true
    }
}
  1. When you receive via your signalling interface some kind of "calling" event - start to play your audio file:
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
}