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

时间:2018-02-21 06:20:44

标签: android speech-recognition speech-to-text google-speech-api google-voice

由于我正在开发一个Android应用程序,并希望将语音转换为文本,我使用内置的Google语音输入活动将语音转换为文本。我需要过去的信息,但它不断被清除我只得到当前的回应。如何处理谷歌语音键盘相同。正如我所说,它包含在当前的String instep of clear。

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

    }
}

3 个答案:

答案 0 :(得分:1)

如果您只想保存一个先前检测到的String,为了实现此目的,您需要创建一个全局String变量并将该值存储在结果列表中的该变量中。(保存与在文本视图上设置相同的String )。但是如果要保存所有字符串,则需要创建全局 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;
        }

    }
}

希望对你有所帮助。如果你不理解任何事情,请随时提出。如果你不想两次保存相同的字符串(已保存的字符串),只需替换下面的条件代码行。

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

答案 1 :(得分:0)

单词存储在Arraylists中。

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

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

答案 2 :(得分:0)

公共类MainActivity扩展了AppCompatActivity {

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

private ImageButton btnSpeak;

//这是存储单词的字符串以及使用左至右光标的过去的字符串。 字符串上一个=“”;

// 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 );

//这是真正的问题。 txtSpeechInput.setText(previous +“” + result.get(0));

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

                txtSpeechInput.setText( previous );




            }
            break;
        }

    }
}

}

相关问题