我使用 SFSpeechRecognizer,基本上可以工作。
1.但有时会出现以下错误。并且主要是在我没有执行之前avStop()。
[Utility] +[AFAggregator logDictationFailedWithError:] Error Domain=kAFAssistantErrorDomain Code=203 "Retry" UserInfo={NSLocalizedDescription=Retry, NSUnderlyingError=0x1c464b880 {Error Domain=SiriSpeechErrorDomain Code=1 "(null)"}}
2.并且完全无法后台工作,会产生如下错误。
[Utility] +[AFAggregator logDictationFailedWithError:] 错误域=kAFAssistantErrorDomain Code=1700 "(null)"
class MySpeech:NSObject{
private var iosRecognizer: SFSpeechRecognizer?
private var iosRequest: SFSpeechAudioBufferRecognitionRequest?
private var iosTask: SFSpeechRecognitionTask?
private let iosAVE = AVAudioEngine()
private let avSession = AVAudioSession.sharedInstance()
func avINIT(){
try? avSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.allowBluetooth])
try? avSession.setMode(AVAudioSessionModeMeasurement)
try? avSession.setActive(true, with: .notifyOthersOnDeactivation)
}
func switchHFP(){
do{
//try avSession.setActive(false)
try avSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.allowBluetooth])
try avSession.setActive(true, with: .notifyOthersOnDeactivation)
} catch {
debugPrint("HFP error: …Run Code Online (Sandbox Code Playgroud)