java - 没有从 SpeechRecognizer 类获得结果?

标签 java android speech-recognition

我有一个 maind UI 类,其中有一个按钮实例化一个类,该类正在实现 SpeechRecognizer 库以将语音从文本转换为文本。

btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

        btnSpeak.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startNoiseProcessService();
            }
        });

startNoiseProcessService() 是一个调用另一个类对象的函数。

public void startNoiseProcessService() {

                StreamService ss = new StreamService(this);
                String s = ss.startStreaming();
                if ( s.equals("NA") ) {
                    Toast t = Toast.makeText(getApplicationContext(),threadCnt + 
                            "Opps! Your device doesn't support Speech to Text",
                            Toast.LENGTH_SHORT);
                    t.show();
            }

}

StreamService 类实现 SpeechRecognizer API

这个 StreamService 类被完全调用,问题在于,在通过 onResults(Bundle) 方法计算结果后,我无法从语音中获取转换后的文本。

我需要转换后的文本来更新我的文本框,但我没有得到它。

startStreaming() 实现:

 public String startStreaming() {

            text = "";

            if ( !SpeechRecognizer.isRecognitionAvailable(context) ) {
                text = "NA";
                return text;
            }

            Log.d(TAG, "started taking input");
            sr = SpeechRecognizer.createSpeechRecognizer(context);

            Intent intent = new Intent(
                     RecognizerIntent.ACTION_RECOGNIZE_SPEECH);


            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);

            sr.setRecognitionListener( new mylistener());
            sr.startListening(intent);

     }

最佳答案

根据您的评论,我认为您在调用 sr.startListening(intent) 时无法获得结果。您需要使用监听器的 onResults 方法获取结果,并从那里处理来自 Bundle 内部的文本。

我实现这一点的方法是让一个Service负责处理SpeechRecognizer。然后,当调用 onResults 时,我使用消息处理程序将文本发送回原始调用者。您可以在此处查看有关如何执行此操作的详细信息:https://developer.android.com/training/multiple-threads/communicate-ui.html

关于java - 没有从 SpeechRecognizer 类获得结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20517152/

相关文章:

android - 如何从此 json 对象中提取字符串数组?

speech-recognition - Nist 球体格式文件

java - 当我运行我的项目时出现此错误 : java. sql.SQLException: Locale not recognized

java - Android单元测试: Instrumentation failed due to a class error

java - Android Java 在单独的线程中运行类似于 C# 风格的 Action 列表

.net - HTML5 应用程序的服务器端语音识别

android - 在播放音乐时通过 android 中的蓝牙耳机进行语音识别

Java:带有附加面板的大弹出菜单

android - ListView 中的自定义控件(微调器)向我显示重复信息

android - 使用新数据从 simpleAdapter 更新 listview