gpt4 book ai didi

ios - 处理没有互联网连接的语音错误

转载 作者:行者123 更新时间:2023-12-03 08:49:44 25 4
gpt4 key购买 nike

我有一个带有longPressGestureRecognizer的UIButton,如果用户点击并按住Button,则语音识别开始,释放该按钮,语音文本出现在textField中
它工作正常,但Apple的Speech Framework需要有效的Internet连接。因此,我希望用户通过警报获得适当的消息。
我就是这样尝试自己的:

 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设置为飞行模式,请启动该应用程序,然后多次按该按钮,该应用程序将崩溃,而没有一些有用的崩溃报告

enter image description here
有什么想法吗?

最佳答案

我使用此功能将互联网作为您功能的第一行。然后,只要有保护声明,只要有互联网,就可以使用保护声明。

func isInternetAvailable() -> Bool
{
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)

let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}

var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
return false
}
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection)
}

关于ios - 处理没有互联网连接的语音错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44053088/

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