我开始意图并等待结果。它在短语中很有效,但如果时间太长,它就不能给出答案。 (将近1分钟)
final Intent searchIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
searchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "tr");
searchIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, true);
searchIntent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, true);
startActivityForResult(searchIntent, VOICE_REQUEST_CODE);
除了SpeechRecognizer之外,还有其他方法可以从ACTION_RECOGNIZE_SPEECH意图中获取结果吗?
答案 0 :(得分:2)
这是一个有效的解决方案:
final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, yourPackageHere);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1000);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Your Prompt");
startActivityForResult(intent,REQUEST_CODE);
但在使用此功能之前,您应该检查用户是否已授予RECORD_AUDIO
权限,且设备是否有ACTION_RECOGNIZE_SPEECH
。
识别语音有长篇大论的有趣行为。如果你给一小部分MAX_RESULTS
识别语音屏幕在长时间讲话后冻结。因此,您需要保持数字更大,并且onActivityResult
List<String> results
的结果会识别语音意图。您可以通过循环获得结果而不是使用。
答案 1 :(得分:1)
试试这个谷歌文本到语音意图启动器,
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Now");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, REQUEST_CODE);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
startActivityForResult(intent,REQUEST_CODE);
希望这会有所帮助:)
答案 2 :(得分:0)
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_SPEECH_INPUT =1000 ;
//views from activity
TextView mTextTv;
ImageButton mVoiceBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
mTextTv= findViewById( R.id.textTv );
mVoiceBtn=findViewById( R.id.voiceBtn );
//button clic to show speech to text dilog
mVoiceBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
speak();
}
} );
}
private void speak() {
//intent is show speech to text dialog
Intent intent= new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH );
intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault() );
intent.putExtra( RecognizerIntent.EXTRA_PROMPT, "HI speak something" );
// Start intent
try {
//in there was no errror
//show dilog
startActivityForResult( intent, REQUEST_CODE_SPEECH_INPUT);
}
catch(Exception e){
//show messageof error and show
Toast.makeText( this,""+e.getMessage(), Toast.LENGTH_SHORT ).show();
}
}
//recive voice input and handle it
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult( requestCode, resultCode, data );
switch (requestCode){
case REQUEST_CODE_SPEECH_INPUT:{
if(resultCode==RESULT_OK && null!=data){
//get text arry form voice intent
ArrayList<String> result=data.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS );
//set to text view
mTextTv.setText( result.get( 0 ) );
}
break;
}
}
}
}