Google speech recognation listener dont listen - android

Hello am running google speech recognizing service but it stops listening after seconds how can i restart it or make it loop
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplication());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getPackageName());
SpeechRecognitionListener listener = new SpeechRecognitionListener();
mSpeechRecognizer.setRecognitionListener(listener);
CountDownTimer mTimer = new CountDownTimer(1500, 500) {
#Override
public void onTick(long l) {
}
#Override
public void onFinish() {
Log.d("Speech", "Timer.onFinish: Timer Finished, Restart recognizer");
//mSpeechRecognizer.cancel();
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
};
mTimer.start();

Remove your CountdownTimer. Put this in a method and when you want to start listening call the method.
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
"Speech not supported on your device",
Toast.LENGTH_SHORT).show();
}
Speech output can be got in the onActivitySpeech method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE_SPEECH_INPUT) {
if (resultCode == RESULT_OK && data != null) {
//Handle the output
}
}
}

Related

Android Voice Recognition in background and convert to text

I have an app idea which recognizes a human voice from Android OS and convert a speech into text. I have done the process in foreground. But the same I need in implemented in Background.
For this, I think the approach. My approach is I get the activity background state and from there I invoked the service. after that whenever the application will be in the background every time the service will be invoked.
I have no idea How can I implement this.
My Foreground Source Code as
private void promptSpeechInput(){
//----takes user input----//
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//----which language users will speak
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;
}
}
}
Calling a service from activity
#Override
protected void onPause(){
super.onPause();
Log.e("Application background", "Background");
//-----Create Service From here and identify the state of background
Intent serviceIntent = new Intent(this,BackgroundService.class);
startService(serviceIntent);
}
BackgroundService.java
public class BackgroundService extends Service {
private SpeechRecognizer speech;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Log.e("servicecreate", "service");
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this, "OnCreate()", Toast.LENGTH_SHORT).show();
}
#Override
public void onDestroy() {
Log.e("servicedestroy", "service");
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "OnDestroy()", Toast.LENGTH_SHORT).show();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("servicecstart", "service");
}
}

How do I hide the speechRecogniser activity behind the main activity?

I am trying to hide the speech recogniser dialog that shows up saying "speak now" and written google as the title ,whenever i call it for passing some voice command. I don't want it to show up. Instead I want it to work in teh background so that the user cannot see it. What should be the correct way to do it?
public void getSpeechInput(View view) {
getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
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(getPackageManager()) != null) {
startActivityForResult(intent, 10);
} else {
Toast.makeText(this, "Your Device Don't Support Speech Input", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 10:
if (resultCode == RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txvResult.setText(result.get(0));
String str = txvResult.getText().toString();
if(str.equals("lights on")){
Toast.makeText(MainActivity.this,
"matched", Toast.LENGTH_LONG).show();
count[0]=1;
turnOnFlash();
}
else if(str.equals("lights off")){
Toast.makeText(MainActivity.this,
"didnt", Toast.LENGTH_LONG).show();
count[0]=0;
turnOffFlash();
}
}
break;
}
}
I tried using this but it didn't work:-
getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
Try this. This listener catches the events and you can process them accordingly.
SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(new RecognitionListener()
{
//Do whatever you want
});

Android: SpeechRecognition not working

I'm working on the speech recognition in my application and I have 2 commands Command A and Command B, Suppose if i click on the mic and speak Command A it has to start Activity A or Activity B is Command is said.
In the below code START_ACTIVITY1 IS MY COMMAND A
protected static final int RESULT_SPEECH_START_ACTIVITYA=1;
protected static final int RESULT_SPEECH_START_ACTIVITYB=2;
mic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mic.setBackgroundResource(R.drawable.micactive);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH_START_ACTIVITYA);
// manInactiveButton.setBackgroundResource(R.drawable.man_inactive);
} catch (ActivityNotFoundException e) {
Toast t = Toast.makeText(getApplicationContext(), "Oops, Your device doesn't support Speech to Text", Toast.LENGTH_LONG);
t.show();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH_START_ACTIVITYA:{
if (resultCode == RESULT_SPEECH_START_ACTIVITYA && null != data) {
ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Intent mainIntent= new Intent(First_Screen.this, Second.class);
startActivity(mainIntent);
}
}
}
}
public class VoiceRecognition implements RecognitionListener, Runnable
{
#Override
public void run()
{
recognizer = SpeechRecognizer.createSpeechRecognizer(yourContext);
recognizer.setRecognitionListener((RecognitionListener) this);
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//... all the intent stuff ...
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
recognizer.startListening(intent);
}
#Override
public void onResults(Bundle results)
{
ArrayList<String> matches;
matches=results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
}
}
....
VoiceRecognition voiceRecognizer = new VoiceRecognition();
Handler loopHandler = new Handler(Looper.getMainLooper());
loopHandler.post(voiceRecognizer);

Hide tap to speak interface google speech api

I'm trying to develop game that using voice command to access main menu. I want to ask how to hide tap to speak interface from google speech? Because it almost cover all my screen.
Call promptSpeechInput() to start the speech recognition activity.
Catch the results with onActivityResult(),
/**
* Showing google speech input dialog
* */
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;
}
}
}
For example, from a button:
speechRecButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
promptSpeechInput();
}
});
For more specific information: http://www.androidhive.info/2014/07/android-speech-to-text-tutorial/

Newer show activity for ACTION_RECOGNIZE_SPEECH

I have this code:
/**
* Showing google speech input dialog
*/
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(getActivity().getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
and i'm doing call:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
promptSpeechInput();
}
});
but i never seen activity with voice recording. Immediately there is a call:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == Activity.RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
mAdapter.addTag(result.get(0));
//txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
with resultCode == Activity.RESULT_CANCEL
I have permission to Internet.
Thank you.

Categories

Resources