Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift, AVAudioRecorder: Error 317: ca_debug_string: inPropertyData == NULL

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!

like image 460
Dev.User993 Avatar asked Sep 05 '18 10:09

Dev.User993


2 Answers

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)
like image 86
Giordany Orellana Avatar answered Oct 17 '22 10:10

Giordany Orellana


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.

like image 37
Jordan Avatar answered Oct 17 '22 10:10

Jordan