swift - 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()
该程序不会中止或崩溃(在模拟器中)。录制工作正常,只是在调试器中弹出这条消息。
如果有人知道可能导致问题的原因,如果您能在此处分享,将不胜感激。非常感谢!
解决方案
您session.setCategory
已过时,必须重命名:
不正确:
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.defaultToSpeaker)
正确的:
try! session.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
推荐阅读
- reactjs - React 路由器是正确的,但不能按预期工作
- cygwin - 兼容性层与虚拟化层
- r - 数据表 lapply 和输出中的附加列
- python - 来自谷歌的 400 错误请求
- java - Vert.x:一个 ChainAuthHandler 中的多个 JWTAuthHandler
- jhipster - jHipster 显示“发生错误 :-(” 在 Internet Explorer 中打开应用程序时,但在 chrome 浏览器中工作正常
- java - 如何指定该用户是否已共享我的应用 x 次?
- lua - Kong v1.0.2 的自定义插件已启用但未安装
- python - 如何摆脱python中的嵌套循环
- c# - 使用双泛型的 C# 类:当它们都应该相同时只给出一个?