识别器和文本到语音

时间:2016-03-25 20:40:24

标签: android speech-recognition

我想以一种方式申请,当我说话时,可以回答我,而无需按任何按钮。我希望我的录音保留在一个字符串上,然后根据记录,TTS可以回答不同的事情。这是我的源代码。

protected static final int RESULT_SPEACH =1;
private Button record, speak;

TextToSpeech t1;
String SpokedText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mai);
    record = (Button) findViewById(R.id.record);
    speak = (Button) findViewById(R.id.speak);

    speak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent reconize = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            try {
                startActivityForResult(reconize, RESULT_SPEACH);

            } catch (ActivityNotFoundException a) {
                Toast t = Toast.makeText(getApplicationContext(), "Your divece cannot execute this kind of operation", Toast.LENGTH_LONG);
                t.show();
            }
        }
    });
}

public void onActivityResult (int requestCode, int resultCode, Intent Data) {
    super.onActivityResult(requestCode,resultCode,Data);
    switch (requestCode) {
        case RESULT_SPEACH: {
            if (resultCode == RESULT_OK && null != Data) {
                final String spoked = (RecognizerIntent.EXTRA_RESULTS);
                if (spoked == "Good Morning") {
                    String SpokedText= "Good Morning";
                    t1.speak(SpokedText, TextToSpeech.QUEUE_FLUSH, null);
                } else {
                    if (spoked== "I need your help") {
                        String SpokedText= "Alright sir";
                        t1.speak(SpokedText, TextToSpeech.QUEUE_FLUSH, null);
                    }
                }
            }
        }
        t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
             @Override
             public void onInit(int status) {
                    if (status != TextToSpeech.ERROR) {
                        t1.setLanguage(Locale.getDefault());
                    }
                }  
        });
 }}

0 个答案:

没有答案
相关问题