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

如何在android中将语音转换为文本时获取过去的信息?,android,speech-recognition,speech-to-text,google-speech-api,google-voice,Android,Speech Recognition,Speech To Text,Google Speech Api,Google Voice,当我正在开发一个android应用程序并希望将语音转换为文本时,我正在使用内置的Google语音输入活动将语音转换为文本。我需要过去的信息,但它不断得到清除,我只有当前的回应。如何处理与谷歌语音键盘相同的问题。正如我所说,它包含了对当前字符串脚背的清除 MainActivity.java public class MainActivity extends AppCompatActivity { private EditText txtSpeechInput; privat

当我正在开发一个android应用程序并希望将语音转换为文本时,我正在使用内置的Google语音输入活动将语音转换为文本。我需要过去的信息,但它不断得到清除,我只有当前的回应。如何处理与谷歌语音键盘相同的问题。正如我所说,它包含了对当前字符串脚背的清除

MainActivity.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;
        }

    }
}
public类MainActivity扩展了AppCompatActivity
{
私有EditText txtSpeechInput;
专用图像按钮btnSpeak;
专用最终输入要求代码语音输入=100;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSpeechInput=findViewById(R.id.txtSpeechInput);
btnSpeak=(图像按钮)findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
promptSpeechInput();
}
});
私有无效promptSpeechInput()
{
意向意向=新意向(识别意向、行动、识别言语);
intent.putExtra(识别器intent.EXTRA_语言_模型,
识别者意图、语言、模型、自由形式);
intent.putExtra(RecognizerIntent.EXTRA_语言,Locale.getDefault());
intent.putExtra(识别器intent.EXTRA\u提示符,
getString(R.string.speech_提示符);
intent.putExtra(识别器intent.EXTRA语音输入最小长度,20000000);
尝试
{
startActivityForResult(意图、请求代码、语音输入);
}
捕获(ActivityNotFoundException a)
{
Toast.makeText(getApplicationContext(),
getString(不支持R.string.speech),
吐司。长度(短)。show();
}
}
受保护的void onActivityResult(int请求代码、int结果代码、意图数据)
{
super.onActivityResult(请求代码、结果代码、数据);
开关(请求代码)
{
案例请求代码语音输入:
{
if(resultCode==RESULT\u OK&&null!=数据)
{
最终ArrayList结果=数据
.getStringArrayListExtra(识别器意图.额外结果);
txtSpeechInput.setText(result.get(0));
}
打破
}
}
}

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

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

    }
}

这些单词存储在ArrayList中

你可以在这里看到一个实现的例子,它运行良好。应用程序存储单词,然后还执行请求的操作


公共类MainActivity扩展了AppCompatActivity{

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

private ImageButton btnSpeak;
//这是存储单词的字符串,并使用从左到右的光标通过字符串。 字符串previous=“”

//ArrayList结果=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 );

}

你所说的过去的信息是什么意思?我从你的问题中了解到,你也想得到以前设置的字符串吗?是的,确切地说@Abdulwaheedy你想只保存以前的字符串或连续保存所有字符串吗?想连续保存所有字符串。当他开始讲之前的字符串时,不应该清除HMMM好的,我正在更新我的答案。
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 );
                previous = txtSpeechInput.getText().toString();

                txtSpeechInput.setText( previous );




            }
            break;
        }

    }
}