0

我正在尝试编写一个应用程序,它应该说出我在这里给出的任何字符串是我的代码......有什么问题让我知道请告诉我......当我运行这段代码时,我得到了android.speech。 tts.TextToSpeech 服务未在 logcat 中启动错误。

注意:我已经从 Google play 安装了 TTS

public class MainActivity extends Activity implements TextToSpeech.OnInitListener {

    private TextToSpeech tts;
    private final int MY_VOICE_CHECK_CODE = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent voiceCheckIntent = new Intent();
        // Intiating voice Recognizer
        voiceCheckIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(voiceCheckIntent, MY_VOICE_CHECK_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case MY_VOICE_CHECK_CODE: {
            if (requestCode == MY_VOICE_CHECK_CODE) {
                if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                    // On Success, create the TTS(Text To Speech Synthesizer)
                    // instance
                    tts = new TextToSpeech(this, (TextToSpeech.OnInitListener) this);
                    speakOut("hello world");
                } else {
                    // missing data, install it, This will give connection to
                    // Google play(TTS data)
                    Intent installIntent = new Intent();
                    installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                    startActivity(installIntent);
                }
            }
            break;
        }    

        default:
            break;
        }
    }       

    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {

        } else {

        }
    }

    private void speakOut(String text) {
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    @Override
    protected void onDestroy() {
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

}
4

1 回答 1

0

在调用 onInit 之前,您不能调用 tts.speak。直到那时它才完全初始化。

于 2013-01-21T08:23:55.533 回答