无法获取 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/