2012-04-11 125 views
0

嘿,我創建的應用程序是TextToSpeech功能。我編寫代碼並運行,但沒有任何語音生成。在logcat中顯示一些錯誤。這裏是logcatTextToSpeech:服務未啓動?

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()方法啓動它。

任何幫助表示讚賞。

回答

0

在調用onInit之前,您不能使用tts

目前,您創建它並嘗試在onCreate方法中使用它,但到那時它不會完成初始化。

您還在創建tts兩次。 onActivityResult中的那個是最有意義的,因爲您首先檢查它是否存在。我將擺脫onCreate中的創作,並將所有實際發言放入onInit