我试图记录用户的声音,并将其更改为文本数据。我使用AVAudioRecorder录制声音和SpeechKit,将其更改为包含在iOS 10中的文本。当用户触摸按钮记录时,按钮被触摸时开始并停止。但是,当我使用do catch语法初始化AVAudioRecorder时,发生错误并失败。 我添加了相应的框架(Speech,AVFoundation)。Xcode 8.0 beta3 AVAudioRecorder(网址,设置)错误捕获 - “错误域= NSOSStatusErrorDomain代码= -50”(空)“”
import UIKit
import Speech
import AVFoundation
class SearchViewController: UIViewController, CLLocationManagerDelegate, AVAudioRecorderDelegate {
var audioRecorder = AVAudioRecorder()
let recordSettings = [AVSampleRateKey : String(NSNumber(value: Float(44100.0))),
AVFormatIDKey : String(kAudioFileCAFType),
AVNumberOfChannelsKey : String(NSNumber(value: 2))]
@IBAction func recordButtonDown(_ sender: AnyObject) {
print("recordButtonDown")
self.audioPlayer.play()
sleep(1)
let fileManager = FileManager.default
let paths = fileManager.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)
var audioURL = paths[0] as NSURL
audioURL = audioURL.appendingPathComponent("soundForMapSearch.caf", isDirectory: false)!
do {
self.audioRecorder = try AVAudioRecorder(url: soundFileURL as URL, settings: self.recordSettings)
self.audioRecorder.delegate = self
self.audioRecorder.prepareToRecord()
self.audioRecorder.record()
} catch (let error) {
print("Error: \(error)")
}
}
@IBAction func recordButtonUp(_ sender: AnyObject) {
self.audioRecorder.stop()
}
override func viewDidLoad() {
super.viewDidLoad()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioSession.setActive(true)
audioSession.requestRecordPermission({ (recordPermission) in
})
} catch {
print("record initiallizing failed")
}
就行
self.audioRecorder = try AVAudioRecorder(url: soundFileURL as URL, settings: self.recordSettings)
错误发生在捕获
Error: Error Domain=NSOSStatusErrorDomain Code=-50 "(null)"
被打印。我搜索了这个错误,代码= -50意味着NSURL对象是无效的。我怎样才能解决这个错误?
你不是应该使用'audioURL',而不是这个'soundFileURL'我们一无所知? – Moritz