嘿,我正在创建具有 TextToSpeech 功能的应用程序。我编写代码并运行,但没有生成任何
语音。logcat 中显示一些错误。这是logcat
04-11 20:21:30.099: VERBOSE/TtsService(481): TtsService.setLanguage(eng, USA, )
04-11 20:21:30.109: INFO/TextToSpeech.java - speak(849): speak text of length 41
04-11 20:21:30.109: ERROR/TextToSpeech.java - speak(849): service isn't started
我不明白如何解决这个问题......这是我的完整代码。
public class ExamAppearingActivity extends Activity implements OnInitListener
{
private int MY_DATA_CHECK_CODE = 0;
private TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.examquestionscreen);
if (isVoiceEnabled==1)
{
tts = new TextToSpeech(this, this);
final List<ObjectiveWiseQuestion> QuestionWiseProfile1= db.getOneQuestion(examId);
for (final ObjectiveWiseQuestion cn : QuestionWiseProfile1)
{
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
db=new MySQLiteHelper(getBaseContext());
db.getWritableDatabase();
counter=cn.getCounter();
String question="Question is "+cn.getQuestion();
String option1="Option A is "+cn.getOptionA();
String option2="Option B is "+cn.getOptionB();
String option3="Option C is "+cn.getOptionC();
String option4="Option D is "+cn.getOptionD();
tts.speak(question, TextToSpeech.QUEUE_ADD, null);
tts.speak(option1, TextToSpeech.QUEUE_ADD, null);
tts.speak(option2, TextToSpeech.QUEUE_ADD, null);
tts.speak(option3, TextToSpeech.QUEUE_ADD, null);
tts.speak(option4, TextToSpeech.QUEUE_ADD, null);
}
}
}
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
tts = new TextToSpeech(this, this);
}
else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
//tts.isLanguageAvailable(Locale.INDIA_HINDI);
startActivity(installIntent);
}
}
}
@Override
public void onInit(int status)
{
if (status == TextToSpeech.SUCCESS)
{
// tts.setLanguage(Locale.US);
Locale loc = new Locale ("hi_IN");
tts.setLanguage(loc);
Toast.makeText(ExamAppearingActivity.this,"Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
}
else if (status == TextToSpeech.ERROR)
{
Toast.makeText(ExamAppearingActivity.this, "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
}
}
此代码仅在我在按钮单击时添加它时运行,但我需要从
onCreate() 方法启动它。
任何帮助表示赞赏。