swift - stopContinouslyRecognition() 阻止应用程序 5-7 秒

标签 swift azure speech-recognition speech-to-text

我正在尝试使用 Swift 在 iOS 项目中使用 Azure 语音 SDK 实现语音识别,但遇到了语音识别完成功能 ( stopContinuousRecognition() ) 阻塞应用程序 UI 几秒钟的问题,但没有内存或处理器负载或泄漏。我尝试将此功能移至 DispatchQueue.main.async {} ,但没有给出任何结果。也许有人遇到过这样的问题?是否有必要将其放在单独的线程中?为什么该函数需要这么长时间才能完成?

编辑: 很难提供工作示例,但基本上我是在按下按钮时调用此函数:

 private func startListenAzureRecognition(lang:String) {
    let audioFormat = SPXAudioStreamFormat.init(usingPCMWithSampleRate: 8000, bitsPerSample: 16, channels: 1)
    azurePushAudioStream = SPXPushAudioInputStream(audioFormat: audioFormat!)
    let audioConfig = SPXAudioConfiguration(streamInput: azurePushAudioStream!)!
    var speechConfig: SPXSpeechConfiguration?

    do {
      let sub = "enter your code here"
      let region = "enter you region here"
      try speechConfig = SPXSpeechConfiguration(subscription: sub, region: region)
      speechConfig!.enableDictation();
      speechConfig?.speechRecognitionLanguage = lang
    } catch {
      print("error \(error) happened")
      speechConfig = nil
    }

    self.azureRecognition = try! SPXSpeechRecognizer(speechConfiguration: speechConfig!, audioConfiguration: audioConfig)
    
    self.azureRecognition!.addRecognizingEventHandler() {reco, evt in
      if (evt.result.text != nil && evt.result.text != "") {
         print(evt.result.text ?? "no result")
      }
    }
    self.azureRecognition!.addRecognizedEventHandler() {reco, evt in
      if (evt.result.text != nil && evt.result.text != "") {
        print(evt.result.text ?? "no result")
      }
    }
    do {
     try! self.azureRecognition?.startContinuousRecognition()
    } catch {
      print("error \(error) happened")
    }
  }

当我再次按下按钮停止识别时,我正在调用此函数:

private func stopListenAzureRecognition(){
   DispatchQueue.main.async {
      print("start")
      // app blocks here
      try! self.azureRecognition?.stopContinuousRecognition()
      self.azurePushAudioStream!.close()
      self.azureRecognition = nil
      self.azurePushAudioStream = nil
      print("stop")
    }
  }

我还使用来自麦克风的原始音频数据( recognizeOnce 非常适合第一个短语,因此音频数据一切正常)

最佳答案

尝试先关闭流,然后停止连续识别:

azurePushAudioStream!.close()
try! azureRecognition?.stopContinuousRecognition()
azureRecognition = nil
azurePushAudioStream = nil

您甚至不需要异步执行。

至少这对我有用。

关于swift - stopContinouslyRecognition() 阻止应用程序 5-7 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70153518/

相关文章:

Azure 应用服务环境 - New-AzureRmWebApp 损坏?

azure - <租户名称>.b2clogin.com 重定向到 login.microsoftonline.com

.net - "Value does not fall within expected range"在运行时错误中是什么意思?

ios - 使用张开的耳朵进行语音识别时准确度非常低

swift - 关于 Swift 函数、命名参数和类型管理

macos - 在 Mac 上,将文件拖到我的 NSTableVIew?

asp.net-mvc - 隐藏 sessionState 提供程序值的最佳方法

nlp - 同步文本和音频。是否有 NLP/语音到文本库来执行此操作?

ios - UIButton,自动布局和获取按钮的高度

ios - Swift 扩展 UIView 返回调用类型