gpt4 book ai didi

Android识别监听器: onResults being called twice

转载 作者:行者123 更新时间:2023-12-02 11:45:25 27 4
gpt4 key购买 nike

我有一个使用 Kotlin 编写的 RecognitionListener 的项目。语音转文本功能一直很成功,从未出现任何问题。

从上周开始,它的 onResult 函数开始被调用两次。该项目没有进行任何更改。我测试了该项目的旧版本(从几个月前开始)并且那些有同样的问题。

有三种不同的情况:

  • 小文本(1 到 8 个单词)和 SpeechRecognizer 自动停止 -> onResult() 调用两次;
  • 大文本(9 个字或更多)和 SpeechRecognizer 自动停止 -> 正常行为(onResult() 调用一次);
  • 手动调用的任何文本大小和 SpeechRecognizer stopListening() 函数(来自代码)-> 正常行为。

  • 这是 VoiceRecognition 语音转文本类代码:
    class VoiceRecognition(private val activity: Activity, language: String = "pt_BR") : RecognitionListener {

    private val AudioLogTag = "AudioInput"

    var voiceRecognitionIntentHandler: VoiceRecognitionIntentHandler? = null
    var voiceRecognitionOnResultListener: VoiceRecognitionOnResultListener? = null //Must have this
    var voiceRecognitionLayoutChanger: VoiceRecognitionLayoutChanger? = null

    var isListening = false

    private val intent: Intent
    private var speech: SpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(activity)

    init {
    speech.setRecognitionListener(this)

    intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    intent.putExtra(
    RecognizerIntent.EXTRA_LANGUAGE_MODEL,
    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
    )
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language)
    }

    //It is important to put this function inside a clickListener
    fun listen(): Boolean {
    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.RECORD_AUDIO), 1)
    return false
    }

    speech.startListening(intent)

    Log.i(AudioLogTag, "startListening")

    return true
    }

    //Use this if you want to stop listening but still get recognition results
    fun endListening(){
    Log.i(AudioLogTag, "stopListening")

    speech.stopListening()
    isListening = false
    }

    fun cancelListening(){
    Log.i(AudioLogTag, "cancelListening")

    speech.cancel()
    voiceRecognitionLayoutChanger?.endListeningChangeLayout()
    isListening = false
    }

    override fun onReadyForSpeech(p0: Bundle?) {
    Log.i(AudioLogTag, "onReadyForSpeech")

    voiceRecognitionLayoutChanger?.startListeningChangeLayout()
    isListening = true
    }

    override fun onRmsChanged(p0: Float) {
    // Log.i(AudioLogTag, "onRmsChanged: $p0")
    // progressBar.setProgress((Int) p0)
    }

    override fun onBufferReceived(p0: ByteArray?) {
    Log.i(AudioLogTag, "onBufferReceived: $p0")
    }

    override fun onPartialResults(p0: Bundle?) {
    Log.i(AudioLogTag, "onPartialResults")
    }

    override fun onEvent(p0: Int, p1: Bundle?) {
    Log.i(AudioLogTag, "onEvent")
    }

    override fun onBeginningOfSpeech() {
    Log.i(AudioLogTag, "onBeginningOfSpeech")
    }

    override fun onEndOfSpeech() {
    Log.i(AudioLogTag, "onEndOfSpeech")

    voiceRecognitionLayoutChanger?.endListeningChangeLayout()
    isListening = false
    }

    override fun onError(p0: Int) {
    speech.cancel()
    val errorMessage = getErrorText(p0)
    Log.d(AudioLogTag, "FAILED: $errorMessage")
    voiceRecognitionLayoutChanger?.endListeningChangeLayout()
    isListening = false
    }

    override fun onResults(p0: Bundle?) {

    val results: ArrayList<String> = p0?.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION) as ArrayList<String>

    Log.i(AudioLogTag, "onResults -> ${results.size}")

    val voiceIntent: Int? = voiceRecognitionIntentHandler?.getIntent(results[0])
    if (voiceIntent != null && voiceIntent != 0) {
    voiceRecognitionIntentHandler?.handle(voiceIntent)
    return
    }

    voiceRecognitionOnResultListener!!.onResult(results[0])
    }

    private fun getErrorText(errorCode: Int): String {
    val message: String
    when (errorCode) {
    SpeechRecognizer.ERROR_AUDIO -> message = "Audio recording error"
    SpeechRecognizer.ERROR_CLIENT -> message = "Client side error"
    SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS -> message = "Insufficient permissions"
    SpeechRecognizer.ERROR_NETWORK -> message = "Network error"
    SpeechRecognizer.ERROR_NETWORK_TIMEOUT -> message = "Network timeout"
    SpeechRecognizer.ERROR_NO_MATCH -> message = "No match"
    SpeechRecognizer.ERROR_RECOGNIZER_BUSY -> message = "RecognitionService busy"
    SpeechRecognizer.ERROR_SERVER -> message = "Error from server"
    SpeechRecognizer.ERROR_SPEECH_TIMEOUT -> message = "No speech input"
    else -> message = "Didn't understand, please try again."
    }
    return message
    }

    //Use it in your overriden onPause function.
    fun onPause() {
    voiceRecognitionLayoutChanger?.endListeningChangeLayout()
    isListening = false

    speech.cancel()
    Log.i(AudioLogTag, "pause")
    }

    //Use it in your overriden onDestroy function.
    fun onDestroy() {
    speech.destroy()
    }

    listen()、endListening() 和 cancelListening() 都是从一个按钮调用的。

    最佳答案

    我发现了这个 Unresolved 问题:https://issuetracker.google.com/issues/152628934

    正如我评论的那样,我认为这是“语音识别服务”的问题,而不是 Android RecognitionListener 类的问题。

    关于Android识别监听器: onResults being called twice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60853257/

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