【发布时间】:2017-05-18 16:20:22
【问题描述】:
我有一个带有 longPressGestureRecognizer 的 UIButton,如果用户点击并按住按钮,语音识别开始,松开按钮,语音文本出现在 textField 它工作正常,但 Apple 的语音框架需要有效的互联网连接。出于这个原因,我希望用户通过警报获得适当的消息。 就是这样,我尽量自己处理:
enum InternetConnectionError: Error {
case noInternet
case lowInternetSpeed
}
// start the recognition
@IBAction func longPressAddArticles(_ sender: UILongPressGestureRecognizer) {
if sender.state == .began {
do {
try startSession()
} catch InternetConnectionError.noInternet {
displayInformationAlert(message: "Sorry, please check your internet connection!")
} catch InternetConnectionError.lowInternetSpeed {
displayInformationAlert(message: "Sorry your internet is to slow!")
// catch All other errors
} catch let error as Error {
displayInformationAlert(message: "Unknown error!")
}
} else if sender.state == .ended {
if audioEngine.isRunning {
audioEngine.stop()
speechRecognitionRequest?.endAudio()
}
}
}
func displayInformationAlert(message: String) {
let alert = UIAlertController(title: "Attention", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel))
present(alert, animated: true)
}
private func startSession() throws {
// check if a previous recognition task is running, and if so cancel it
if let recognitionTask = speechRecognitionTask {
recognitionTask.cancel()
self.speechRecognitionTask = nil
}
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(AVAudioSessionCategoryRecord)
speechRecognitionRequest = SFSpeechAudioBufferRecognitionRequest()
guard let recognitionRequest = speechRecognitionRequest else {
throw InternetConnectionError.noInternet
}
guard let inputNode = audioEngine.inputNode else {
throw InternetConnectionError.lowInternetSpeed
}
let recordingFormat = inputNode.outputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
self.speechRecognitionRequest?.append(buffer)
}
audioEngine.prepare()
do {
try audioEngine.start()
} catch {
displayInformationAlert(message: "The recognition could'n start!")
}
speechRecognitionRequest?.shouldReportPartialResults = false
speechRecognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest) { result, error in
var finished = false
if let result = result {
let articleName = result.bestTranscription.formattedString
if (self.checkIfShopExists(name:articleName)) {
self.delegate?.openExistShopWith(name: articleName)
} else {
self.createNewArticle(name: articleName)
}
finished = result.isFinal
}
if error != nil || finished {
self.audioEngine.stop()
inputNode.removeTap(onBus: 0)
self.speechRecognitionRequest = nil
self.speechRecognitionTask = nil
self.btnRecordButton.isEnabled = true
}
}
}
如果我将 iPhone 置于飞行模式,启动应用程序,多次按下按钮,应用程序崩溃,但没有一些有用的崩溃报告
【问题讨论】:
-
你的控制台输出是什么?
-
重点是:没什么。正如您在我的问题发布的图片中看到的那样。应用程序崩溃,AppDelegate 出现“线程 1:信号 SIGABRT”
-
那不是控制台,点击右下角的另一个面板显示控制台。
-
对不起 :) 错误:[0x1a86a1b40] >avae> AVAudioNode.mm:565:CreateRecordingTap:所需条件为假:_recordingTap == nil 由于未捕获的异常而终止应用程序'com.apple.coreaudio.avfaudio ”,原因: '所需的条件为假:_recordingTap ==零' ***第一掷调用堆栈:(0x18255efd8 0x180fc0538 0x18255eeac 0x19c5761cc 0x19c5ea57c 0x19c5e817c 0x1000fc630 0x1000f6688 0x1000f6c28 0x188c5fc54 0x188c63488 0x18881d540 0x1886bf45c 0x188c5372c 0x18250c9a0的libc ++ abi.dylib:与未捕获的异常终止NSException (lldb) 类型的
-
请编辑您的问题并显示错误的相关代码
标签: ios error-handling swift3