从非Activity类启动TextToSpeech?

时间:2014-03-18 01:36:39

标签: java android text-to-speech

我构建了一个帮助类,其中包含了一些我需要的文本到语音合成方法,但是我无法在没有getApplicationContext()的情况下启动TextToSpeech对象。我该如何发起?

public class SpeechHelper {
    private TextToSpeech speech;
    private Context context = null;

    public SpeechHelper(Context context)
    {
        this.context = context;
        speech = new TextToSpeech(getApplicationContext(), context);
    }

1 个答案:

答案 0 :(得分:1)

这是您实现接口的代码:

public class SpeechHelper implements OnInitListener {
    private TextToSpeech speech;
    private Context context = null;

    public SpeechHelper(Context context)
    {
        this.context = context;
        speech = new TextToSpeech(context, this);
    }

    @Override
    public void onInit(int status) {
        // TextToSpeech engine is initialized
    }
}