Android 语音引擎的字符串输入
在我看来,类 TextToSpeech 的方法“speak”仅适用于方法 onInit 或 onUtteranceCompleted。但是,onInit 和 onUtteranceCompleted 没有任何用于传递字符串的参数。
在下面的代码中,我尝试在方法之外定义一个全局字符串数组列表,并使用数组列表作为字符串输入。由于某种原因,它没有成功。但引擎确实会说“你睡得好吗”。任何帮助表示赞赏。
public class TTS extends Activity implements OnInitListener,OnUtteranceCompletedListener,Runnable {
ArrayList<String> content=new ArrayList<String>();
int MY_DATA_CHECK_CODE=50;
private TextToSpeech mTts;
公共无效onCreate(捆绑保存实例状态){
content.add("test");
content.add("another test");
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
mTts = new TextToSpeech(this,this);
} else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
public void onInit(int status){
if(status==TextToSpeech.SUCCESS){
mTts.setLanguage(Locale.US);
mTts.setOnUtteranceCompletedListener(this);
String myText1 = "Did you sleep well?";
mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
for(int i=0;i<content.size();i++){
mTts.speak(content.get(i),TextToSpeech.QUEUE_ADD,null);
}
if(status==TextToSpeech.ERROR){
mTts.shutdown();
}
}
}
It seems to me that the method "speak" of class TextToSpeech only works in method onInit or onUtteranceCompleted. However, onInit and onUtteranceCompleted don't have any parameter for passing strings.
In the following code, I tried to define a global string arraylist outside the methods and used the arraylist for string input.For some reason , it didn't work out.But the engine did speak "did you sleep well". Any help is appreciated.
public class TTS extends Activity implements OnInitListener,OnUtteranceCompletedListener,Runnable {
ArrayList<String> content=new ArrayList<String>();
int MY_DATA_CHECK_CODE=50;
private TextToSpeech mTts;
public void onCreate(Bundle savedInstanceState) {
content.add("test");
content.add("another test");
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
mTts = new TextToSpeech(this,this);
} else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
public void onInit(int status){
if(status==TextToSpeech.SUCCESS){
mTts.setLanguage(Locale.US);
mTts.setOnUtteranceCompletedListener(this);
String myText1 = "Did you sleep well?";
mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
for(int i=0;i<content.size();i++){
mTts.speak(content.get(i),TextToSpeech.QUEUE_ADD,null);
}
if(status==TextToSpeech.ERROR){
mTts.shutdown();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您的某些代码丢失了,但仅供参考,可以通过参数映射为话语分配 ID,例如:
“第一个话语的 ID”将传递给
onUtteranceCompleted(String utteranceId)
请参阅使用文本转语音。
I believe some of your code is missing, but FYI it is possible to assign an ID to an utterance via the parameters map, e.g.:
"ID of First Utterance" will be passed to
onUtteranceCompleted(String utteranceId)
Please see Using Text-to-Speech.