I've implemented a SpeechRecognizer, and it's working perfectly on Android 6, but its not working on android 7.0, I don't know why.
If I try to use the recognizer on startActivityforResult(), it work's perfectly, but if I use createListener method, nothing happens, even I tried to output all results.
Here is my code :
class listener implements RecognitionListener
{
String TAG = "AFFICHAGE";
public void onReadyForSpeech(Bundle params)
{
Toast.makeText(MainActivity.this,"READY TO LISTEN", Toast.LENGTH_LONG).show();
}
public void onBeginningOfSpeech()
{
}
public void onRmsChanged(float rmsdB)
{
}
public void onBufferReceived(byte[] buffer)
{
Log.d(TAG, "onBufferReceived");
}
public void onEndOfSpeech()
{
Log.d(TAG, "onEndofSpeech");
}
public void onError(int error)
{
switch (error){
case SpeechRecognizer.ERROR_AUDIO:
Toast.makeText(MainActivity.this, "ERROR AUDIO",Toast.LENGTH_LONG).show();
break;
case SpeechRecognizer.ERROR_CLIENT:
Toast.makeText(MainActivity.this, "ERROR CLIENT",Toast.LENGTH_LONG).show();
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
Toast.makeText(MainActivity.this, "ERROR PERMISSION",Toast.LENGTH_LONG).show();
break;
case SpeechRecognizer.ERROR_NETWORK:
Toast.makeText(MainActivity.this, "ERROR INTERNET",Toast.LENGTH_LONG).show();
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
Toast.makeText(MainActivity.this, "ERROR TIMEOUT",Toast.LENGTH_LONG).show();
break;
case SpeechRecognizer.ERROR_NO_MATCH:
Toast.makeText(MainActivity.this, "ERROR NO MATCH",Toast.LENGTH_LONG).show();
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
Toast.makeText(MainActivity.this, "ERROR BUSY",Toast.LENGTH_LONG).show();
break;
case SpeechRecognizer.ERROR_SERVER:
Toast.makeText(MainActivity.this, "ERROR SERVER",Toast.LENGTH_LONG).show();
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
Toast.makeText(MainActivity.this, "ERROR TIMEOUT",Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
public void onResults(Bundle results)
{
String str = new String();
Log.d(TAG, "onResults " + results);
ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < data.size(); i++)
{
Log.d(TAG, "result " + data.get(i));
str += data.get(i);
}
txtSpeechInput.setText("results: "+String.valueOf(data.size()));
}
public void onPartialResults(Bundle partialResults)
{
Log.d(TAG, "onPartialResults");
}
public void onEvent(int eventType, Bundle params)
{
Log.d(TAG, "onEvent " + eventType);
}
}
and here is where I call it :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
btn = (Button) findViewById(R.id.button);
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new listener());
btn.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_CALLING_PACKAGE,"voice.recognition.test");
//intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
sr.startListening(intent);
Log.i("111111","11111111");
}
});
}
Thank for answer.
I've found a solution to make it work on initializiting the google librairie like this : SpeechRecognizer.createSpeechRecognizer(getApplication(), ComponentName.unflattenFromString("com.google.android.googlequicksearchbox/com.google.android.voicesearch.serviceapi.GoogleRecognitionService"));
And it's work, so I don't now if it's a good solution or not.
Related
I am writing an application, which has a WebView, that is handling a voice call via WebRTC. Microphone is working OK as I have granted permission to WebView.
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onPermissionRequest(final PermissionRequest request) {
request.grant(request.getResources());
}
Later I decided to add SpeechRecognizer so I can recognize what I am saying during the WebRTC call. I tried to make speech recognition work in the same activity, later I did it in a separate service, but unfortunally, it doesn't allow to work in a simultaneous way. Microphone gets occupied by WebView, and SpeechRecognizer doesn't get any sound (RMS is constantly -2.12). Or, if I try to run service before making call through WebView, the person I am calling to doesn't hear me at all (SpeechRecognizer occupies microphone and WebView doesn't get anything).
I hope to find any solution if it exists. I am not an iOS developer, but I've heard, that it's possible on iPhone, so it's a surprise, that is impossible on Android device.
My Speech Recognition service code
public class RecognitionService extends Service implements RecognitionListener {
private String LOG_TAG = "RecognitionService";
private SpeechRecognizer speech = null;
private Intent recognizerIntent;
public RecognitionService() {
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
startRecognition();
return null;
}
#Override
public void onCreate() {
Log.i("Test", "RecognitionService: onCreate");
startRecognition();
}
private void startRecognition() {
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
"ru-RU");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
speech.startListening(recognizerIntent);
}
#Override
public void onBeginningOfSpeech() {
Log.i(LOG_TAG, "onBeginningOfSpeech");
}
#Override
public void onBufferReceived(byte[] buffer) {
Log.i(LOG_TAG, "onBufferReceived: " + buffer);
}
#Override
public void onEndOfSpeech() {
Log.i(LOG_TAG, "onEndOfSpeech");
}
#Override
public void onError(int errorCode) {
String errorMessage = getErrorText(errorCode);
Log.d(LOG_TAG, "FAILED " + errorMessage);
speech.destroy();
startRecognition();
}
#Override
public void onEvent(int arg0, Bundle arg1) {
Log.i(LOG_TAG, "onEvent");
}
#Override
public void onPartialResults(Bundle arg0) {
Log.i(LOG_TAG, "onPartialResults");
}
#Override
public void onReadyForSpeech(Bundle arg0) {
Log.i(LOG_TAG, "onReadyForSpeech");
}
#Override
public void onResults(Bundle results) {
Log.i(LOG_TAG, "onResults");
ArrayList<String> matches = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String text = "";
for (String result : matches)
text += result + "\n";
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();
speech.destroy();
startRecognition();
}
public static String getErrorText(int errorCode) {
String message;
switch (errorCode) {
case SpeechRecognizer.ERROR_AUDIO:
message = "Audio recording error";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "Client side error";
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "Insufficient permissions";
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "Network error";
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "Network timeout";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "No match";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "RecognitionService busy";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "error from server";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "No speech input";
break;
default:
message = "Didn't understand, please try again.";
break;
}
return message;
}
#Override
public void onRmsChanged(float rmsdB) {
Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
}
}
I want to convert speech to text in my app..
For this i am using recognitionListener interface
Everything works fine but how to get the text updated and shown even while speaking( like in google now voice search)
I have set the RecognizerIntent.EXTRA_PARTIAL_RESULTS, to true
And also used the onPartialResults(Bundle arg() method of the recognitionListener interface to set text
By the whole text is getting displayed at once after speech recognition is complete
But i want the realtime text to be displayed as the user speaks
my activity
public class MainActivity extends Activity implements RecognitionListener
{
private TextView returnedText;
private ToggleButton toggleButton;
private ProgressBar progressBar;
private SpeechRecognizer speech = null;
private Intent recognizerIntent;
private String LOG_TAG = "VoiceRecognitionActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
returnedText = (TextView) findViewById(R.id.textView1);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
Button recordbtn = (Button) findViewById(R.id.mainButton);
progressBar.setVisibility(View.INVISIBLE);
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
"en");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 3000);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
speech.startListening(recognizerIntent);
} else {
progressBar.setIndeterminate(false);
progressBar.setVisibility(View.INVISIBLE);
speech.stopListening();
}
}
});
recordbtn.setOnLongClickListener(new OnLongClickListener(){
#Override
public boolean onLongClick(View p1)
{
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
speech.startListening(recognizerIntent);
return true;
}
});
}
#Override
public void onResume() {
super.onResume();
}
#Override
protected void onPause() {
super.onPause();
if (speech != null) {
speech.destroy();
Log.i(LOG_TAG, "destroy");
}
}
#Override
public void onBeginningOfSpeech() {
Log.i(LOG_TAG, "onBeginningOfSpeech");
progressBar.setIndeterminate(false);
progressBar.setMax(10);
}
#Override
public void onBufferReceived(byte[] buffer) {
Log.i(LOG_TAG, "onBufferReceived: " + buffer);
}
#Override
public void onEndOfSpeech() {
Log.i(LOG_TAG, "onEndOfSpeech");
progressBar.setIndeterminate(true);
toggleButton.setChecked(false);
}
#Override
public void onError(int errorCode) {
String errorMessage = getErrorText(errorCode);
Log.d(LOG_TAG, "FAILED " + errorMessage);
returnedText.setText(errorMessage);
toggleButton.setChecked(false);
}
#Override
public void onEvent(int arg0, Bundle arg1) {
Log.i(LOG_TAG, "onEvent");
}
#Override
public void onPartialResults(Bundle arg0) {
Log.i(LOG_TAG, "onPartialResults");
ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String text = "";
for (String result : matches)
text += result + "\n";
returnedText.setText(text);
}
#Override
public void onReadyForSpeech(Bundle arg0) {
Log.i(LOG_TAG, "onReadyForSpeech");
}
#Override
public void onResults(Bundle results) {
Log.i(LOG_TAG, "onResults");
}
#Override
public void onRmsChanged(float rmsdB) {
Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
progressBar.setProgress((int) rmsdB);
}
public static String getErrorText(int errorCode) {
String message;
switch (errorCode) {
case SpeechRecognizer.ERROR_AUDIO:
message = "Audio recording error";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "Client side error";
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "Insufficient permissions";
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "Network error";
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "Network timeout";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "No match";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "RecognitionService busy";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "error from server";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "No speech input";
break;
default:
message = "Didn't understand, please try again.";
break;
}
return message;
}
}
HOW TO MAKE THIS HAPPEN
You can't achieve a realtime recognition with google API. In the best case you can achieve the same result than google when you are using OK Google or for example Recognition in Whatsup for write Text word by word adding to your intent:
recognizerIntent.putExtra("android.speech.extra.DICTATION_MODE", true);
The Speechrecognizer take his time to process all the info and split it as info to be able to use it in your app. you can check the next post to see if it help you optimizing your app: Make SpeechRecognizer Faster
Hope that it will help you !
I want to integrate speech recognition in my app.
for example: when user say specific word i put it in code like "hello"
it will capture a picture from my app.
I tried many code put about speech recognition but can't integrate it with my app
how to integrate into my app ?
how to make specific word to make capture ?
Show speech to text
public class MainActivity extends Activity {
private TextView txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
// hide the action bar
getActionBar().hide();
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
promptSpeechInput();
}
});
}
/**
* 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;
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Get speech in txtSpeechInput box .
I think this is going to help you
public class YourClassName RecognitionListener{
private SpeechRecognizer speech = null;
private Intent recognizerIntent;
private String LOG_TAG = "VoiceRecognitionActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_camera);
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
"en");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication().getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
speech.startListening(recognizerIntent);
}
#Override
public void onReadyForSpeech(Bundle params) {
Log.i(LOG_TAG+">"+ "onReadyForSpeech");
}
#Override
public void onBeginningOfSpeech() {
Log.i(LOG_TAG+"> "+"onBeginningOfSpeech");
}
#Override
public void onRmsChanged(float rmsdB) {
Log.i(LOG_TAG+">"+ "onRmsChanged: " + rmsdB);
}
#Override
public void onBufferReceived(byte[] buffer) {
Log.i(LOG_TAG+">"+ "onBufferReceived: " + buffer);
}
#Override
public void onEndOfSpeech() {
Log.i(LOG_TAG+">"+ "onEndOfSpeech");
}
#Override
public void onError(int error) {
String errorMessage = getErrorText(error);
Log.d(LOG_TAG+">"+ "FAILED " + errorMessage);
if(errorMessage.contains("RecognitionService busy"))
{ speech.stopListening();
speech.startListening(recognizerIntent);
}else if(errorMessage.contains("No speech input")){
speech.stopListening();
speech.startListening(recognizerIntent);
}else if(errorMessage.contains("No match")){
speech.stopListening();
speech.startListening(recognizerIntent);
}
// speech.destroy();
// speech.startListening(recognizerIntent);
}
#Override
public void onResults(Bundle results) {
Log.i(LOG_TAG+">"+ "onResults");
ArrayList<String> matches = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String text = "";
for (String result : matches)
text += result + "\n";
if(text.contains("your Keyword here")){
camera.takePicture(null, null, this);
Log.v(text+"these are results");
}
Log.v(text+"On result");
}
#Override
public void onPartialResults(Bundle partialResults) {
Log.i(LOG_TAG+">"+ "onPartialResults");
}
#Override
public void onEvent(int eventType, Bundle params) {
Log.i(LOG_TAG+">"+ "onEvent");
}
public String getErrorText(int errorCode) {
String message;
switch (errorCode) {
case SpeechRecognizer.ERROR_AUDIO:
message = "Audio recording error";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "Client side error";
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "Insufficient permissions";
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "Network error";
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "Network timeout";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "No match";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "RecognitionService busy";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "error from server";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "No speech input";
break;
default:
message = "Didn't understand, please try again.";
break;
}
return message;
}
}
I am trying to sync an audio file and a lyrics file(String format). After following many posts on SO, I tried using Speech To Text to get the text from audio. But it doesn't listen the audio,it waits for the speech. And lyrics file need to be converted into an SRT file in accordance with the song. How could the below be possible?
Listening to audio file playing using TTS ? Is this the right way to do it?
How to convert lyrics file(string format) into an SRT file ?
Any suggestions/guidance will be greatly appreciated.
The below code waits for me to speak some words.But I need it to listen to the audio file playing.
Code I have tried so far :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new listener());
class listener implements RecognitionListener
{
public void onReadyForSpeech(Bundle params)
{
Log.d(TAG, "onReadyForSpeech");
}
public void onBeginningOfSpeech()
{
Log.d(TAG, "onBeginningOfSpeech");
}
public void onRmsChanged(float rmsdB)
{
Log.d(TAG, "onRmsChanged");
}
public void onBufferReceived(byte[] buffer)
{
Log.d(TAG, "onBufferReceived");
}
public void onEndOfSpeech()
{
Log.d(TAG, "onEndofSpeech");
}
public void onError(int error)
{
String mError=null;
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
switch (error) {
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
mError = " network timeout";
sr.startListening(intent);
break;
case SpeechRecognizer.ERROR_NETWORK:
mError = " network" ;
//toast("Please check data bundle or network settings");
return;
case SpeechRecognizer.ERROR_AUDIO:
mError = " audio";
break;
case SpeechRecognizer.ERROR_SERVER:
mError = " server";
sr.startListening(intent);
break;
case SpeechRecognizer.ERROR_CLIENT:
mError = " client";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
mError = " speech time out" ;
break;
case SpeechRecognizer.ERROR_NO_MATCH:
mError = " no match" ;
sr.startListening(intent);
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
mError = " recogniser busy" ;
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
mError = " insufficient permissions" ;
break;
}
Log.d(TAG, "error " + mError);
}
public void onResults(Bundle results)
{
String str = new String();
Log.d(TAG, "onResults " + results);
ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < data.size(); i++)
{
Log.d(TAG, "result " + data.get(i));
str += data.get(i);
}
}
public void onPartialResults(Bundle partialResults)
{
Log.d(TAG, "onPartialResults");
}
public void onEvent(int eventType, Bundle params)
{
Log.d(TAG, "onEvent " + eventType);
}
}
public void onSongPicked()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
sr.startListening(intent);}
I am using the Android's speech API to continuously getting input from the user. However this doesn't work quite well when errors occur.
What I do is restarting the listener in the method that detects error. It works sometime but the recognizer hangs often for some time. Especially after detecting Server, network time out and recognizer busy errors. This is annoying!
I have found some attempt to solve this problem, but none of them worked for me.
Do you have a better idea?
Here i my code:
private void startSR(){
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//intent.putExtra(RecognizerIntent., value)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, mContext.getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
Log.d(TAG,"Speech recognition started!");
if (recognizer != null) {
recognizer = null;
mListener = null;
}
Log.d(TAG,"setRecognitionListener");
recognizer = SpeechRecognizer.createSpeechRecognizer(mContext);
mListener = new Listener();
recognizer.setRecognitionListener(mListener);
recognizer.startListening(intent);
}
class Listener implements RecognitionListener{
#Override
public void onBeginningOfSpeech() {
Log.i(TAG, "onBeginningOfSpeech");
mStatus = "Beginning speech";
}
#Override
public void onBufferReceived(byte[] buffer) {
Log.i(TAG, "onBufferReceived");
}
#Override
public void onEndOfSpeech() {
Log.i(TAG, "onEndOfSpeech");
mStatus = "Speech ended";
}
#Override
public void onEvent(int eventType, Bundle params) {
Log.i(TAG, "onEvent " + eventType);
}
#Override
public void onPartialResults(Bundle partialResults) {
Log.i(TAG, "onPartialResults");
mStatus = "Partial results";
}
#Override
public void onReadyForSpeech(Bundle params) {
Log.i(TAG, "onReadyForSpeech");
mReady = true;
mStatus = "Speech engine ready";
}
#Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
#Override
public void onError(int error) {
// TODO Auto-generated method stub
mError = "";
mStatus = "Error detected";
switch (error) {
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
mError = " network timeout";
startListening();
break;
case SpeechRecognizer.ERROR_NETWORK:
mError = " network" ;
//toast("Please check data bundle or network settings");
return;
case SpeechRecognizer.ERROR_AUDIO:
mError = " audio";
break;
case SpeechRecognizer.ERROR_SERVER:
mError = " server";
startListening();
break;
case SpeechRecognizer.ERROR_CLIENT:
mError = " client";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
mError = " speech time out" ;
break;
case SpeechRecognizer.ERROR_NO_MATCH:
mError = " no match" ;
startListening();
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
mError = " recogniser busy" ;
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
mError = " insufficient permissions" ;
break;
}
Log.i(TAG, "Error: " + error + " - " + mError);
//startSR();
}
#Override
public void onResults(Bundle results) {
mStatus = "Got some results";
mResultAvailable = true;
String str = new String();
Log.d(TAG, "onResults " + results);
mResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
//mConfidences = results.getDoubleArray(SpeechRecognizer.CONFIDENCE_SCORES);
Log.i(TAG, toString());
startListening();
}
}// class Listener
public ArrayList<String> getResults(){
return mResults;
}
public void startListening(){
if (SpeechRecognizer.isRecognitionAvailable(mContext)) {
if (recognizer!=null){
recognizer.startListening(intent);
mResultAvailable = false;
mResults = new ArrayList<String>();
}
else
startSR();
}
}
If you get recognizer busy error you have to call cancel and then call startListening
If you get server or network errors you have to check for network connection before calling startListening
Intent RC = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
if (!RC.hasExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE))
{
RC.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.dummy");
}
sr.startListening(RC);
Try to properly clean up after the error, e.g. call cancel or destroy on the SpeechRecognizer.
I m posting this late , but this might help some one.
I was also facing same error , & also it was not listening after some random time ,
i tried the following & it helped me somewhat, Try doing this ,
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
recognizer.destroy();
startSR(); // As it destroys the current objects & calling startSR() will instantiate
// objects again