android 如何在将语音转换为文本时获取过去的信息?

How to get past information while converting speech to text in android?

由于我正在开发 android 应用程序并希望将语音转换为文本,因此我使用内置 Google 语音输入 activity 将语音转换为文本。我需要过去的信息,但它不断被清除,我只得到当前的回应。需要如何处理与 google 语音键盘相同。正如我所说,它包含在当前的 String 脚背上。

主活动.java

public class MainActivity extends AppCompatActivity
  {
     private EditText txtSpeechInput;
     private ImageButton btnSpeak;
     private final int REQ_CODE_SPEECH_INPUT = 100;

 @Override
  protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      txtSpeechInput =  findViewById(R.id.txtSpeechInput);
      btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
    btnSpeak.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            promptSpeechInput();
        }
    });
   private void promptSpeechInput()
      {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));
    intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000000);

  try
    {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    }
    catch (ActivityNotFoundException a)
    {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode)
    {
        case REQ_CODE_SPEECH_INPUT:
        {
            if (resultCode == RESULT_OK && null != data)
            {

              final ArrayList<String> result= data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);


                txtSpeechInput.setText(result.get(0));
            }
            break;
        }

    }
}

如果您只想保存一个先前检测到的字符串,为此,您需要创建一个全局字符串变量并将值存储在结果列表中的该变量中。(保存与您设置的相同的字符串文本视图)。但是如果你想保存所有的字符串,你需要制作全局 String Arraylist 并将所有这些字符串添加到该数组列表中。下面是代码。

private EditText txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;
private List<String> previousStringList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    previousStringList = new ArrayList<>();

    txtSpeechInput = findViewById(R.id.txtSpeechInput);
    btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
    btnSpeak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            promptSpeechInput();
        }
    });
}

private void promptSpeechInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));
    intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000000);

    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {

                final ArrayList<String> result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);


                txtSpeechInput.setText(result.get(0));
                if (result.get(0) != null) {
                    previousStringList.add(result.get(0));
                }
            }
            break;
        }

    }
}

希望对您有所帮助you.If您有什么不明白的可以随时提问。如果您不想保存相同的字符串两次(已保存的字符串),只需替换下面的条件代码行..

if (result.get(0) != null && !previousStringList.contains(result.get(0))) {
    previousStringList.add(result.get(0));
   }

单词存储在数组列表中。

您可以在此处查看实施示例,效果很好。该应用程序会存储单词,然后还会执行请求的操作。

https://github.com/saumyabahu/Travel-Safe/blob/master/MainActivity.java

public class MainActivity 扩展 AppCompatActivity {

private SpeechRecognizer speechRecognizer;
private Intent intentRecognizer;
private EditText txtSpeechInput;

private ImageButton btnSpeak;

//这是存储单词的字符串和从左到右光标过去的字符串。 String previous = " ";

// ArrayList result = null;

private final int REQ_CODE_SPEECH_INPUT = 100;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );

    // ActivityCompat.requestPermissions( this, new String[]{Manifest.permission.RECORD_AUDIO}, PackageManager.PERMISSION_GRANTED );


    txtSpeechInput = findViewById( R.id.ed );


    btnSpeak = (ImageButton) findViewById( R.id.iButton );



    btnSpeak.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            promptSpeechInput();


        }
    } );

}

private void promptSpeechInput() {
    Intent intent = new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH );
    intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM );
    intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault() );
    intent.putExtra( RecognizerIntent.EXTRA_PROMPT,
            getString( R.string.speech_prompt ) );

    intent.putExtra( RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000000 );



    try {
        startActivityForResult( intent, REQ_CODE_SPEECH_INPUT );
    } catch (ActivityNotFoundException a) {
        Toast.makeText( getApplicationContext(),
                getString( R.string.speech_not_supported ),
                Toast.LENGTH_SHORT ).show();
    }

}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult( requestCode, resultCode, data );

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {

                final ArrayList<String> result = data
                        .getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS );

//这才是真正的问题。 txtSpeechInput.setText( 前一个 + " " + result.get( 0 ) );

                previous = txtSpeechInput.getText().toString();

                txtSpeechInput.setText( previous );




            }
            break;
        }

    }
}

}