gpt4 book ai didi

android - Android 中文本到语音的异常行为

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:22 25 4
gpt4 key购买 nike

我在文本转语音方面有一种奇怪的体验。

查看我的代码:

Button b;
TextView title, body;
TextToSpeech tts;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.popups);
b = (Button) findViewById(R.id.buttonok);
title = (TextView) findViewById(R.id.textViewtitle);
body = (TextView) findViewById(R.id.textViewbody);
b.setText("ok");
title.setText(Receive.address);
body.setText(Receive.body);
tts = new TextToSpeech(Popup.this, new TextToSpeech.OnInitListener() {

public void onInit(int status) {
// TODO Auto-generated method stub
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
}
}
});
play();//this is not working??!!i don't know why
b.performClick();//even this is not working
/* but when i click this button it works??? how and why?*/
b.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
play(); // Popup.this.finish();
}
});
}

private void play() {
// TODO Auto-generated method stub
tts.speak(Receive.body, TextToSpeech.QUEUE_FLUSH, null);

}

只有当我点击按钮时,我的文字转语音才能正常工作,但只要我不点击这个按钮并在正常代码中写入 tts.speak(),它就不起作用......为什么?

问候查理

最佳答案

您必须等待调用onInit 才能开始speak。在您的代码中,您在声明后立即调用 play() 。 onInit 是一个回调,它需要一些时间才能被调用。如果您在按钮出现后立即单击您的按钮,有时它会失败,因为尚未调用 onInit。您应该有一个类成员 boolean mIsReady 并在 onInit 中将其设置为 true。在你的 play() 方法中

private void play() {
// TODO Auto-generated method stub
if (mIsReady) {
tts.speak(Receive.body, TextToSpeech.QUEUE_FLUSH, null);
}

关于android - Android 中文本到语音的异常行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15720730/

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