gpt4 book ai didi

java - 从非 Activity 类启动 TextToSpeech?

转载 作者:行者123 更新时间:2023-11-30 03:01:28 25 4
gpt4 key购买 nike

我使用文本到语音合成所需的几个方法构建了一个帮助程序类,但是我无法在没有 getApplicationContext() 的情况下启动 TextToSpeech 对象。我该如何启动它?

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

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

最佳答案

这是实现接口(interface)的代码:

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
}
}

关于java - 从非 Activity 类启动 TextToSpeech?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22468819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com