I have an Speech Recognizer Intent on click of a button.
voiceSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Now...");
startActivityForResult(intent, REQUEST_CODE);
}
catch (ActivityNotFoundException e) {
Log.v("Speech", "Could not find any Speech Recognition Actions");
}
}
});
On Activity Result I have the code as
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("Request code++++++++++++++++++++++++++++"+requestCode);
System.out.println("Result Code+++++++++++++++++++++++++++++"+resultCode);
System.out.println("Data++++++++++++++++++++++++++++++++++++"+data);
System.out.println("Language"+data.getStringExtra(RecognizerIntent.EXTRA_LANGUAGE));
System.out.println("data.getDataString()"+data.getDataString());
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
if (data != null && data.getData() != null) {
String searchKey = data.getData().toString();
System.out.println("Search Key++++++++++++++++++++++++"+searchKey);
searchEditText.setText(searchKey);
webView.loadUrl(url+"searchKey");
}
}
}
The output I get is when I am printing in onActivityResult is
Request Code I get as 1
Result Code I get as -1
Data I get as Intent { (has extras) }
Language data.getStringExtra(RecognizerIntent.EXTRA_LANGUAGE) as null.
Data.getDataString() as null.
Could anybody let me know what could be wrong I might be making in the code.
thanks.
I think you're trying to get at the results in the wrong way. They're not stored in the data string, but in a matches array.
You can get the array by doing:
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
Each entry in the array will be a multi-word string representing one guess the recogniser has for what the user said.
Related
I'm trying to include a microphone button where user can click it and it can prompt a voice input from the user. Then the voice input will be converted into String and shown in a TextView in my android app. For now, the voice input can be prompted but the converted String will not be shown in my TextView and there are no crashes or logcat. Just that it wont be shown in my TextView. All this is happening in a class which extends Fragment.
Below is the code for my microphone button :-
//Button to activate voice recognition
microphonebutton = (ImageButton) view.findViewById(R.id.button_microphone);
microphonebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
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());
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(intent, 10);
} else {
Toast.makeText(getActivity().getApplicationContext(), "Your device does not support speech input !",Toast.LENGTH_SHORT).show();
}
}
});
The method onActivityResult:-
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case 10:
if (resultCode == Activity.RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
texttotranslate.setText("");
texttotranslate.setText(result.get(0), TextView.BufferType.EDITABLE);
}
break;
}
}
I got these codes from a tutorial on youtube and it seemed to be working fine for the person who was presenting the tutorial.
You have a typo in the method onActivityResult. In the switch you are chechking the resultCode twice. First you should check the requestCode. The requestCode is the code which you defined in startActivityForResult, in your case 10.
What is the code about:
With my code I can open a mic, speak into it and see the output.
Present Situation
Right now if I speak into the mic, I see the output and jump directly in other activity.
Requirement
I want take the output in the activity but before I text to much I show you the code.
Mainactivity.java
private TextView voiceInput;
private ImageView speakButton;
private final int REQ_CODE_SPEECH_INPUT = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
voiceInput = (TextView) findViewById(R.id.voiceInput);
speakButton = (ImageView) findViewById(R.id.btnSpeak);
speakButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
askSpeechInput();
}
});
}
// Showing google speech input dialog
private void askSpeechInput() {
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, "Sprechen Sie was ein");
//tent.putStringArrayListExtra("result",resultat);
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
Intent intent=new Intent(this,Zweites.class);
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
voiceInput.setText(result.get(0));
intent.putStringArrayListExtra("resultat",result);
//ActivityZweites wird gestartet
startActivity(intent);
}
break;
}
} }} <br>
In that I got the following problem:
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//Ausgabe in andere Activity
voiceInput.setText(result.get(0));
Intent intent=new Intent(this,Zweites.class);
intent.putStringArrayListExtra("resultat",result);
//ActivityZweites wird gestartet
startActivity(intent);
}
break;
}
There it outputs the spoken input:
voiceInput.setText(result.get(0));
And afterwards it jumps into the activity.
How can I get this output in the activity without first display the output and jump in another activity?
I tried some things but don´t come to a solution how to fix this problem.
Do you guys got some ideas?
Kind regards
If you simply want to pass the same text to your intent that you're displaying in your TextView, exchange
intent.putStringArrayListExtra("resultat",result);
with
intent.putExtra("resultat", result.get(0));
Then, in your Zweites activity, you can get the string like so:
Intent intent = getIntent();
String text = intent.getStringExtra("resultat");
I am using the RecognizerIntent.ACTION_RECOGNIZE_SPEECH to capture audio using the following code:
public void promptSpeechInput() {
if (checkAudioPermission()) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Your Query Now");
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} else {
showShortSnackBar("Record Permission Required");
}
}
and I have implemented the onActivityResult as follows:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String userQuery = result.get(0);
addMessage(false, "Query: \n" + userQuery);
txtSpeechInput.setText(userQuery);
}
break;
}
}
}
Everything works fine except I get an annoying toast saying:
"Saving audio to .....#gmail.com"
How can I get rid of this toast message?
I'm making new App and i need to use voice recognition to write the words that the user said and do some algorithmic on them, What API's shall i use or a library ?
Thanks
Following two method is enough for basic understanding of Speech to text conversion:
private void promptSpeechInput() {
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,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
/**
* Receiving speech input
* */
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
I can share a complete project which is available online as well.
Download the sample project from here.
My software architecture as below:
TabActivity is a TabHost Activity.
It contain 2 ActivityGroup: AGroup and BGroup.
AGroup contain 2 Activities: A1Activity and A2Activity.
I want to implement recognizer in A1Activity.
My code as below:
private static final int VOICE_RECOGNIZER_REQUEST_CODE = 0x1008;
public void Recognizera() {
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if(activities.size() != 0) {
try {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "語音辨識");
startActivityForResult(intent, VOICE_RECOGNIZER_REQUEST_CODE);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
#Override
protected void onActivityResult(int RequestCode, int ResultCode, Intent data) {
switch(RequestCode) {
case VOICE_RECOGNIZER_REQUEST_CODE:
if(RequestCode == VOICE_RECOGNIZER_REQUEST_CODE && ResultCode == RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
for(int i = 0; i < results.size(); i++) {
System.out.println("results " + results.get(i));
}
}
break;
}
super.onActivityResult(RequestCode, ResultCode, data);
}
But it show "Unknown problem" as the picture below URL.
But without any error message in logcat.
How to modify it?
This happens because an error was encountered during the voice recognition. The ResultCode parameter will be the error code, one of the errors here. First find what is that error, so you can investigate it further.