Android文本到语音和语音到文本

时间:2015-04-10 19:26:56

标签: android

我正在同时处理文本到语音和语音到文本。我正在制作一个应用程序,通过文本到语音提问,并通过语音和应用程序从用户获得答案将其转换为文本。但它不能正常工作。两者都在同时工作,就像它说的那样,它发短信给它。我们可以给予一些延迟,以便当它停止说话时,它会听取声音并返回该文本。 `

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends Activity implementsTextToSpeech.OnInitListener {
    TextView eText1;
    TextToSpeech textToSpeech;
    String speech = "Hey, Can u read me?";
    private final int REQ_CODE_SPEECH_INPUT = 100;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        eText1 = (TextView)findViewById(R.id.textView2);
        textToSpeech = new TextToSpeech(this,this);
    }
    @Override
    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) {
                    ArrayList<String> result = data
                       .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    eText1.setText(result.get(0));
                }
                break;
            }
        }
    }
    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_PROMPT, speech);
        try {
            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
        } catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),
                    getString(R.string.speech_not_supported),
                    Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private void speakOut() {
        String text = speech;
        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
    @Override
    public void onInit(int status) {
        if(status == TextToSpeech.SUCCESS){
            int result = textToSpeech.setLanguage(Locale.ENGLISH);
            if(result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){
                Toast.makeText(this, "This language is not supported", Toast.LENGTH_LONG).show();
            }
            else{
                speakOut();
                promptSpeechInput();
            }
        }else{
            Toast.makeText(this, "Initialization failed", Toast.LENGTH_LONG).show();
        }
    }
}

`

1 个答案:

答案 0 :(得分:0)

尝试更改:

speakOut();
promptSpeechInput();

promptSpeechInput();

然后添加:

speakOut();

eText1.setText(result.get(0));

这应该在完成文本

后说出文字

编辑:使用this确定语音何时结束。当它只是调用promptSpeechInput()