语音到文字android功能

时间:2015-11-26 19:30:47

标签: java android-studio speech-recognition

我试图实施的语音转文字功能存在问题。

按下按钮开始语音到文本功能,但每次按下该按钮时,它只显示吐司消息,而不是实际将语音转换为文本

这是代码。

public class Speech extends Activity{
private TextView resultTEXT;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.speechtotext);
    resultTEXT = (TextView)findViewById((R.id.TVresult));
}

public void onButtonClick (View v)
{
    if(v.getId() == R.id.imageButton)
    {

        promptSpeechInput(); //method call
    }
}
public void promptSpeechInput()
{
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL , RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something");

    try
    {
        startActivityForResult(i, 100);
    }
    catch(ActivityNotFoundException a)
    {
        Toast.makeText(Speech.this, "Sorry, your device doesn't support speech to text", Toast.LENGTH_LONG).show();
    }
}

public void onActivityResult(int request_code, int result_code, Intent i)
{
  super.onActivityResult(request_code, result_code, i);

  switch(request_code)
  {
      case 100: if(result_code == RESULT_OK && i != null)
      {
          ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
          resultTEXT.setText(result.get(0));
      }
          break;
  }

}

}

0 个答案:

没有答案