How to append text in edittext with real time speech to text? - android

I have an Edittext for which I am using speech to text without Activity. I would like to append text in the Edittext instead of overwriting.
I tried
editText.append(result)
editText.getText()
editText.setText(gotText + result)
but. when i use, result be overlapping.
For example , if i speak abc dragon, result is abc dragon abc dragon
Hope your help !
This is my Intent and RecognitionListner code:
public void recognizeDirectly(Intent recognizerIntent) {
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ko-KR");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(listener);
recognizer.startListening(recognizerIntent);
}
private RecognitionListener listener = new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
Log.d(TAG, "Ready for speech " + params);
}
#Override
public void onBeginningOfSpeech() {
progressBar1.setMax(10);
}
#Override
public void onRmsChanged(float rmsdB) {
progressBar1.setProgress((int) rmsdB);
}
#Override
public void onBufferReceived(byte[] buffer) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onError(int error) {
Log.d(TAG, getErrorText(error));
}
#Override
public void onResults(Bundle results) {
Log.d(TAG, "full results");
}
#Override
public void onPartialResults(Bundle partialResults) {
receiveResults(partialResults);
}
#Override
public void onEvent(int eventType, Bundle params) {
}
};
private void receiveResults(Bundle results) {
if ((results != null) && results.containsKey(SpeechRecognizer.RESULTS_RECOGNITION)) {
List<String> heard = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
resultText = heard.get(0);
txtText.append(resultText);
}
}

I seems like you're first append()'ing the result, and then reading it from the EditText object and setting the text again after you append the result to the already appended text.
Use only either
editText.append(result)
or
String gotText = editText.getText()
editText.setText(gotText + result)

Use the TextView.append() method.
The Argument will be appended at the end of the Editable.
From Official Link :
Convenience method: Append the specified text to the TextView's
display buffer, upgrading it to BufferType.EDITABLE if it was not
already editable.
For Example :
String title = bundle.getString("number1");
EditText editText = (EditText) findViewById(R.id.editText1);
editText.append(title);
If you want to set the only new value use this
editText.setText(title);

You store current text in onBeginningOfSpeech:
public String currentText;
#Override
public void onBeginningOfSpeech() {
currentText = txtText.getText();
}
and then update append recognition result in onPartialResults and in onResults
#Override
public void onResults(Bundle results) {
txtText.setText(currentText + " " +
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).get(0));
}
#Override
public void onPartialResults(Bundle partialResults) {
txtText.setText(currentText + " " +
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).get(0));
}

Related

How to increase the voice listen time in Google Recognizer Intent(Speech Recognition) Android

I did try giving time in millisecond to these below given extras
Recognizer Intent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS
Recognizer Intent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
Recognizer Intent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS
But doesn't effect the voice listen time!
The voice listen time i get now is just 3 second!
How do I achieve a Listen time of 10 seconds
It seems that it only works on ICS below :
https://stackoverflow.com/a/17675098/9427932
But you can always customize the google speech by creating your own class and inherit the speechrecognizer, I wrote these code on Xamarin Android, so it will be pretty much similar to android:
public class CustomRecognizer : Java.Lang.Object, IRecognitionListener, TextToSpeech.IOnInitListener
{
private SpeechRecognizer _speech;
private Intent _speechIntent;
public string Words;
public CustomRecognizer(Context _context)
{
this._context = _context;
Words = "";
_speech = SpeechRecognizer.CreateSpeechRecognizer(this._context);
_speech.SetRecognitionListener(this);
_speechIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
_speechIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
_speechIntent.PutExtra(RecognizerIntent.ActionRecognizeSpeech, RecognizerIntent.ExtraPreferOffline);
_speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1000);
_speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1000);
_speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 1500);
}
void startover()
{
_speech.Destroy();
_speech = SpeechRecognizer.CreateSpeechRecognizer(this._context);
_speech.SetRecognitionListener(this);
_speechIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
_speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1000);
_speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1000);
_speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 1500);
StartListening();
}
public void StartListening()
{
_speech.StartListening(_speechIntent);
}
public void StopListening()
{
_speech.StopListening();
}
public void OnBeginningOfSpeech()
{
}
public void OnBufferReceived(byte[] buffer)
{
}
public void OnEndOfSpeech()
{
}
public void OnError([GeneratedEnum] SpeechRecognizerError error)
{
Words = error.ToString();
startover();
}
public void OnEvent(int eventType, Bundle #params)
{
}
public void OnPartialResults(Bundle partialResults)
{
}
public void OnReadyForSpeech(Bundle #params)
{
}
public void OnResults(Bundle results)
{
var matches = results.GetStringArrayList(SpeechRecognizer.ResultsRecognition);
if (matches == null)
Words = "Null";
else
if (matches.Count != 0)
Words = matches[0];
else
Words = "";
//do anything you want for the result
}
startover();
}
public void OnRmsChanged(float rmsdB)
{
}
public void OnInit([GeneratedEnum] OperationResult status)
{
if (status == OperationResult.Error)
txtspeech.SetLanguage(Java.Util.Locale.Default);
}}
you can call startover() to immidiately start recording after it ends, so it will seems like a "continuous speech" (10 second in your case)

Speech only first time

I am interested in speech recognition in Android but I can't do it: it is not continuous. If you stop speaking, it doesn't continue, and you have to click on the button again.
I do not want this behaviour..
Does anybody have any suggestions as to what I can to fix this?
Recognise speech only first time I do not want this behaviour.
Here is the code:
private SpeechRecognizer speech;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
speech.startListening(intent);
}
#Override
public void onBeginningOfSpeech()
{
}
#Override
public void onBufferReceived(byte[] arg0)
{
}
#Override
public void onEndOfSpeech()
{
}
#Override
public void onError(int e)
{
}
#Override
public void onEvent(int arg0, Bundle arg1)
{
}
#Override
public void onPartialResults(Bundle arg0)
{
}
public void onReadyForSpeech(Bundle arg0)
{
}
#Override
public void onResults(Bundle data)
{
ArrayList<String> matches = data.getStringArrayList(
SpeechRecognizer.RESULTS_RECOGNITION);
}
#Override
public void onRmsChanged(float arg0)
{
}
Try to call again
speech.startListening(intent);
inside On Results and OnError.

How to get SpeechRecognizer to return non-English strings

I'm using the SpeechRecognizer to try to get some German strings back. I'm passing in a Google Translate German recording (e.g., "How are you?" -> "Wie geht es Ihnen?").
I've looked at several SO question/answers about how to change the language to accept something other than English but none seem to work.
Does anyone know how to do this while keeping the device's language setting to English (en-US)?
I'm able to successfully get German strings back only when I set the device's language to "de-DE".
Here's my sample code:
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
Button mSpeakBtn;
TextView mResultTextView;
SpeechRecognizer mSpeechRecognizer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Init Views
mSpeakBtn = (Button) findViewById(R.id.speak_btn);
mResultTextView = (TextView) findViewById(R.id.result_tv);
// Set click listener for button
mSpeakBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mResultTextView.setText("Speak now...");
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
}
#Override
public void onBeginningOfSpeech() {
}
#Override
public void onRmsChanged(float rmsdB) {
}
#Override
public void onBufferReceived(byte[] buffer) {
}
#Override
public void onEndOfSpeech() {
mResultTextView.setText("Processing...");
}
#Override
public void onError(int error) {
mResultTextView.setText("ERROR: " + error);
}
#Override
public void onResults(Bundle results) {
List<String> allWords = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Log.d(TAG, "onResults:\t" + allWords.toString());
String result = allWords.get(0);
mResultTextView.setText("RESULT: '" + result + "'");
}
#Override
public void onPartialResults(Bundle partialResults) {
}
#Override
public void onEvent(int eventType, Bundle params) {
}
});
String locale = Locale.GERMANY.toString().replace("_", "-");
Log.d(TAG, "Locale:\t" + locale);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, locale);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, locale);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication().getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
mSpeechRecognizer.startListening(intent);
}
});
}
}
I've also tried it where I'm only doing this:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, locale);
mSpeechRecognizer.startListening(intent);
But the result is the same -- getting 'English' translation of the German utterance.
Any pointers?

Use Android Speech Recognition so that it stops only at the press of a button

I want to use speech recognition to record until you press the stop button .
Do you know any way to do this?
I tried a solution but it only works as long as you do not take breaks too long(4-5s).
If you press the STOP button when it stops then starts again to work...
How can I correct it?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText = (TextView) findViewById(R.id.STT);
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizer = SpeechRecognizer
.createSpeechRecognizer(getApplicationContext());
recognizer.setRecognitionListener(this);
}
public void start(View view) {
recognizer.startListening(intent);
}
public void stop(View view) {
recognizer.stopListening();
}
#Override
public void onResults(Bundle res) {
ArrayList<String> results = res
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
myText.setText(results.get(0));
// ...
recognizer.startListening(intent);
}
#Override
public void onRmsChanged(float rmsdB) {
//...
}
#Override
public void onReadyForSpeech(Bundle params) {
//...
}
#Override
public void onPartialResults(Bundle data) {
//...
}
#Override
public void onEvent(int eventType, Bundle params) {
//...
}
#Override
public void onError(int error) {
//...
}
#Override
public void onEndOfSpeech() {
//...
}
#Override
public void onBufferReceived(byte[] buffer) {
//...
}
#Override
public void onBeginningOfSpeech() {
//...
}
This is a JB problem maybe by design. For a work around you can implement the voice recognition in a service and then send update to your UI based on the results. For an implementation of service work around see Android Speech Recognition as a service on Android 4.1 & 4.2

Android speech To Text Handling

in softkeyboard i have the option for speech to text , when i spoke it show a list of suggestion , when i select a text ,i need to fill my editText with this text, how can i done this i have see SpeechRecognizer class ,i don't know how can i use this ,please help me
SpeechRecognizer rec=SpeechRecognizer.createSpeechRecognizer(context);
RecognitionListener listener = new RecognitionListener() {
#Override
public void onRmsChanged(float rmsdB) {
}
#Override
public void onResults(Bundle results) {
ArrayList<String> voiceResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
}
#Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onPartialResults(Bundle partialResults) {
}
#Override
public void onEvent(int eventType, Bundle params) {
}
#Override
public void onError(int error) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onBufferReceived(byte[] buffer) {
}
#Override
public void onBeginningOfSpeech() {
}
};
rec.setRecognitionListener(listener);
Assuming your text edit is named "te":
public void onResults(Bundle results) {
ArrayList<String> voiceResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
StringBuilder sb = new StringBuilder();
for(String p: voiceResults) {
sb.append(p);
sb.append("\n"); } te.setText(sb.toString());
}
Normally, you are only interested in the first result (i.e voiceResults (0)) since that is the most probable match but the code above shows all of them so you can see what is returned.

Categories

Resources