我正在使用Android,SpeechRecognizer
并希望限制Google返回的转录结果数量.
从文档,EXTRA_MAX_RESULTS在RecognizerIntent
貌似我想要的东西:
要返回的最大结果数量的可选限制.如果省略,识别器将选择返回多少结果.必须是整数.
但是,将此额外添加到意图中无效.该Bundle
中onResults
总是有五个改编,无论我指定的号码.
public class MainActivity extends AppCompatActivity implements RecognitionListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext()); recognizer.setRecognitionListener(this); final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 2); //should limit to 2 results FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { recognizer.startListening(intent); } }); } @Override public void onResults(Bundle results) { ArrayListmatches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if (matches != null && !matches.isEmpty()) { Log.d("All matches", "number matches: " + matches.size()); //always 5 } } ... }
限制Google返回的转录数量的正确方法是什么?