【发布时间】:2017-02-20 17:06:44
【问题描述】:
我使用的是 iPhone 6s plus,这里是语音识别视图控制器的代码:
import Speech
import UIKit
protocol SpeechRecognitionDelegate: class {
func speechRecognitionComplete(query: String?)
func speechRecognitionCancelled()
}
class SpeechRecognitionViewController: UIViewController, SFSpeechRecognizerDelegate {
var textView: UITextView!
private let speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))
private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
private var recognitionTask: SFSpeechRecognitionTask?
private let audioEngine = AVAudioEngine()
private var query: String?
weak var delegate: SpeechRecognitionDelegate?
var isListening: Bool = false
init(delegate: SpeechRecognitionDelegate, frame: CGRect) {
super.init(nibName: nil, bundle: nil)
self.delegate = delegate
self.view.frame = frame
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
enum ErrorMessage: String {
case denied = "To enable Speech Recognition go to Settings -> Privacy."
case notDetermined = "Authorization not determined - please try again."
case restricted = "Speech Recognition is restricted on this device."
case noResults = "No results found - please try a different search."
}
func displayErrorAlert(message: ErrorMessage) {
let alertController = UIAlertController(title: nil,
message: message.rawValue,
preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(alertAction)
OperationQueue.main.addOperation {
self.present(alertController, animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
speechRecognizer?.delegate = self
//initialize textView and add it to self.view
}
func startListening() {
guard !isListening else {return}
isListening = true
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
guard let recognitionRequest = recognitionRequest else {
print("SpeechRecognitionViewController recognitionRequest \(self.recognitionRequest)")
return
}
recognitionRequest.shouldReportPartialResults = true
recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
var isFinal = false
if result != nil {
self.query = result?.bestTranscription.formattedString
self.textView.text = self.query
isFinal = (result?.isFinal)!
}
if error != nil || isFinal {
print("recognitionTask error = \(error?.localizedDescription)")
self.stopListening()
}
})
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryRecord)
try audioSession.setMode(AVAudioSessionModeMeasurement)
try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
} catch {
print("Audio session isn't configured correctly")
}
let recordingFormat = audioEngine.inputNode?.outputFormat(forBus: 0)
audioEngine.inputNode?.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, time) in
self.recognitionRequest?.append(buffer)
}
audioEngine.prepare()
do {
try audioEngine.start()
textView.text = "Listening..."
} catch {
print("Audio engine failed to start")
}
}
func stopListening() {
guard isListening else {return}
audioEngine.stop()
audioEngine.inputNode?.removeTap(onBus: 0)
recognitionRequest = nil
recognitionTask = nil
isListening = false
}
// MARK: SFSpeechRecognizerDelegate
func speechRecognizer(_ speechRecognizer: SFSpeechRecognizer, availabilityDidChange available: Bool) {
if !available {
let alertController = UIAlertController(title: nil,
message: "Speech Recognition is currently unavailable.",
preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default) { (alertAction) in
.self.stopListening()
}
alertController.addAction(alertAction)
present(alertController, animated: true)
}
}
}
此 VC 嵌入在另一个视图控制器中。
当在父视图控制器中点击一个按钮时,startListening() 被调用。当再次点击同一个按钮时,stopListening() 会被调用。
第一次语音识别效果很好。在第二次尝试时,我得到了这个错误(我猜它与语法加载有关?):
recognitionTask error = Optional("The operation couldn’t be completed. (kAFAssistantErrorDomain error 209.)")
并且语音识别不再起作用。 30 秒后出现超时错误:
Optional(Error Domain=kAFAssistantErrorDomain Code=203 "Timeout" UserInfo={NSLocalizedDescription=Timeout, NSUnderlyingError=0x170446f90 {Error Domain=SiriSpeechErrorDomain Code=100 "(null)"}})
原码在这里SayWhat
我错过了什么?
【问题讨论】:
-
您不应该只在实际启动监听的
do块内将isListening属性设置为true吗?我不知道这是否是问题的根源,但我看到您的很多方法都是根据该属性的值来保护的。 -
不,
guard与该错误无关。错误总是在这一行产生recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler
标签: ios swift speech-recognition ios10