Android speech To Text Handling - android

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.

Related

Trying to use SpeechRecognizer (Android speech recognition) but it's not starting?

Here's my code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_route);
SetupButton();
}
private void SetupButton()
{
Button createNewMessage = (Button) findViewById(R.id.button);
createNewMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ListenForNewMessage();
}
});
}
private void ListenForNewMessage()
{
final SpeechRecognizer newDeliverySpeech = SpeechRecognizer.createSpeechRecognizer(this);
RecognitionListener newDeliveryRecognitionListener = new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
Log.d("SpeechListening","onReadyForSpeech");
}
#Override
public void onBeginningOfSpeech() {
Log.d("SpeechListening","onBeginningOfSpeech");
}
#Override
public void onRmsChanged(float rmsdB) {
//do nothing
}
#Override
public void onBufferReceived(byte[] buffer) {
//do nothing
}
#Override
public void onEndOfSpeech() {
Log.d("SpeechListening","onEndOfSpeech");
}
#Override
public void onError(int error) {
//do nothing
}
#Override
public void onResults(Bundle results) {
ArrayList<String> userMessage;
userMessage = results.getStringArrayList(RESULTS_RECOGNITION);
PushNewDelivery(userMessage);
}
#Override
public void onPartialResults(Bundle partialResults) {
//do nothing
}
#Override
public void onEvent(int eventType, Bundle params) {
//do nothing
}
};
newDeliverySpeech.setRecognitionListener(newDeliveryRecognitionListener);
if (newDeliverySpeech.isRecognitionAvailable(getApplicationContext()))
{
Log.d("SpeechListening","started listening hopefully");
newDeliverySpeech.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
}
}
}
The problem is, only the started listening hopefully is logged, the RecognitionListener never has onReadyForSpeech() or any of its methods called.
Can someone please tell me what I'm doing wrong here?
You can change this below line
newDeliverySpeech.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
To,
Intent mSpeechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test");
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
newDeliverySpeech.startListening(mSpeechIntent);
Ask the user for the permission to record audio before calling startListening:
String[] permissions = {Manifest.permission.RECORD_AUDIO};
ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);
https://developer.android.com/guide/topics/media/mediarecorder#audio-record-permission

onReadyForSpeech() in Android speech recognition (SpeechRecognizer) never gets called

Here's my code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_route);
SetupButton();
}
private void SetupButton()
{
Button createNewMessage = (Button) findViewById(R.id.button);
createNewMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ListenForNewMessage();
}
});
}
private void ListenForNewMessage()
{
final SpeechRecognizer newDeliverySpeech = SpeechRecognizer.createSpeechRecognizer(this);
RecognitionListener newDeliveryRecognitionListener = new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
Log.d("SpeechListening","onReadyForSpeech");
}
#Override
public void onBeginningOfSpeech() {
Log.d("SpeechListening","onBeginningOfSpeech");
}
#Override
public void onRmsChanged(float rmsdB) {
//do nothing
}
#Override
public void onBufferReceived(byte[] buffer) {
//do nothing
}
#Override
public void onEndOfSpeech() {
Log.d("SpeechListening","onEndOfSpeech");
}
#Override
public void onError(int error) {
//do nothing
}
#Override
public void onResults(Bundle results) {
ArrayList<String> userMessage;
userMessage = results.getStringArrayList(RESULTS_RECOGNITION);
PushNewDelivery(userMessage);
}
#Override
public void onPartialResults(Bundle partialResults) {
//do nothing
}
#Override
public void onEvent(int eventType, Bundle params) {
//do nothing
}
};
newDeliverySpeech.setRecognitionListener(newDeliveryRecognitionListener);
if (newDeliverySpeech.isRecognitionAvailable(getApplicationContext()))
{
Log.d("SpeechListening","started listening hopefully");
newDeliverySpeech.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
}
}
}
The problem is, only the started listening hopefully is logged, the RecognitionListener never has onReadyForSpeech() or any of its methods called.
details
details
details
details
Can someone please tell me what I'm doing wrong here?
You are basically creating a new SpeechRecognizer object and register a new listener each time you click on the button. On top of that you create the SpeechRecognizer using the current Activity Context but you are actually using the Application context when calling: isRecognitionAvailable();
Try to create the SpeechRecognizer as a member object and register your listener when onCreate() is called. Also try to avoid using the Application context to avoid memory leaks.
Here is an example of how you should do it.
private SpeechRecognizer mDeliverySpeech;
private Intent mSpeechIntent;
private boolean mListening = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_route);
SetupButton();
SetupSpeechRecognizer();
mSpeechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
}
private void SetupButton()
{
Button createNewMessage = (Button) findViewById(R.id.button);
createNewMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ListenForNewMessage();
}
});
}
private void SetupSpeechRecognizer()
{
mDeliverySpeech = SpeechRecognizer.createSpeechRecognizer(this);
RecognitionListener newDeliveryRecognitionListener = new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
Log.d("SpeechListening","onReadyForSpeech");
}
#Override
public void onBeginningOfSpeech() {
Log.d("SpeechListening","onBeginningOfSpeech");
}
#Override
public void onRmsChanged(float rmsdB) {
//do nothing
}
#Override
public void onBufferReceived(byte[] buffer) {
//do nothing
}
#Override
public void onEndOfSpeech() {
Log.d("SpeechListening","onEndOfSpeech");
}
#Override
public void onError(int error) {
//do nothing
}
#Override
public void onResults(Bundle results) {
ArrayList<String> userMessage;
userMessage = results.getStringArrayList(RESULTS_RECOGNITION);
PushNewDelivery(userMessage);
}
#Override
public void onPartialResults(Bundle partialResults) {
//do nothing
}
#Override
public void onEvent(int eventType, Bundle params) {
//do nothing
}
};
mDeliverySpeech.setRecognitionListener(newDeliveryRecognitionListener);
}
private void ListenForNewMessage()
{
if (mDeliverySpeech.isRecognitionAvailable(this) && !mListening)
{
Log.d("SpeechListening","started listening hopefully");
mListening = true;
mDeliverySpeech.startListening(mSpeechIntent);
new CountDownTimer(5000, 5000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
mDeliverySpeech.stopListening();
mListening = false;
}
}.start();
}
}
Do not forget to properly handle the activity life cycle when working with listener to avoid memory leak.

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

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

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.

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

Categories

Resources