You see, I have a button in my application so that what we want to say can be transcribed by voice. The problem is that I have created a counter on the button so that when I press the button again, it stops listening, but it doesn't work:
public class MainActivity extends AppCompatActivity {
private static final int MY_PERMISSIONS_RECORD_AUDIO = 1;
int count = 0;
SpeechRecognizer mSpeechRecognizer;
Intent mSpeechRecognizerIntent;
@BindView(R.id.etBeforeTranslate)
EditText etBeforeTranslate;
@BindView(R.id.ivLogo)
ImageView ivLogo;
@BindView(R.id.btnTranslate)
Button btnTranslate;
@BindView(R.id.btnVoice)
ImageButton btnVoice;
private DatabaseReference Translates;
public String removeAccents() {...}
public String textTranslate() {...}
public void registerTranslates() {...}
private void checkPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
//When permission is not granted by user, show them message why this permission is needed.
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.RECORD_AUDIO)) {
//Give user option to still opt-in the permissions
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECORD_AUDIO},
MY_PERMISSIONS_RECORD_AUDIO);
} else {
// Show user dialog to grant permission to record audio
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECORD_AUDIO},
MY_PERMISSIONS_RECORD_AUDIO);
}
}
//If permission is granted, then go ahead recording audio
else if (ContextCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED) {
//Go ahead with recording audio now
speechToText();
}
}
private void speechToText() {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
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());
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() {
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (matches != null) {
etBeforeTranslate.setText(matches.get(0));
}
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
count = 1;
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_RECORD_AUDIO: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
speechToText();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(this, "Vaya, pues te quedas sin hablarme", Toast.LENGTH_LONG).show();
}
return;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
btnVoice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (count == 0) {
checkPermission();
speechToText();
}
else if (count == 1) {
mSpeechRecognizer.stopListening();
etBeforeTranslate.setText("holi");
}
}
});
btnTranslate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String texto = textTranslate();
if (etBeforeTranslate.getText().toString().trim().isEmpty()) {
ivLogo.setImageResource(R.drawable.logotipoangry);
Toast.makeText(MainActivity.this, "¡Ay que agobio! Intridici il tixti, anda", Toast.LENGTH_SHORT).show();
} else {
Translates = FirebaseDatabase.getInstance().getReference("Translates");
registerTranslates();
ivLogo.setImageResource(R.drawable.logotipo);
Intent intent = new Intent(MainActivity.this, TranslateActivity.class);
intent.putExtra("texto", texto);
startActivity(intent);
}
}
});
}
}
The text that I entered was to verify that it showed it to me to see that it was being carried out and indeed it changes, it shows it to me, that is to say that it enters, but that it goes, I do not get the sound as if it has stopped listening and I don't know if It is a problem of my application or of third parties, since I read in answers from years ago that this service was not working well, but I suppose that over the years it has been solved. Let's see if you can give me a hand, thank you very much :)
As you mention, a long time ago there was a "bug" in
SpeechRecognizer.stopListening()
which it was "solved", in my opinion it was actually the incorrect use of this method.If you really want to end the voice recognition service, do it by:
By the way, it is good practice to release resources, making the same call in the method
onDestroy()
of yourActivity
: