cspam
cspam

Reputation: 2941

iOS - AudioKit Crashes when receiving a phone call

AudioKit 4.9.3 iOS 11+

I am working on a project where the user is recording on the device using the microphone and it continues to record, even if the app is in the background. This works fine but when receiving a phone call I get an AudioKit error. I assume it has something to do with the phone taking over the mic or something. here is the error:

[avae] AVAEInternal.h:109
[AVAudioEngineGraph.mm:1544:Start: (err = PerformCommand(*ioNode, kAUStartIO, NULL, 0)): error 561017449 AudioKit+StartStop.swift:restartEngineAfterRouteChange(_:):198:error restarting engine after route change

basically everything that i have recording up until that point is lost.

here is my set up AudioKit code:

func configureAudioKit() {
        AKSettings.audioInputEnabled = true
        AKSettings.defaultToSpeaker = true
        do {
            try try audioSession.setCategory((AVAudioSession.Category.playAndRecord), options: AVAudioSession.CategoryOptions.mixWithOthers)
            try audioSession.setActive(true)
            audioSession.requestRecordPermission({ allowed in
                DispatchQueue.main.async {
                    if allowed {
                        print("Audio recording session allowed")
                        self.configureAudioKitSession()
                    } else {
                        print("Audio recoding session not allowed")
                    }
                }
            })
        } catch let error{
            print("Audio recoding session not allowed: \(error.localizedDescription)")
        }
    }


func configureAudioKitSession() {
        isMicPresent = AVAudioSession.sharedInstance().isInputAvailable
        if !isMicPresent {
            return
        }
        print("mic present and configuring audio session")
        mic = AKMicrophone()
        do{
        let _ = try AKNodeRecorder(node: mic)
        let recorderGain = AKBooster(mic, gain: 0)
        AudioKit.output = recorderGain
        //try AudioKit.start()
        }
        catch let error{
            print("configure audioKit error: ", error)
        }
}

and when tapping on the record button code:

    do {
         audioRecorder = try AVAudioRecorder(url: actualRecordingPath, settings: audioSettings)
         audioRecorder?.record()
         //print("Recording: \(isRecording)")
         do{
           try AudioKit.start()
          }
          catch let error{
            print("Cannot start AudioKit", error.localizedDescription)
          }
}

Current audio Settings:

private let audioSettings = [
        AVFormatIDKey : Int(kAudioFormatMPEG4AAC),
        AVSampleRateKey : 44100,
        AVNumberOfChannelsKey : 2,
        AVEncoderAudioQualityKey : AVAudioQuality.medium.rawValue
    ]

What can I do to ensure that I can get a proper recording, even when receiving a phone call? The error happens as soon as you receive the call - whether you choose to answer it or decline.

Any thoughts?

Upvotes: 4

Views: 618

Answers (2)

Reuben Scratton
Reuben Scratton

Reputation: 38727

I've done work in this area, I'm afraid you cannot access the microphone(s) while a call or a VOIP call is in progress.

This is a basic privacy measure that is enforced by iOS for self-evident reasons.

Upvotes: 1

Aurelius Prochazka
Aurelius Prochazka

Reputation: 4573

AudioKit handles only the basic route change handling for an audio playback app. We've found that when an app becomes sufficiently complex, the framework can't effectively predestine the appropriate course of action when interruptions occur. So, I would suggest turning off AudioKit's route change handling and respond to the notifications yourself.

Also, I would putting AudioKit activation code in a button.

Upvotes: 1

Related Questions