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.
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