Tengo un problema. Tengo una aplicación en la que solicito los permisos para grabar audio. El problema es que los solicito una vez que pulso el botón de grabar pero después tengo que volver a pulsar para que grabe, yo lo que quiero es pulsar el botón, me solicite los permisos y ya grabe sin tener que volver a pulsar. El código es el siguiente:
public class MainActivity extends AppCompatActivity {
private static final int MY_PERMISSIONS_RECORD_AUDIO = 1;
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) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, MY_PERMISSIONS_RECORD_AUDIO);
}
}
private void speechToText(){
checkPermission();
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) {
}
});
}
@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) {
speechToText();
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
});
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);
}
}
});
}
}
A ver, se que una solución sería pedirlos nada más iniciar la aplicación, pero quiero pedirlos justo al pulsar el botón pero que acto seguido ya grabe la voz.