2013-01-21 87 views
0

我試圖寫一個應用程序,它應該說什麼字符串我給這裏是我的代碼...是否有任何問題,讓我知道PLZ ....當我運行此代碼我得到android.speech.tts.TextToSpeech服務未啓動 logcat中的錯誤。android.speech.tts.TextToSpeech服務未啓動

注:我已經安裝了谷歌從TTS發揮

public class MainActivity extends Activity implements TextToSpeech.OnInitListener { 

    private TextToSpeech tts; 
    private final int MY_VOICE_CHECK_CODE = 1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Intent voiceCheckIntent = new Intent(); 
     // Intiating voice Recognizer 
     voiceCheckIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
     startActivityForResult(voiceCheckIntent, MY_VOICE_CHECK_CODE); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     switch (requestCode) { 
     case MY_VOICE_CHECK_CODE: { 
      if (requestCode == MY_VOICE_CHECK_CODE) { 
       if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { 
        // On Success, create the TTS(Text To Speech Synthesizer) 
        // instance 
        tts = new TextToSpeech(this, (TextToSpeech.OnInitListener) this); 
        speakOut("hello world"); 
       } else { 
        // missing data, install it, This will give connection to 
        // Google play(TTS data) 
        Intent installIntent = new Intent(); 
        installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
        startActivity(installIntent); 
       } 
      } 
      break; 
     }  

     default: 
      break; 
     } 
    }  

    public void onInit(int status) { 
     if (status == TextToSpeech.SUCCESS) { 

     } else { 

     } 
    } 

    private void speakOut(String text) { 
     tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
    } 

    @Override 
    protected void onDestroy() { 
     if (tts != null) { 
      tts.stop(); 
      tts.shutdown(); 
     } 
     super.onDestroy(); 
    } 

} 
+0

onInit(int status)不被調用 – Pradeep

+0

在onInit()中使用一些日誌來確定它是否被初始化? – Jambaaz

+0

謝謝我修復了這個錯誤 – Pradeep

回答

0

你不能叫tts.speak直到OnInit中被稱爲後。直到那時才完全初始化。