我正在使用Android SpeechRecognizer
,并希望限制Google返回的转录结果数量。
从文档中,RecognizerIntent
中的EXTRA_MAX_RESULTS看起来就像我想要的那样:
要返回的最大结果数量的可选限制。如果省略,识别器将选择返回多少结果。必须是整数。
但是,将此额外添加到意图中无效。 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) {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (matches != null && !matches.isEmpty()) {
Log.d("All matches", "number matches: " + matches.size()); //always 5
}
}
...
}
限制Google返回的转录数量的正确方法是什么?