Handling RecognitionListener Errors - android

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

Related

Restart SpeechRecognizer using onUtteranceProgressListener

I've been working on this code for weeks with no progress at all, even though it seems like such an easy fix!! Basically I am using TextToSpeech with SpeechRecognizer in order to have the app ask me a question and then listen for my answer. Here's my code... I'm thinking its a thread problem or something.
Basically when I hold down the button, SPeechRecognizer starts listening until i take away finger... this works fine.. but I programmatically want the SpeechRecognizer to start up again after Utterance is Completed or simulate a button press to get it to work but neither works...
public class MainActivity extends AppCompatActivity {
TextToSpeech textToSpeech;
// SpeechRecognizer
SpeechRecognizer mSpeechRecognizer;
Intent mSpeechRecognizerIntent;
// Speech Analysis Support
SpeechToTextAnalyzer speechToTextAnalyzer;
String speechToTextAnalyzerResponse;
String SPEECH_RESPONSE_BACK;
int buttonPressCounter;
boolean responsive;
TextView StatusDisplay, WordingDisplay;
Button SimulateButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPermission();
buttonPressCounter = 0;
// Initialize TextToSpeech
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.CANADA);
}
textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
// Speaking started.
}
#Override
public void onDone(String utteranceId) {
// Speaking stopped.
if(responsive){ // if(utteranceId == "LISTEN_FOR_NEW_RESPONSE")
// possible method one:
SimulateButton.performClick();
// possible method two:
//mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
// NOTE: SO FAR NEITHER OF THESE LINES MAKE THE SPEECHRECOGNIZER START UP WITHOUT ME TOUCHING THE BUTTON!!!
}
}
#Override
public void onError(String utteranceId) {
// There was an error.
}
});
}
});
// Initialize UI View Components
StatusDisplay = (TextView)findViewById(R.id.StatusUpdater);
WordingDisplay = (TextView)findViewById(R.id.WordsHeard);
// Create Reference to Classes
speechToTextAnalyzer = new SpeechToTextAnalyzer(this, this);
// Initialize SpeechRecognizer and Intent
final SpeechRecognizer mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
final Intent mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
Locale.getDefault());
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle bundle) {
}
#Override
public void onBeginningOfSpeech() {
}
#Override
public void onRmsChanged(float v) {
}
#Override
public void onBufferReceived(byte[] bytes) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onError(int errorCode) {
String errorMessage = getErrorText(errorCode);
//Log.d(LOG_TAG, "FAILED " + errorMessage);
//listeningOn = false; // UNSURE!!
}
// accompanying text for onError
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 = "Couldn't Understand, Please Try Again";
break;
}
return message;
}
#Override
public void onResults(Bundle bundle) {
//getting all the matches
ArrayList<String> matches = bundle
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String firstMatch = "";
//displaying the first match
if (matches != null) {
for(int i = 0; i < matches.size(); i++){
firstMatch = matches.get(i); // get the last one, which has the actual words I spoke...
}
matches.clear();
}
//StatusDisplay.setText("Not Listening...");
// ENTRY POINT FOR ALL COMMAND FUNCTIONALITY
speechToTextAnalyzerResponse = speechToTextAnalyzer.AnalyzeSpeechSingleResponse(firstMatch);
buttonPressCounter = speechToTextAnalyzer.AnalyzeSpeechCounter(buttonPressCounter);
responsive = speechToTextAnalyzer.AnalyzeSpeechResponseThenListen(buttonPressCounter);
SPEECH_RESPONSE_BACK = speechToTextAnalyzerResponse;
WordingDisplay.setText(SPEECH_RESPONSE_BACK);
if(responsive){
RESPONSIVE_SPEECH("... okay now say something new ... ");
}
else {
SINGLE_RESPONSE();
}
}
#Override
public void onPartialResults(Bundle bundle) {
}
#Override
public void onEvent(int i, Bundle bundle) {
}
}); // end of mSpeechRecognizer.setRecognitionListener
SimulateButton = (Button)findViewById(R.id.DoSomething);
SimulateButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_UP:
//when the user removed the finger
mSpeechRecognizer.stopListening();
StatusDisplay.setText("Not Listening...");
break;
case MotionEvent.ACTION_DOWN:
//finger is on the button
StatusDisplay.setText("Listening...");
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
// WHILE I HOLD BUTTON THIS ACTUALLY WORKS BUT NOT INSIDE ONUTTERANCEPROGRESSLISTENER.........
break;
}
return true;
}
});
} // end of onCreate()
public void SINGLE_RESPONSE(){
if (!textToSpeech.isSpeaking()) {
textToSpeech.speak(SPEECH_RESPONSE_BACK, TextToSpeech.QUEUE_FLUSH, null);
//textToSpeech.speak(WordingDisplay.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
} else {
textToSpeech.stop();
}
}
public void RESPONSIVE_SPEECH(String PleaseSayThis){
SPEECH_RESPONSE_BACK = PleaseSayThis;
// https://android-developers.googleblog.com/2009/09/introduction-to-text-to-speech-in.html
if (!textToSpeech.isSpeaking()) {
HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
stringStringHashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "LISTEN_FOR_NEW_RESPONSE"); // when using normal text to speech, the ID is "ID_MAIN_CALL" to be searched for in onUtteranceCompleted
textToSpeech.speak(SPEECH_RESPONSE_BACK, textToSpeech.QUEUE_FLUSH, stringStringHashMap);
} else {
textToSpeech.stop();
}
}
private void checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + getPackageName()));
startActivity(intent);
finish();
}
}
}
protected void onDestroy(){
if(textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
textToSpeech = null;
}
super.onDestroy();
}
According to the UtteranceProgressListener docs, "The callbacks specified in this method can be called from multiple threads."
To verify this, you can add
boolean wasCalledFromBackgroundThread = (Thread.currentThread().getId() != 1);
Log.i("XXX", "was onDone() called on a background thread? : " + wasCalledFromBackgroundThread);
to the onDone() method body.
In my experience, they are called on background threads more often than not.
So, inside your onDone() method body, I would suggest surrounding the UI manipulation with runOnUiThread() like so:
#Override
public void onDone(String utteranceId) {
runOnUiThread(new Runnable() {
#Override
public void run() {
// possible method one:
SimulateButton.performClick();
// possible method two:
//mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
});
}

SpeechRecognizer on Android 7.0

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.

Speech recognition while transmitting voice from microphone through WebView WebRTC

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);
}
}

Android speech to text :: getting text updated at realtime

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 !

Syncing audio with lyrics

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);}

Categories

Resources