文本到语音不起作用

时间:2014-06-02 02:08:57

标签: android text-to-speech

我想在我的应用中使用文字转语音,我有这段代码

使用此处的教程http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/

package voice;

import java.util.Locale;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;

public class CustomTTS implements TextToSpeech.OnInitListener {

    private Boolean isReady = false;
    private TextToSpeech tts;

    public CustomTTS(Context context) {
        this.tts = new TextToSpeech(context, this);
    }

    public Boolean isReady() {
        return isReady;
    }

    @Override
    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) {
                Log.e("TTS", "This Language is not supported");
            } else {
                isReady = true;
                speakOut("Hello");
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }
    }

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

    public void end() {
        tts.stop();
        tts.shutdown();
    }
}

但它不起作用。这是与活动类不同的类。在activity类中,我创建了上面一个类的实例。当我创建它时,它应该在它出现时自动说“你好”。但是当我在手机上测试时,我什么都没听到......

我正在测试软糖4.1.2。

有谁知道这里有什么不对吗?

由于

1 个答案:

答案 0 :(得分:0)

我想你无法开始说onInit方法。 在onInit返回并且textToSpeech已正确初始化后,您将能够这样做。

例如,您可以在布局中放置一个按钮,然后点击,您可以说出您想要的内容,并且引擎可以正常工作。

m_btnSpeak.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View argView) {
        // TODO Auto-generated method stub
        speakOut("Hello");
    }
});
相关问题