gpt4 book ai didi

ios - AVAudioRecorder 不工作

转载 作者:行者123 更新时间:2023-11-30 12:58:05 25 4
gpt4 key购买 nike

无法获取 AVAudioRecorder 与 URL 配合使用的代码。有人能够在 iOS 和 Swift 3 上录制音频吗?下面的粗体文本是我收到以下错误的地方:

"Cannot invoke initializer for type 'AVAudioRecorder' with an argument list of type '(url: URL, settings..."

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose of any resources that can be recreated.
}

func setupRecorder(){
let recordSettings = [AVFormatIDKey : kAudioFormatAppleLossless,
AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,
AVEncoderBitRateKey : 320000,
AVNumberOfChannelsKey : 2,
AVSampleRateKey : 44100.0 ] as [String : Any]

var error : NSError?

func getCacheDirectory() -> String {
let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory,.userDomainMask, true)

return paths [0]
}

func getFileURL() -> URL {
let path = (getCacheDirectory() as NSString).strings(byAppendingPaths: ["fileName"])
let filePath = URL(fileURLWithPath: "path")
return filePath
}

// Error on the following line
soundRecorder = try AVAudioRecorder(url: getFileURL() as URL, settings: recordSettings, error: &error)


if let err = error {
NSLog("There was an error")
}
else {
soundRecorder.delegate = self
soundRecorder.prepareToRecord()
}

最佳答案

在 Swift 中,您无需提供 error 参数,因为它会抛出

更改此:

soundRecorder = try AVAudioRecorder(url: getFileURL() as URL, settings: recordSettings, error: &error)

至:

soundRecorder = try AVAudioRecorder(url: getFileURL() as URL, settings: recordSettings)

关于ios - AVAudioRecorder 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40187486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com