Android 中未启动文本转语音服务
我正在尝试制作这样的应用程序,当图像自行加载时它会自动说话。 我尝试过一个 TTS 应用程序(示例),该应用程序工作正常,但这段代码给了我 我的代码是文本转语音服务未启动。
public class show_image extends Activity implements TextToSpeech.OnInitListener {
Intent mIntent;
String value;
ImageView mImageView;
private TextToSpeech tts;
int j;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIntent = getIntent();
Bundle b = getIntent().getExtras();
final String name = b.getString("name");
j = name.toCharArray()[0];
tts = new TextToSpeech(this, this);
tts.speak("Welcome", TextToSpeech.QUEUE_ADD, null);
mImageView = (ImageView) findViewById(R.id.imageView1);
switch (j) {
case 65:
mImageView.setBackgroundResource(R.drawable.a);
tts.speak(name + " for Apple", TextToSpeech.QUEUE_ADD, null);
// Toast.makeText(getApplicationContext(),
// "Saying:" + name + " for Apple", Toast.LENGTH_SHORT)
// .show();
tts.isLanguageAvailable(Locale.ENGLISH);
break;
case 66:
mImageView.setBackgroundResource(R.drawable.b);
tts.speak(name + "for Bluetooth", TextToSpeech.QUEUE_ADD, null);
break;
case 67:
mImageView.setBackgroundResource(R.drawable.c);
tts.speak(name + "for Chat", TextToSpeech.QUEUE_ADD, null);
break;
case 68:
mImageView.setBackgroundResource(R.drawable.d);
tts.speak(name + "for Download", TextToSpeech.QUEUE_ADD, null);
break;
case 69:
mImageView.setBackgroundResource(R.drawable.e);
tts.speak(name + "for E Mail", TextToSpeech.QUEUE_ADD, null);
break;
case 70:
mImageView.setBackgroundResource(R.drawable.f);
tts.speak(name + "for Facebook", TextToSpeech.QUEUE_ADD, null);
break;
case 71:
mImageView.setBackgroundResource(R.drawable.g);
tts.speak(name + "for Google", TextToSpeech.QUEUE_ADD, null);
break;
case 72:
mImageView.setBackgroundResource(R.drawable.h);
tts.speak(name + "for Hewlwtt Packard", TextToSpeech.QUEUE_ADD,
null);
break;
case 73:
mImageView.setBackgroundResource(R.drawable.i);
tts.speak(name + "for Iphone", TextToSpeech.QUEUE_ADD, null);
break;
case 74:
mImageView.setBackgroundResource(R.drawable.j);
tts.speak(name + "for Java", TextToSpeech.QUEUE_ADD, null);
break;
case 75:
mImageView.setBackgroundResource(R.drawable.k);
tts.speak(name + "for kingston", TextToSpeech.QUEUE_ADD, null);
break;
case 76:
mImageView.setBackgroundResource(R.drawable.l);
tts.speak(name + "for Laptop", TextToSpeech.QUEUE_ADD, null);
break;
case 77:
mImageView.setBackgroundResource(R.drawable.m);
tts.speak(name + "for Messenger", TextToSpeech.QUEUE_ADD, null);
break;
case 78:
mImageView.setBackgroundResource(R.drawable.n);
tts.speak(name + "for Nero", TextToSpeech.QUEUE_ADD, null);
break;
case 79:
mImageView.setBackgroundResource(R.drawable.o);
tts.speak(name + "for Orkut", TextToSpeech.QUEUE_ADD, null);
break;
case 80:
mImageView.setBackgroundResource(R.drawable.p);
tts.speak(name + "for Picassa", TextToSpeech.QUEUE_ADD, null);
break;
case 81:
mImageView.setBackgroundResource(R.drawable.q);
tts.speak(name + "for Quick Heal", TextToSpeech.QUEUE_ADD, null);
break;
case 82:
mImageView.setBackgroundResource(R.drawable.r);
tts.speak(name + "for Ram", TextToSpeech.QUEUE_ADD, null);
break;
case 83:
mImageView.setBackgroundResource(R.drawable.s);
tts.speak(name + "for Server", TextToSpeech.QUEUE_ADD, null);
break;
case 84:
mImageView.setBackgroundResource(R.drawable.t);
tts.speak(name + "for Twitter", TextToSpeech.QUEUE_ADD, null);
break;
case 85:
mImageView.setBackgroundResource(R.drawable.u);
tts.speak(name + "for Usb", TextToSpeech.QUEUE_ADD, null);
break;
case 86:
mImageView.setBackgroundResource(R.drawable.v);
tts.speak(name + "for Vista", TextToSpeech.QUEUE_ADD, null);
break;
case 87:
mImageView.setBackgroundResource(R.drawable.w);
tts.speak(name + "for WiFi", TextToSpeech.QUEUE_ADD, null);
break;
case 88:
mImageView.setBackgroundResource(R.drawable.x);
tts.speak(name + "for XP", TextToSpeech.QUEUE_ADD, null);
break;
case 89:
mImageView.setBackgroundResource(R.drawable.y);
tts.speak(name + "for Youtube", TextToSpeech.QUEUE_ADD, null);
break;
case 90:
mImageView.setBackgroundResource(R.drawable.z);
tts.speak(name + "for Zorpia", TextToSpeech.QUEUE_ADD, null);
break;
default:
break;
}
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, RESULT_OK);
final Thread t1 = new Thread() {
public void run() {
try {
Thread.sleep(10000);
finish();
// this.destroy();
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
};
t1.start();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RESULT_OK) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
tts = 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) {
Toast.makeText(this,
"Write and Listen engine is initialized & Ready",
Toast.LENGTH_LONG).show();
} else if (status == TextToSpeech.ERROR) {
Toast.makeText(this,
"Error occurred while initializing Text-To-Speech engine",
Toast.LENGTH_LONG).show();
}
}
protected void onDestroy() {
super.onDestroy();
if (tts != null) {
tts.shutdown();
}
}
}
I am trying make such application which speak automatically when the image loaded it self.
I had tried a single application (example) of TTS which is working properly but this code gives me
My Code is Text To Speech service isn't started.
public class show_image extends Activity implements TextToSpeech.OnInitListener {
Intent mIntent;
String value;
ImageView mImageView;
private TextToSpeech tts;
int j;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIntent = getIntent();
Bundle b = getIntent().getExtras();
final String name = b.getString("name");
j = name.toCharArray()[0];
tts = new TextToSpeech(this, this);
tts.speak("Welcome", TextToSpeech.QUEUE_ADD, null);
mImageView = (ImageView) findViewById(R.id.imageView1);
switch (j) {
case 65:
mImageView.setBackgroundResource(R.drawable.a);
tts.speak(name + " for Apple", TextToSpeech.QUEUE_ADD, null);
// Toast.makeText(getApplicationContext(),
// "Saying:" + name + " for Apple", Toast.LENGTH_SHORT)
// .show();
tts.isLanguageAvailable(Locale.ENGLISH);
break;
case 66:
mImageView.setBackgroundResource(R.drawable.b);
tts.speak(name + "for Bluetooth", TextToSpeech.QUEUE_ADD, null);
break;
case 67:
mImageView.setBackgroundResource(R.drawable.c);
tts.speak(name + "for Chat", TextToSpeech.QUEUE_ADD, null);
break;
case 68:
mImageView.setBackgroundResource(R.drawable.d);
tts.speak(name + "for Download", TextToSpeech.QUEUE_ADD, null);
break;
case 69:
mImageView.setBackgroundResource(R.drawable.e);
tts.speak(name + "for E Mail", TextToSpeech.QUEUE_ADD, null);
break;
case 70:
mImageView.setBackgroundResource(R.drawable.f);
tts.speak(name + "for Facebook", TextToSpeech.QUEUE_ADD, null);
break;
case 71:
mImageView.setBackgroundResource(R.drawable.g);
tts.speak(name + "for Google", TextToSpeech.QUEUE_ADD, null);
break;
case 72:
mImageView.setBackgroundResource(R.drawable.h);
tts.speak(name + "for Hewlwtt Packard", TextToSpeech.QUEUE_ADD,
null);
break;
case 73:
mImageView.setBackgroundResource(R.drawable.i);
tts.speak(name + "for Iphone", TextToSpeech.QUEUE_ADD, null);
break;
case 74:
mImageView.setBackgroundResource(R.drawable.j);
tts.speak(name + "for Java", TextToSpeech.QUEUE_ADD, null);
break;
case 75:
mImageView.setBackgroundResource(R.drawable.k);
tts.speak(name + "for kingston", TextToSpeech.QUEUE_ADD, null);
break;
case 76:
mImageView.setBackgroundResource(R.drawable.l);
tts.speak(name + "for Laptop", TextToSpeech.QUEUE_ADD, null);
break;
case 77:
mImageView.setBackgroundResource(R.drawable.m);
tts.speak(name + "for Messenger", TextToSpeech.QUEUE_ADD, null);
break;
case 78:
mImageView.setBackgroundResource(R.drawable.n);
tts.speak(name + "for Nero", TextToSpeech.QUEUE_ADD, null);
break;
case 79:
mImageView.setBackgroundResource(R.drawable.o);
tts.speak(name + "for Orkut", TextToSpeech.QUEUE_ADD, null);
break;
case 80:
mImageView.setBackgroundResource(R.drawable.p);
tts.speak(name + "for Picassa", TextToSpeech.QUEUE_ADD, null);
break;
case 81:
mImageView.setBackgroundResource(R.drawable.q);
tts.speak(name + "for Quick Heal", TextToSpeech.QUEUE_ADD, null);
break;
case 82:
mImageView.setBackgroundResource(R.drawable.r);
tts.speak(name + "for Ram", TextToSpeech.QUEUE_ADD, null);
break;
case 83:
mImageView.setBackgroundResource(R.drawable.s);
tts.speak(name + "for Server", TextToSpeech.QUEUE_ADD, null);
break;
case 84:
mImageView.setBackgroundResource(R.drawable.t);
tts.speak(name + "for Twitter", TextToSpeech.QUEUE_ADD, null);
break;
case 85:
mImageView.setBackgroundResource(R.drawable.u);
tts.speak(name + "for Usb", TextToSpeech.QUEUE_ADD, null);
break;
case 86:
mImageView.setBackgroundResource(R.drawable.v);
tts.speak(name + "for Vista", TextToSpeech.QUEUE_ADD, null);
break;
case 87:
mImageView.setBackgroundResource(R.drawable.w);
tts.speak(name + "for WiFi", TextToSpeech.QUEUE_ADD, null);
break;
case 88:
mImageView.setBackgroundResource(R.drawable.x);
tts.speak(name + "for XP", TextToSpeech.QUEUE_ADD, null);
break;
case 89:
mImageView.setBackgroundResource(R.drawable.y);
tts.speak(name + "for Youtube", TextToSpeech.QUEUE_ADD, null);
break;
case 90:
mImageView.setBackgroundResource(R.drawable.z);
tts.speak(name + "for Zorpia", TextToSpeech.QUEUE_ADD, null);
break;
default:
break;
}
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, RESULT_OK);
final Thread t1 = new Thread() {
public void run() {
try {
Thread.sleep(10000);
finish();
// this.destroy();
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
};
t1.start();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RESULT_OK) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
tts = 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) {
Toast.makeText(this,
"Write and Listen engine is initialized & Ready",
Toast.LENGTH_LONG).show();
} else if (status == TextToSpeech.ERROR) {
Toast.makeText(this,
"Error occurred while initializing Text-To-Speech engine",
Toast.LENGTH_LONG).show();
}
}
protected void onDestroy() {
super.onDestroy();
if (tts != null) {
tts.shutdown();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
OnCreate
中,您编写了此代码,当时尚未安装引擎,因此它给出错误并退出,并且永远不会去安装引擎,
因此在
OnCreate
中执行的第一个任务应该是,您在安装之前很长时间后正在执行此操作,并尝试在安装之前
speak
,因此请将引擎检查的意图写为 onCreate 中的第一行。我明白,其他一切都很好。
In
OnCreate
You have Written this code,At that time Engine is not installed so it Gives error and Quit and never goes to install the Engine,
So First task to do in
OnCreate
should be,Which you are doing long after and trying to
speak
before it's installedSo write that intent for Engine Check as first line in onCreate.Everything else is Fine I see.