Reputation: 659
I started intent and wait for the result. It works pretty well on short speech but it does not give me the answer of the speech if it is too long. (nearly 1 min)
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);
Is there a way other than SpeechRecognizer to get results from ACTION_RECOGNIZE_SPEECH intent?
Upvotes: 5
Views: 6663
Reputation: 17
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;
}
}
}
}
Upvotes: 0
Reputation: 11921
Here's a working solution:
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);
But before use this feature you should check if user granted the RECORD_AUDIO
permission and device has ACTION_RECOGNIZE_SPEECH
available.
Recognise Speech has an interesting behaviour with long speech. If you give a small number to MAX_RESULTS
recognise speech screen freezes after a long speech. So you need to keep number bigger and you get a result in onActivityResult
with List<String> results
from recognise speech intent. You can get results with a loop and than use.
Upvotes: 4
Reputation: 132
Try this google text to speech intent launcher,
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);
Hope this helps :)
Upvotes: 1