I know that there are threads where this error message appears but they do not really supply an answer to the problem, this is why I decided open up another thread and ask again
I had the same issue. It works if you add another "audioRecorder.record()" line. You might want to do an audioRecorder.isRecording to check if it is recording inside an if statement. If it is recording then don't do anything, if it isn't then re-run that line.
Your session.setCategory
is outdated and has to be renamed:
Incorrect:
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.defaultToSpeaker)
Correct:
try! session.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.defaultToSpeaker)