我想在来电期间将文本序列作为语音播放,将铃声降低几秒钟。我已经通过 Intent 将字符串从广播接收器发送到另一个类文件。 如果我在通话期间在 toast 中使用文本,则会显示文本,但不会播放语音。 我的类(class)文件中文本到语音的代码是:-
public class callName extends Activity {
String call;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
call = extras.getString("sms");
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
}
}
});
Toast.makeText(getBaseContext(), call, Toast.LENGTH_LONG).show();
tts.speak(call, TextToSpeech.QUEUE_FLUSH, null);
}
}
最佳答案
调用 speak 时 tts 还没有初始化,你必须把你的 speak 代码移到 onInit()
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
tts.speak(call, TextToSpeech.QUEUE_FLUSH, null);
}
}
});
关于android - 文本到语音在 android 中的来电期间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22945583/