首页 > 解决方案 > Swift,AVAudioRecorder:错误 317:ca_debug_string:inPropertyData == NULL

问题描述

我知道有些线程会出现此错误消息,但它们并没有真正提供问题的答案,这就是为什么我决定打开另一个线程并再次询问...希望其他人遇到同样的问题,解决它.

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

}

发生错误的行:

audioRecorder.record()

该程序不会中止或崩溃(在模拟器中)。录制工作正常,只是在调试器中弹出这条消息。

如果有人知道可能导致问题的原因,如果您能在此处分享,将不胜感激。非常感谢!

标签: swiftxcodeavfoundation

解决方案


session.setCategory已过时,必须重命名:

不正确:

try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.defaultToSpeaker)

正确的:

try! session.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.defaultToSpeaker)

推荐阅读