无法运行android文本到语音

时间:2014-04-13 13:05:46

标签: java android eclipse text-to-speech

我的代码如下

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    runBtn = (Button)findViewById(R.id.button_run_demo);
    ttobj = new TextToSpeech(getApplicationContext(),
            new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    ttobj.setLanguage(Locale.UK);
                }
            }
        });
    runBtn.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

        }
    });
}

public void onPause() {
    if (ttobj != null) {
        ttobj.stop();
        ttobj.shutdown();
    }
    super.onPause();
}

public void speakText(String text) {
    String toSpeak = text;
    Toast.makeText(getApplicationContext(), toSpeak,
        Toast.LENGTH_SHORT).show();
    ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {

        //do my stuff

        speakText("Hello");

    }
}   

虽然我可以做我的东西,但我无法运行speech

我错过了什么吗?

全部谢谢

1 个答案:

答案 0 :(得分:1)

onActivityResult()被调用时,TextToSpeech已经关闭,因为您在onPause()中将其关闭了。您需要设置一个标记,以便在TextToSpeech重新初始化时说“Hello”,或者将您的代码放在onPause() onDestroy()中。