android.speech.tts.TextToSpeech服务未启动

时间:2013-01-21 07:57:09

标签: android text-to-speech

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

注意:我已经从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();
    }

}

1 个答案:

答案 0 :(得分:0)

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