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.
Related
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?
The first time I call startActivityForResult() with requestCode x it return to onActivityResult().But the second time I call startActivityForResult() within the same activity with diffrent requestCode it doesnt return to the onActivityResult() and just proceed
with the code.
I have tried to add this property to the manifest andter the activity
android:noHistory
Why it doesnt return to onActivityResult()?And how i can fix it?
Thanks a lot,
Leon
Edit: Here is my code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
myTTS = new TextToSpeech(this, this);
}
else {
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
else if(RESULT_SPEECH == requestCode)
{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textFromSpeech = text.get(0);
}
}
}
private void getTextFromSpeech()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Ops! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tts_);
/*
* New Intent purely for the purposes of checking the user data
*/
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
}
I have an application with the API Android VoiceRecognizer. It's work well but I can't get the confidence score of the result. For this I use RecognizerIntent.EXTRA_CONFIDENCE_SCORES but it's return no result. I have a device with the API 16 and I have specified on the manifest :
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
This is my code
public class MainActivity extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
private ListView mlvTextMatches;
private Button mbtSpeak;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlvTextMatches = (ListView) findViewById(R.id.lvTextMatches);
mbtSpeak = (Button) findViewById(R.id.btSpeak);
checkVoiceRecognition();
// Disable button if no recognition service is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0)
{
mbtSpeak.setEnabled(false);
mbtSpeak.setText("Recognizer not present");
}
}
public void checkVoiceRecognition() {
// Check if voice recognition is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
mbtSpeak.setEnabled(false);
mbtSpeak.setText("Voice recognizer not present");
Toast.makeText(this, "Voice recognizer not present",
Toast.LENGTH_SHORT).show();
}
}
public void speak(View view) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Straight talk please...");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "fr-FR");
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)
if(resultCode == RESULT_OK) {
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ArrayList<String> confidence = data.getStringArrayListExtra(RecognizerIntent.EXTRA_CONFIDENCE_SCORES); ;
if(confidence==null)
Log.d("VoiceRecognition","confidence null");
else
Log.d("VoiceRecognition","confidence "+confidence.size());
mlvTextMatches.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,matches));
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Have you any ideas why I haven't result for the confidence score (I have 5 results for RecognizerIntent.EXTRA_RESULTS) ?
Thank you for your help.
In the link to EXTRA_CONFIDENCE_SCORES you'll see that it returns a float array, not a string array.
Therefore you'll have to use something like this:
float [] confidence = data.getFloatArrayExtra(RecognizerIntent.EXTRA_CONFIDENCE_SCORES);
Be aware though, there are some bugs identified with the confidence scores.
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.