TTS:成功从onInit()返回,但仍然有第一次说话的延迟

时间:2015-04-02 06:12:55

标签: android text-to-speech

使用的代码:

public class TexttoSpeechActivity extends Activity implements OnInitListener {
    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tts = new TextToSpeech(this, this);
        btnSpeak = (Button) findViewById(R.id.btnSpeak);
        txtText = (EditText) findViewById(R.id.txtText);

        btnSpeak.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                speakOut();
            }

        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

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

            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Toast.makeText(this, "Language not supported", Toast.LENGTH_LONG).show();
                Log.e("TTS", "Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
            }
        } else {
            Log.e("TTS", "Initilization Failed");
        }

    }

    private void speakOut() {
        String text = txtText.getText().toString();
        if (text.length() == 0) {
            tts.speak("You haven't typed text", TextToSpeech.QUEUE_FLUSH, null);
        } else {
            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        }

        System.out.println("apr2 tts isSpeaking "+tts.isSpeaking());
    }
}

从onInit()返回成功状态。但是第一次说话有一些延迟。即如果在启动按钮上被点击以便说话,则不能正常工作。经过一段时间的延迟(10-15秒)后,工作正常。

我可以使用处理程序等待10-15秒,如Text to Speech not working as expected中所述。 但这并不好。因为这10-15秒的延迟可能会因设备而异。 任何人都知道如何处理这个第一次延迟。

0 个答案:

没有答案