4

I am trying to implement VoIP using webRTC and Callkit. The audio works perfectly fine during the call, but I would like to play sounds to the user while the user is initiating a call (an outgoing call).

When the user initiates a call and waits for the recipient to answer, i would like to play the waiting beep sound (long beeps). I can manage to play the sound without using Callkit but when I inform Callkit about an outgoing call it somehow cancels the audio. My assumption is it does this because it IOS silences audio when a call is beging made.

So my question is, how can i playback an mp3 file when Callkit is active. Or is this waiting sound somehow integrated in Callkit or WebRTC?

I messed around with different categories for the audiosession but no luck so far. See below a summary of my current code.

public var audioPlayer: AVAudioPlayer?

private init() {
    do {
        audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: Bundle.main.path(forResource: "dialring", ofType: "mp3")!))
        audioPlayer!.prepareToPlay()
        audioPlayer!.numberOfLoops = -1 //loop
    } catch {
        print(error.localizedDescription)
    }
}

func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
    configureAudioSession()
    audioPlayer?.play()
}

func configureAudioSession() {
    print("Configuring audio session")
    let session = AVAudioSession.sharedInstance()
    do {
        try AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: [.mixWithOthers])
        try session.setMode(AVAudioSession.Mode.voiceChat)
    } catch (let error) {
        print("Error while configuring audio session: \(error)")
    }
}

If anyone could point me in the right direction I would be grateful.

EDIT: I have background mode for audio enabled.

Marco
  • 1,572
  • 1
  • 10
  • 21
daanabc
  • 63
  • 5
  • I don't know if it's related to your issue, but you should call `action.fulfill()` inside `provider(_ provider: CXProvider, perform action: CXStartCallAction)` – Marco May 24 '19 at 11:18
  • @marco , Yes I actually already had that in my code. I did not show it here for so I could keep the post short. It has something to do with that the call silences my other sounds. I however have no idea how to work around this. – daanabc May 24 '19 at 13:15

1 Answers1

2

Threw around the structure a bit and now it works. I am not exactly sure what changed thought. If people face the same issue. - Make sure you keep a strong reference to the audioplayer. - Make sure the mode is either in .playback or .playAndRecord

daanabc
  • 63
  • 5