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... hoping someone else has come across the same problem, solving it.
import UIKit
import AVFoundation
class RecordSoundsViewController: UIViewController, AVAudioRecorderDelegate {
// MARK: IBOutlets for Buttons
@IBOutlet weak var recordingLable: UILabel!
@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var stopRecordingButton: UIButton!
var audioRecorder : AVAudioRecorder!
override func viewDidLoad() {
super.viewDidLoad()
stopRecordingButton.isEnabled = false
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
@IBAction func recordAudio(_ sender: Any) {
configureUI(true) // Call to set Buttons enabled state correct
// MARK: AVAudio code and config....
let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0] as String
let recordingName = "recordedVoice.wav"
let pathArray = [dirPath, recordingName]
let filePath = URL(string: pathArray.joined(separator: "/"))
let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.defaultToSpeaker)
try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record() //ca_debug_string Error occurs when this call is made.
}
// MARK: IBAction Stop Recording
@IBAction func stopRecording(_ sender: Any) {
configureUI(false) // Call to set Buttons enabled state correct
audioRecorder.stop()
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setActive(false)
}
// MARK: Func audioRecorderDidFinishRecording
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
if flag {
performSegue(withIdentifier: "stopRecording", sender: audioRecorder.url)
} else {
print("recording was not successful")
}
}
// MARK: ConfigureUI Method for State of Buttons
func configureUI(_ recordingState: Bool) {
if recordingState {
recordingLable.text = "Recording in progress"
stopRecordingButton.isEnabled = true
recordButton.isEnabled = false
} else {
recordingLable.text = "Tap to record"
stopRecordingButton.isEnabled = false
recordButton.isEnabled = true
}
}
// MARK: Prepare Viewcontroller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "stopRecording" {
let playSoundsVC = segue.destination as! PlaySoundsViewController //forced upcase
let recordedAudioURL = sender as! URL
playSoundsVC.recordedAudioURL = recordedAudioURL
}
}
}
The line where the error occurs:
audioRecorder.record()
The program doesn't abort or crash (in the simulator). The recording works fine, just this message pops up in the debugger.
If anybody has an idea what could be causing the problem it would be greatly appreciated if you would share it here. Thanks a lot!
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With