управление микрофоном,
Когда вы нажимаете на значок, чтобы говорить, он быстро закрывается, если я не говорю быстро
, и когда вы перестаете говорить, он автоматически отключается.
Я хочу, чтобы микрофон оставался даже после того, как я прекращаю говорить. Мой код < /p>
Код: Выделить всё
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton=findViewById(R.id.button);
editText=findViewById(R.id.edittext);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECORD_AUDIO},1);
}
speechRecognizer=SpeechRecognizer.createSpeechRecognizer(this);
final Intent speechRecognizerIntent= new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (count==0)
{
imageButton.setImageDrawable(getDrawable(R.drawable.ic_baseline_mic_24));
//startlistening
speechRecognizer.startListening(speechRecognizerIntent);
count=1;
}
else
{
imageButton.setImageDrawable(getDrawable(R.drawable.ic_baseline_mic_off_24));
//stop listening
speechRecognizer.stopListening();
count=0;
}
}
});
speechRecognizer.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() {
/* if (speechRecognizer != null) {
speechRecognizer.setRecognitionListener(this);
}*/
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
ArrayList data = results.getStringArrayList(speechRecognizer.RESULTS_RECOGNITION);
editText.setText(data.get(0));
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode==1)
{
if(grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT);
}
else
{
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/678 ... ic-control
Мобильная версия